From 8605d35ebb3c3f5f953d862f6f18250c6c55e898 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sat, 6 Jul 2024 21:41:11 -0700 Subject: [PATCH 01/10] remove fs-extra from tools package --- packages/tools/package.json | 1 - packages/tools/src/BoltTool.ts | 18 ++++++++-------- packages/tools/src/LernaTool.ts | 26 +++++++++++++----------- packages/tools/src/PnpmTool.ts | 10 ++++----- packages/tools/src/RootTool.ts | 11 +++++----- packages/tools/src/RushTool.ts | 14 +++++++------ packages/tools/src/YarnTool.ts | 19 ++++++++--------- packages/tools/src/expandPackageGlobs.ts | 18 ++++++++-------- 8 files changed, 60 insertions(+), 57 deletions(-) diff --git a/packages/tools/package.json b/packages/tools/package.json index e1d350d..ea51ada 100644 --- a/packages/tools/package.json +++ b/packages/tools/package.json @@ -9,7 +9,6 @@ "license": "MIT", "main": "dist/manypkg-tools.cjs.js", "dependencies": { - "fs-extra": "^8.1.0", "globby": "^11.0.0", "jju": "^1.4.0", "read-yaml-file": "^1.1.0" diff --git a/packages/tools/src/BoltTool.ts b/packages/tools/src/BoltTool.ts index 8a4005f..2417921 100644 --- a/packages/tools/src/BoltTool.ts +++ b/packages/tools/src/BoltTool.ts @@ -1,5 +1,5 @@ import path from "path"; -import fs from "fs-extra"; +import fs from "fs"; import { Tool, PackageJSON, Packages, InvalidMonorepoError } from "./Tool"; import { @@ -18,9 +18,9 @@ export const BoltTool: Tool = { async isMonorepoRoot(directory: string): Promise { try { - const pkgJson = (await fs.readJson( + const pkgJson = JSON.parse((await fs.promises.readFile( path.join(directory, "package.json") - )) as BoltPackageJSON; + )).toString()) as BoltPackageJSON; if (pkgJson.bolt && pkgJson.bolt.workspaces) { return true; } @@ -35,9 +35,9 @@ export const BoltTool: Tool = { isMonorepoRootSync(directory: string): boolean { try { - const pkgJson = fs.readJsonSync( + const pkgJson = JSON.parse(fs.readFileSync( path.join(directory, "package.json") - ) as BoltPackageJSON; + ).toString()) as BoltPackageJSON; if (pkgJson.bolt && pkgJson.bolt.workspaces) { return true; } @@ -54,9 +54,9 @@ export const BoltTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = (await fs.readJson( + const pkgJson = JSON.parse((await fs.promises.readFile( path.join(rootDir, "package.json") - )) as BoltPackageJSON; + )).toString()) as BoltPackageJSON; if (!pkgJson.bolt || !pkgJson.bolt.workspaces) { throw new InvalidMonorepoError( `Directory ${rootDir} is not a valid ${BoltTool.type} monorepo root: missing bolt.workspaces entry` @@ -88,9 +88,9 @@ export const BoltTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = fs.readJsonSync( + const pkgJson = JSON.parse(fs.readFileSync( path.join(rootDir, "package.json") - ) as BoltPackageJSON; + ).toString()) as BoltPackageJSON; if (!pkgJson.bolt || !pkgJson.bolt.workspaces) { throw new InvalidMonorepoError( `Directory ${directory} is not a valid ${BoltTool.type} monorepo root: missing bolt.workspaces entry` diff --git a/packages/tools/src/LernaTool.ts b/packages/tools/src/LernaTool.ts index 06028dc..61a5063 100644 --- a/packages/tools/src/LernaTool.ts +++ b/packages/tools/src/LernaTool.ts @@ -1,5 +1,5 @@ import path from "path"; -import fs from "fs-extra"; +import fs from "fs"; import { Tool, @@ -23,9 +23,9 @@ export const LernaTool: Tool = { async isMonorepoRoot(directory: string): Promise { try { - const lernaJson = (await fs.readJson( + const lernaJson = JSON.parse((await fs.promises.readFile( path.join(directory, "lerna.json") - )) as LernaJson; + )).toString()) as LernaJson; if (lernaJson.useWorkspaces !== true) { return true; } @@ -40,9 +40,9 @@ export const LernaTool: Tool = { isMonorepoRootSync(directory: string): boolean { try { - const lernaJson = fs.readJsonSync( + const lernaJson = JSON.parse(fs.readFileSync( path.join(directory, "lerna.json") - ) as LernaJson; + ).toString()) as LernaJson; if (lernaJson.useWorkspaces !== true) { return true; } @@ -59,10 +59,12 @@ export const LernaTool: Tool = { const rootDir = path.resolve(directory); try { - const lernaJson = await fs.readJson(path.join(rootDir, "lerna.json")); - const pkgJson = (await fs.readJson( + const lernaJson = JSON.parse((await fs.promises.readFile( + path.join(rootDir, "lerna.json") + )).toString()) as LernaJson; + const pkgJson = JSON.parse((await fs.promises.readFile( path.join(rootDir, "package.json") - )) as PackageJSON; + )).toString()) as PackageJSON; const packageGlobs: string[] = lernaJson.packages || ["packages/*"]; return { @@ -89,12 +91,12 @@ export const LernaTool: Tool = { const rootDir = path.resolve(directory); try { - const lernaJson = fs.readJsonSync( + const lernaJson = JSON.parse(fs.readFileSync( path.join(rootDir, "lerna.json") - ) as LernaJson; - const pkgJson = fs.readJsonSync( + ).toString()) as LernaJson; + const pkgJson = JSON.parse(fs.readFileSync( path.join(rootDir, "package.json") - ) as PackageJSON; + ).toString()) as PackageJSON; const packageGlobs: string[] = lernaJson.packages || ["packages/*"]; return { diff --git a/packages/tools/src/PnpmTool.ts b/packages/tools/src/PnpmTool.ts index 2b58046..ad2eb9b 100644 --- a/packages/tools/src/PnpmTool.ts +++ b/packages/tools/src/PnpmTool.ts @@ -1,6 +1,6 @@ import path from "path"; import readYamlFile, { sync as readYamlFileSync } from "read-yaml-file"; -import fs from "fs-extra"; +import fs from "fs"; import { Tool, @@ -64,9 +64,9 @@ export const PnpmTool: Tool = { const manifest = await readYamlFile<{ packages?: string[] }>( path.join(rootDir, "pnpm-workspace.yaml") ); - const pkgJson = (await fs.readJson( + const pkgJson = JSON.parse((await fs.promises.readFile( path.join(rootDir, "package.json") - )) as PackageJSON; + )).toString()) as PackageJSON; const packageGlobs: string[] = manifest.packages!; return { @@ -96,9 +96,9 @@ export const PnpmTool: Tool = { const manifest = readYamlFileSync<{ packages?: string[] }>( path.join(rootDir, "pnpm-workspace.yaml") ); - const pkgJson = fs.readJsonSync( + const pkgJson = JSON.parse(fs.readFileSync( path.join(rootDir, "package.json") - ) as PackageJSON; + ).toString()) as PackageJSON; const packageGlobs: string[] = manifest.packages!; return { diff --git a/packages/tools/src/RootTool.ts b/packages/tools/src/RootTool.ts index d31b18f..62d6595 100644 --- a/packages/tools/src/RootTool.ts +++ b/packages/tools/src/RootTool.ts @@ -1,5 +1,5 @@ import path from "path"; -import fs from "fs-extra"; +import fs from "fs"; import { Tool, @@ -8,7 +8,6 @@ import { Packages, InvalidMonorepoError, } from "./Tool"; -import { expandPackageGlobs } from "./expandPackageGlobs"; export const RootTool: Tool = { type: "root", @@ -27,9 +26,9 @@ export const RootTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson: PackageJSON = await fs.readJson( + const pkgJson = JSON.parse((await fs.promises.readFile( path.join(rootDir, "package.json") - ); + )).toString()) as PackageJSON; const pkg: Package = { dir: rootDir, relativeDir: ".", @@ -56,9 +55,9 @@ export const RootTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson: PackageJSON = fs.readJsonSync( + const pkgJson = JSON.parse(fs.readFileSync( path.join(rootDir, "package.json") - ); + ).toString()) as PackageJSON; const pkg: Package = { dir: rootDir, relativeDir: ".", diff --git a/packages/tools/src/RushTool.ts b/packages/tools/src/RushTool.ts index 72dd05d..e36f521 100644 --- a/packages/tools/src/RushTool.ts +++ b/packages/tools/src/RushTool.ts @@ -1,5 +1,5 @@ import path from "path"; -import fs from "fs-extra"; +import fs from "fs"; import jju from "jju"; import { @@ -24,7 +24,7 @@ export const RushTool: Tool = { async isMonorepoRoot(directory: string): Promise { try { - await fs.readFile(path.join(directory, "rush.json"), "utf8"); + await fs.promises.readFile(path.join(directory, "rush.json"), "utf8"); return true; } catch (err) { if (err && (err as { code: string }).code === "ENOENT") { @@ -52,7 +52,7 @@ export const RushTool: Tool = { const rootDir = path.resolve(directory); try { - const rushText: string = await fs.readFile( + const rushText: string = await fs.promises.readFile( path.join(rootDir, "rush.json"), "utf8" ); @@ -69,7 +69,9 @@ export const RushTool: Tool = { return { dir, relativeDir: path.relative(directory, dir), - packageJson: await fs.readJson(path.join(dir, "package.json")), + packageJson: JSON.parse((await fs.promises.readFile( + path.join(dir, "package.json") + )).toString()), }; }) ); @@ -107,9 +109,9 @@ export const RushTool: Tool = { path.resolve(rootDir, project.projectFolder) ); const packages: Package[] = directories.map((dir: string) => { - const packageJson: PackageJSON = fs.readJsonSync( + const packageJson: PackageJSON = JSON.parse(fs.readFileSync( path.join(dir, "package.json") - ); + ).toString()); return { dir, relativeDir: path.relative(directory, dir), diff --git a/packages/tools/src/YarnTool.ts b/packages/tools/src/YarnTool.ts index 6d80b22..7736b2e 100644 --- a/packages/tools/src/YarnTool.ts +++ b/packages/tools/src/YarnTool.ts @@ -1,9 +1,8 @@ import path from "path"; -import fs from "fs-extra"; +import fs from "fs"; import { Tool, - Package, PackageJSON, Packages, InvalidMonorepoError, @@ -22,9 +21,9 @@ export const YarnTool: Tool = { async isMonorepoRoot(directory: string): Promise { try { - const pkgJson = (await fs.readJson( + const pkgJson = JSON.parse((await fs.promises.readFile( path.join(directory, "package.json") - )) as YarnPackageJSON; + )).toString()) as YarnPackageJSON; if (pkgJson.workspaces) { if ( Array.isArray(pkgJson.workspaces) || @@ -44,9 +43,9 @@ export const YarnTool: Tool = { isMonorepoRootSync(directory: string): boolean { try { - const pkgJson = fs.readJsonSync( + const pkgJson = JSON.parse(fs.readFileSync( path.join(directory, "package.json") - ) as YarnPackageJSON; + ).toString()) as YarnPackageJSON; if (pkgJson.workspaces) { if ( Array.isArray(pkgJson.workspaces) || @@ -68,9 +67,9 @@ export const YarnTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = (await fs.readJson( + const pkgJson = JSON.parse((await fs.promises.readFile( path.join(rootDir, "package.json") - )) as YarnPackageJSON; + )).toString()) as YarnPackageJSON; const packageGlobs: string[] = Array.isArray(pkgJson.workspaces) ? pkgJson.workspaces : pkgJson.workspaces!.packages; @@ -99,9 +98,9 @@ export const YarnTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = fs.readJsonSync( + const pkgJson = JSON.parse(fs.readFileSync( path.join(rootDir, "package.json") - ) as YarnPackageJSON; + ).toString()) as YarnPackageJSON; const packageGlobs: string[] = Array.isArray(pkgJson.workspaces) ? pkgJson.workspaces : pkgJson.workspaces!.packages; diff --git a/packages/tools/src/expandPackageGlobs.ts b/packages/tools/src/expandPackageGlobs.ts index 3058c31..5307e4e 100644 --- a/packages/tools/src/expandPackageGlobs.ts +++ b/packages/tools/src/expandPackageGlobs.ts @@ -1,8 +1,8 @@ import path from "path"; -import fs from "fs-extra"; +import fs from "fs"; import globby from "globby"; -import { Tool, Package, PackageJSON, Packages, MonorepoRoot } from "./Tool"; +import { Package, PackageJSON } from "./Tool"; /** * This internal method takes a list of one or more directory globs and the absolute path @@ -26,19 +26,21 @@ export async function expandPackageGlobs( const discoveredPackages: Array = await Promise.all( directories.map((dir) => fs - .readJson(path.join(dir, "package.json")) + .promises + .readFile(path.join(dir, "package.json")) .catch((err) => { if (err && (err as { code: string }).code === "ENOENT") { return undefined; } throw err; }) - .then((result: PackageJSON | undefined) => { - if (result) { + .then((result: Buffer | undefined) => { + const s = result?.toString(); + if (s) { return { dir: path.resolve(dir), relativeDir: path.relative(directory, dir), - packageJson: result, + packageJson: JSON.parse(s), }; } }) @@ -68,9 +70,9 @@ export function expandPackageGlobsSync( const discoveredPackages: Array = directories.map( (dir) => { try { - const packageJson: PackageJSON = fs.readJsonSync( + const packageJson: PackageJSON = JSON.parse(fs.readFileSync( path.join(dir, "package.json") - ); + ).toString()); return { dir: path.resolve(dir), relativeDir: path.relative(directory, dir), From 8901fe67c19741b6a2a866118ce1a147dac71219 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sun, 7 Jul 2024 07:27:56 -0700 Subject: [PATCH 02/10] update find-root package --- packages/find-root/package.json | 3 +-- packages/find-root/src/index.ts | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/find-root/package.json b/packages/find-root/package.json index 6753b08..b629536 100644 --- a/packages/find-root/package.json +++ b/packages/find-root/package.json @@ -11,8 +11,7 @@ "module": "dist/manypkg-find-root.esm.js", "dependencies": { "@manypkg/tools": "^1.1.1", - "find-up": "^4.1.0", - "fs-extra": "^8.1.0" + "find-up": "^4.1.0" }, "devDependencies": { "@types/node": "^16.11.7", diff --git a/packages/find-root/src/index.ts b/packages/find-root/src/index.ts index 072138d..c6d4316 100644 --- a/packages/find-root/src/index.ts +++ b/packages/find-root/src/index.ts @@ -1,6 +1,6 @@ import findUp, { sync as findUpSync } from "find-up"; import path from "path"; -import fs from "fs-extra"; +import fs from "fs"; import { Tool, @@ -115,7 +115,7 @@ export async function findRoot( let rootDir = await findUp( async (directory) => { try { - await fs.access(path.join(directory, "package.json")); + await fs.promises.access(path.join(directory, "package.json")); return directory; } catch (err) { if (!isNoEntryError(err)) { From d4c5e671ebdaa9a1d70e46a85c50abc3a6282256 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sun, 7 Jul 2024 08:27:35 -0700 Subject: [PATCH 03/10] move to a util method --- packages/tools/src/BoltTool.ts | 18 +++++------------- packages/tools/src/LernaTool.ts | 22 ++++++---------------- packages/tools/src/PnpmTool.ts | 11 +++-------- packages/tools/src/RootTool.ts | 10 +++------- packages/tools/src/RushTool.ts | 11 +++-------- packages/tools/src/utils.ts | 11 +++++++++++ 6 files changed, 31 insertions(+), 52 deletions(-) create mode 100644 packages/tools/src/utils.ts diff --git a/packages/tools/src/BoltTool.ts b/packages/tools/src/BoltTool.ts index 2417921..a4b60ca 100644 --- a/packages/tools/src/BoltTool.ts +++ b/packages/tools/src/BoltTool.ts @@ -1,11 +1,11 @@ import path from "path"; -import fs from "fs"; import { Tool, PackageJSON, Packages, InvalidMonorepoError } from "./Tool"; import { expandPackageGlobs, expandPackageGlobsSync, } from "./expandPackageGlobs"; +import { readJson, readJsonSync } from "./utils"; export interface BoltPackageJSON extends PackageJSON { bolt?: { @@ -18,9 +18,7 @@ export const BoltTool: Tool = { async isMonorepoRoot(directory: string): Promise { try { - const pkgJson = JSON.parse((await fs.promises.readFile( - path.join(directory, "package.json") - )).toString()) as BoltPackageJSON; + const pkgJson = await readJson(directory, "package.json") as BoltPackageJSON; if (pkgJson.bolt && pkgJson.bolt.workspaces) { return true; } @@ -35,9 +33,7 @@ export const BoltTool: Tool = { isMonorepoRootSync(directory: string): boolean { try { - const pkgJson = JSON.parse(fs.readFileSync( - path.join(directory, "package.json") - ).toString()) as BoltPackageJSON; + const pkgJson = readJsonSync(directory, "package.json") as BoltPackageJSON; if (pkgJson.bolt && pkgJson.bolt.workspaces) { return true; } @@ -54,9 +50,7 @@ export const BoltTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = JSON.parse((await fs.promises.readFile( - path.join(rootDir, "package.json") - )).toString()) as BoltPackageJSON; + const pkgJson = await readJson(rootDir, "package.json") as BoltPackageJSON; if (!pkgJson.bolt || !pkgJson.bolt.workspaces) { throw new InvalidMonorepoError( `Directory ${rootDir} is not a valid ${BoltTool.type} monorepo root: missing bolt.workspaces entry` @@ -88,9 +82,7 @@ export const BoltTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = JSON.parse(fs.readFileSync( - path.join(rootDir, "package.json") - ).toString()) as BoltPackageJSON; + const pkgJson = readJsonSync(rootDir, "package.json") as BoltPackageJSON; if (!pkgJson.bolt || !pkgJson.bolt.workspaces) { throw new InvalidMonorepoError( `Directory ${directory} is not a valid ${BoltTool.type} monorepo root: missing bolt.workspaces entry` diff --git a/packages/tools/src/LernaTool.ts b/packages/tools/src/LernaTool.ts index 61a5063..0b9f3e5 100644 --- a/packages/tools/src/LernaTool.ts +++ b/packages/tools/src/LernaTool.ts @@ -3,7 +3,6 @@ import fs from "fs"; import { Tool, - Package, PackageJSON, Packages, InvalidMonorepoError, @@ -12,6 +11,7 @@ import { expandPackageGlobs, expandPackageGlobsSync, } from "./expandPackageGlobs"; +import { readJson, readJsonSync } from "./utils"; export interface LernaJson { useWorkspaces?: boolean; @@ -23,9 +23,7 @@ export const LernaTool: Tool = { async isMonorepoRoot(directory: string): Promise { try { - const lernaJson = JSON.parse((await fs.promises.readFile( - path.join(directory, "lerna.json") - )).toString()) as LernaJson; + const lernaJson = await readJson(directory, "lerna.json") as LernaJson; if (lernaJson.useWorkspaces !== true) { return true; } @@ -40,9 +38,7 @@ export const LernaTool: Tool = { isMonorepoRootSync(directory: string): boolean { try { - const lernaJson = JSON.parse(fs.readFileSync( - path.join(directory, "lerna.json") - ).toString()) as LernaJson; + const lernaJson = readJsonSync(directory, "lerna.json") as LernaJson; if (lernaJson.useWorkspaces !== true) { return true; } @@ -59,9 +55,7 @@ export const LernaTool: Tool = { const rootDir = path.resolve(directory); try { - const lernaJson = JSON.parse((await fs.promises.readFile( - path.join(rootDir, "lerna.json") - )).toString()) as LernaJson; + const lernaJson = await readJson(rootDir, "lerna.json") as LernaJson; const pkgJson = JSON.parse((await fs.promises.readFile( path.join(rootDir, "package.json") )).toString()) as PackageJSON; @@ -91,12 +85,8 @@ export const LernaTool: Tool = { const rootDir = path.resolve(directory); try { - const lernaJson = JSON.parse(fs.readFileSync( - path.join(rootDir, "lerna.json") - ).toString()) as LernaJson; - const pkgJson = JSON.parse(fs.readFileSync( - path.join(rootDir, "package.json") - ).toString()) as PackageJSON; + const lernaJson = readJsonSync(rootDir, "lerna.json") as LernaJson; + const pkgJson = readJsonSync(rootDir, "package.json") as PackageJSON; const packageGlobs: string[] = lernaJson.packages || ["packages/*"]; return { diff --git a/packages/tools/src/PnpmTool.ts b/packages/tools/src/PnpmTool.ts index ad2eb9b..fcfd749 100644 --- a/packages/tools/src/PnpmTool.ts +++ b/packages/tools/src/PnpmTool.ts @@ -1,10 +1,8 @@ import path from "path"; import readYamlFile, { sync as readYamlFileSync } from "read-yaml-file"; -import fs from "fs"; import { Tool, - Package, PackageJSON, Packages, InvalidMonorepoError, @@ -13,6 +11,7 @@ import { expandPackageGlobs, expandPackageGlobsSync, } from "./expandPackageGlobs"; +import { readJson, readJsonSync } from "./utils"; export interface PnpmWorkspaceYaml { packages?: string[]; @@ -64,9 +63,7 @@ export const PnpmTool: Tool = { const manifest = await readYamlFile<{ packages?: string[] }>( path.join(rootDir, "pnpm-workspace.yaml") ); - const pkgJson = JSON.parse((await fs.promises.readFile( - path.join(rootDir, "package.json") - )).toString()) as PackageJSON; + const pkgJson = await readJson(rootDir, "package.json") as PackageJSON; const packageGlobs: string[] = manifest.packages!; return { @@ -96,9 +93,7 @@ export const PnpmTool: Tool = { const manifest = readYamlFileSync<{ packages?: string[] }>( path.join(rootDir, "pnpm-workspace.yaml") ); - const pkgJson = JSON.parse(fs.readFileSync( - path.join(rootDir, "package.json") - ).toString()) as PackageJSON; + const pkgJson = readJsonSync(rootDir, "package.json") as PackageJSON; const packageGlobs: string[] = manifest.packages!; return { diff --git a/packages/tools/src/RootTool.ts b/packages/tools/src/RootTool.ts index 62d6595..2ab2c0e 100644 --- a/packages/tools/src/RootTool.ts +++ b/packages/tools/src/RootTool.ts @@ -1,5 +1,4 @@ import path from "path"; -import fs from "fs"; import { Tool, @@ -8,6 +7,7 @@ import { Packages, InvalidMonorepoError, } from "./Tool"; +import { readJson, readJsonSync } from "./utils"; export const RootTool: Tool = { type: "root", @@ -26,9 +26,7 @@ export const RootTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = JSON.parse((await fs.promises.readFile( - path.join(rootDir, "package.json") - )).toString()) as PackageJSON; + const pkgJson = await readJson(rootDir, "package.json") as PackageJSON; const pkg: Package = { dir: rootDir, relativeDir: ".", @@ -55,9 +53,7 @@ export const RootTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = JSON.parse(fs.readFileSync( - path.join(rootDir, "package.json") - ).toString()) as PackageJSON; + const pkgJson = readJsonSync(rootDir, "package.json") as PackageJSON; const pkg: Package = { dir: rootDir, relativeDir: ".", diff --git a/packages/tools/src/RushTool.ts b/packages/tools/src/RushTool.ts index e36f521..a0504b9 100644 --- a/packages/tools/src/RushTool.ts +++ b/packages/tools/src/RushTool.ts @@ -9,6 +9,7 @@ import { Packages, InvalidMonorepoError, } from "./Tool"; +import { readJson, readJsonSync } from "./utils"; interface RushJson { projects: RushProject[]; @@ -32,7 +33,6 @@ export const RushTool: Tool = { } throw err; } - return false; }, isMonorepoRootSync(directory: string): boolean { @@ -45,7 +45,6 @@ export const RushTool: Tool = { } throw err; } - return false; }, async getPackages(directory: string): Promise { @@ -69,9 +68,7 @@ export const RushTool: Tool = { return { dir, relativeDir: path.relative(directory, dir), - packageJson: JSON.parse((await fs.promises.readFile( - path.join(dir, "package.json") - )).toString()), + packageJson: await readJson(dir, "package.json"), }; }) ); @@ -109,9 +106,7 @@ export const RushTool: Tool = { path.resolve(rootDir, project.projectFolder) ); const packages: Package[] = directories.map((dir: string) => { - const packageJson: PackageJSON = JSON.parse(fs.readFileSync( - path.join(dir, "package.json") - ).toString()); + const packageJson: PackageJSON = readJsonSync(dir, "package.json"); return { dir, relativeDir: path.relative(directory, dir), diff --git a/packages/tools/src/utils.ts b/packages/tools/src/utils.ts new file mode 100644 index 0000000..a68828e --- /dev/null +++ b/packages/tools/src/utils.ts @@ -0,0 +1,11 @@ +import fs from "fs"; +import fsp from "fs/promises"; +import path from "path"; + +export const readJson = async (directory: string, file: string) => JSON.parse((await fsp.readFile( + path.join(directory, file) + )).toString()); + +export const readJsonSync = (directory: string, file: string) => JSON.parse(fs.readFileSync( + path.join(directory, file) + ).toString()); From 46727bc71c013aafa7b0dc7590f998a8548fe9eb Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sun, 7 Jul 2024 14:27:31 -0700 Subject: [PATCH 04/10] update from code review --- packages/find-root/src/index.ts | 3 ++- packages/tools/src/LernaTool.ts | 4 +--- packages/tools/src/RushTool.ts | 5 +++-- packages/tools/src/YarnTool.ts | 9 +++------ 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/find-root/src/index.ts b/packages/find-root/src/index.ts index c6d4316..678cdec 100644 --- a/packages/find-root/src/index.ts +++ b/packages/find-root/src/index.ts @@ -1,6 +1,7 @@ import findUp, { sync as findUpSync } from "find-up"; import path from "path"; import fs from "fs"; +import fsp from "fs/promises"; import { Tool, @@ -115,7 +116,7 @@ export async function findRoot( let rootDir = await findUp( async (directory) => { try { - await fs.promises.access(path.join(directory, "package.json")); + await fsp.access(path.join(directory, "package.json")); return directory; } catch (err) { if (!isNoEntryError(err)) { diff --git a/packages/tools/src/LernaTool.ts b/packages/tools/src/LernaTool.ts index 0b9f3e5..266394c 100644 --- a/packages/tools/src/LernaTool.ts +++ b/packages/tools/src/LernaTool.ts @@ -56,9 +56,7 @@ export const LernaTool: Tool = { try { const lernaJson = await readJson(rootDir, "lerna.json") as LernaJson; - const pkgJson = JSON.parse((await fs.promises.readFile( - path.join(rootDir, "package.json") - )).toString()) as PackageJSON; + const pkgJson = await readJson(rootDir, "package.json") as PackageJSON; const packageGlobs: string[] = lernaJson.packages || ["packages/*"]; return { diff --git a/packages/tools/src/RushTool.ts b/packages/tools/src/RushTool.ts index a0504b9..3921e95 100644 --- a/packages/tools/src/RushTool.ts +++ b/packages/tools/src/RushTool.ts @@ -1,5 +1,6 @@ import path from "path"; import fs from "fs"; +import fsp from "fs/promises"; import jju from "jju"; import { @@ -25,7 +26,7 @@ export const RushTool: Tool = { async isMonorepoRoot(directory: string): Promise { try { - await fs.promises.readFile(path.join(directory, "rush.json"), "utf8"); + await fsp.readFile(path.join(directory, "rush.json"), "utf8"); return true; } catch (err) { if (err && (err as { code: string }).code === "ENOENT") { @@ -51,7 +52,7 @@ export const RushTool: Tool = { const rootDir = path.resolve(directory); try { - const rushText: string = await fs.promises.readFile( + const rushText: string = await fsp.readFile( path.join(rootDir, "rush.json"), "utf8" ); diff --git a/packages/tools/src/YarnTool.ts b/packages/tools/src/YarnTool.ts index 7736b2e..740976b 100644 --- a/packages/tools/src/YarnTool.ts +++ b/packages/tools/src/YarnTool.ts @@ -11,6 +11,7 @@ import { expandPackageGlobs, expandPackageGlobsSync, } from "./expandPackageGlobs"; +import { readJson, readJsonSync } from "./utils"; export interface YarnPackageJSON extends PackageJSON { workspaces?: string[] | { packages: string[] }; @@ -21,9 +22,7 @@ export const YarnTool: Tool = { async isMonorepoRoot(directory: string): Promise { try { - const pkgJson = JSON.parse((await fs.promises.readFile( - path.join(directory, "package.json") - )).toString()) as YarnPackageJSON; + const pkgJson = await readJson(directory, "package.json") as YarnPackageJSON; if (pkgJson.workspaces) { if ( Array.isArray(pkgJson.workspaces) || @@ -43,9 +42,7 @@ export const YarnTool: Tool = { isMonorepoRootSync(directory: string): boolean { try { - const pkgJson = JSON.parse(fs.readFileSync( - path.join(directory, "package.json") - ).toString()) as YarnPackageJSON; + const pkgJson = readJsonSync(directory, "package.json") as YarnPackageJSON; if (pkgJson.workspaces) { if ( Array.isArray(pkgJson.workspaces) || From 5822e8f9a6a2f46f37cca938790a63a47b23df73 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sun, 7 Jul 2024 14:29:11 -0700 Subject: [PATCH 05/10] one more --- packages/tools/src/LernaTool.ts | 1 - packages/tools/src/YarnTool.ts | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/tools/src/LernaTool.ts b/packages/tools/src/LernaTool.ts index 266394c..51cee9a 100644 --- a/packages/tools/src/LernaTool.ts +++ b/packages/tools/src/LernaTool.ts @@ -1,5 +1,4 @@ import path from "path"; -import fs from "fs"; import { Tool, diff --git a/packages/tools/src/YarnTool.ts b/packages/tools/src/YarnTool.ts index 740976b..59f900c 100644 --- a/packages/tools/src/YarnTool.ts +++ b/packages/tools/src/YarnTool.ts @@ -64,9 +64,7 @@ export const YarnTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = JSON.parse((await fs.promises.readFile( - path.join(rootDir, "package.json") - )).toString()) as YarnPackageJSON; + const pkgJson = await readJson(rootDir, "package.json") as YarnPackageJSON; const packageGlobs: string[] = Array.isArray(pkgJson.workspaces) ? pkgJson.workspaces : pkgJson.workspaces!.packages; From 6ed5824dadf7d46ba676cd549d20541c68d4a3b5 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sun, 7 Jul 2024 14:30:32 -0700 Subject: [PATCH 06/10] and another one --- packages/tools/src/YarnTool.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/tools/src/YarnTool.ts b/packages/tools/src/YarnTool.ts index 59f900c..406be58 100644 --- a/packages/tools/src/YarnTool.ts +++ b/packages/tools/src/YarnTool.ts @@ -1,5 +1,4 @@ import path from "path"; -import fs from "fs"; import { Tool, @@ -93,9 +92,7 @@ export const YarnTool: Tool = { const rootDir = path.resolve(directory); try { - const pkgJson = JSON.parse(fs.readFileSync( - path.join(rootDir, "package.json") - ).toString()) as YarnPackageJSON; + const pkgJson = readJsonSync(rootDir, "package.json") as YarnPackageJSON; const packageGlobs: string[] = Array.isArray(pkgJson.workspaces) ? pkgJson.workspaces : pkgJson.workspaces!.packages; From dde20fcab7015ec9bfb993ee35d2a0c5b7de032f Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 8 Jul 2024 06:32:27 -0700 Subject: [PATCH 07/10] import fs/promises one last place --- packages/tools/src/expandPackageGlobs.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/tools/src/expandPackageGlobs.ts b/packages/tools/src/expandPackageGlobs.ts index 5307e4e..17af518 100644 --- a/packages/tools/src/expandPackageGlobs.ts +++ b/packages/tools/src/expandPackageGlobs.ts @@ -1,5 +1,6 @@ import path from "path"; import fs from "fs"; +import fsp from "fs/promises"; import globby from "globby"; import { Package, PackageJSON } from "./Tool"; @@ -25,8 +26,7 @@ export async function expandPackageGlobs( const discoveredPackages: Array = await Promise.all( directories.map((dir) => - fs - .promises + fsp .readFile(path.join(dir, "package.json")) .catch((err) => { if (err && (err as { code: string }).code === "ENOENT") { From 69ec534d28ebe6eaf2a4dd500092585cfd673be9 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 8 Jul 2024 06:37:05 -0700 Subject: [PATCH 08/10] use util --- packages/tools/src/expandPackageGlobs.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/tools/src/expandPackageGlobs.ts b/packages/tools/src/expandPackageGlobs.ts index 17af518..fc94c2d 100644 --- a/packages/tools/src/expandPackageGlobs.ts +++ b/packages/tools/src/expandPackageGlobs.ts @@ -1,9 +1,9 @@ import path from "path"; -import fs from "fs"; import fsp from "fs/promises"; import globby from "globby"; import { Package, PackageJSON } from "./Tool"; +import { readJsonSync } from "./utils"; /** * This internal method takes a list of one or more directory globs and the absolute path @@ -70,9 +70,7 @@ export function expandPackageGlobsSync( const discoveredPackages: Array = directories.map( (dir) => { try { - const packageJson: PackageJSON = JSON.parse(fs.readFileSync( - path.join(dir, "package.json") - ).toString()); + const packageJson: PackageJSON = readJsonSync(dir, "package.json"); return { dir: path.resolve(dir), relativeDir: path.relative(directory, dir), From 3cf3ecb50510581b168e259f11f878d72c5fe5d3 Mon Sep 17 00:00:00 2001 From: Emma Hamilton Date: Fri, 9 Aug 2024 10:19:58 +1000 Subject: [PATCH 09/10] Create spotty-owls-pull.md --- .changeset/spotty-owls-pull.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/spotty-owls-pull.md diff --git a/.changeset/spotty-owls-pull.md b/.changeset/spotty-owls-pull.md new file mode 100644 index 0000000..8ade087 --- /dev/null +++ b/.changeset/spotty-owls-pull.md @@ -0,0 +1,6 @@ +--- +"@manypkg/find-root": patch +"@manypkg/tools": patch +--- + +Remove fs-extra from dependencies From 7bffb28352ad429918f833a8ac425102bf0ec3dd Mon Sep 17 00:00:00 2001 From: Emma Hamilton Date: Fri, 9 Aug 2024 10:25:50 +1000 Subject: [PATCH 10/10] Pass encoding into readFile --- packages/tools/src/expandPackageGlobs.ts | 9 ++++----- packages/tools/src/utils.ts | 10 ++++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/tools/src/expandPackageGlobs.ts b/packages/tools/src/expandPackageGlobs.ts index fc94c2d..4c0ee2c 100644 --- a/packages/tools/src/expandPackageGlobs.ts +++ b/packages/tools/src/expandPackageGlobs.ts @@ -27,20 +27,19 @@ export async function expandPackageGlobs( const discoveredPackages: Array = await Promise.all( directories.map((dir) => fsp - .readFile(path.join(dir, "package.json")) + .readFile(path.join(dir, "package.json"), "utf-8") .catch((err) => { if (err && (err as { code: string }).code === "ENOENT") { return undefined; } throw err; }) - .then((result: Buffer | undefined) => { - const s = result?.toString(); - if (s) { + .then((result) => { + if (result) { return { dir: path.resolve(dir), relativeDir: path.relative(directory, dir), - packageJson: JSON.parse(s), + packageJson: JSON.parse(result), }; } }) diff --git a/packages/tools/src/utils.ts b/packages/tools/src/utils.ts index a68828e..9decd1e 100644 --- a/packages/tools/src/utils.ts +++ b/packages/tools/src/utils.ts @@ -3,9 +3,11 @@ import fsp from "fs/promises"; import path from "path"; export const readJson = async (directory: string, file: string) => JSON.parse((await fsp.readFile( - path.join(directory, file) - )).toString()); + path.join(directory, file), + "utf-8" + ))); export const readJsonSync = (directory: string, file: string) => JSON.parse(fs.readFileSync( - path.join(directory, file) - ).toString()); + path.join(directory, file), + "utf-8" + ));