Skip to content

Commit

Permalink
remove fs-extra from tools package
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jul 7, 2024
1 parent aec5096 commit 8605d35
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 57 deletions.
1 change: 0 additions & 1 deletion packages/tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 9 additions & 9 deletions packages/tools/src/BoltTool.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -18,9 +18,9 @@ export const BoltTool: Tool = {

async isMonorepoRoot(directory: string): Promise<boolean> {
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;
}
Expand All @@ -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;
}
Expand All @@ -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`
Expand Down Expand Up @@ -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`
Expand Down
26 changes: 14 additions & 12 deletions packages/tools/src/LernaTool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
import fs from "fs-extra";
import fs from "fs";

import {
Tool,
Expand All @@ -23,9 +23,9 @@ export const LernaTool: Tool = {

async isMonorepoRoot(directory: string): Promise<boolean> {
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;
}
Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions packages/tools/src/PnpmTool.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions packages/tools/src/RootTool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
import fs from "fs-extra";
import fs from "fs";

import {
Tool,
Expand All @@ -8,7 +8,6 @@ import {
Packages,
InvalidMonorepoError,
} from "./Tool";
import { expandPackageGlobs } from "./expandPackageGlobs";

export const RootTool: Tool = {
type: "root",
Expand All @@ -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: ".",
Expand All @@ -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: ".",
Expand Down
14 changes: 8 additions & 6 deletions packages/tools/src/RushTool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
import fs from "fs-extra";
import fs from "fs";
import jju from "jju";

import {
Expand All @@ -24,7 +24,7 @@ export const RushTool: Tool = {

async isMonorepoRoot(directory: string): Promise<boolean> {
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") {
Expand Down Expand Up @@ -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"
);
Expand All @@ -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()),
};
})
);
Expand Down Expand Up @@ -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),
Expand Down
19 changes: 9 additions & 10 deletions packages/tools/src/YarnTool.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import path from "path";
import fs from "fs-extra";
import fs from "fs";

import {
Tool,
Package,
PackageJSON,
Packages,
InvalidMonorepoError,
Expand All @@ -22,9 +21,9 @@ export const YarnTool: Tool = {

async isMonorepoRoot(directory: string): Promise<boolean> {
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) ||
Expand All @@ -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) ||
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 10 additions & 8 deletions packages/tools/src/expandPackageGlobs.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,19 +26,21 @@ export async function expandPackageGlobs(
const discoveredPackages: Array<Package | undefined> = 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),
};
}
})
Expand Down Expand Up @@ -68,9 +70,9 @@ export function expandPackageGlobsSync(
const discoveredPackages: Array<Package | undefined> = 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),
Expand Down

0 comments on commit 8605d35

Please sign in to comment.