From 8c21fa70f09e7879b6704f3be52cb0219f898336 Mon Sep 17 00:00:00 2001 From: ziebam Date: Mon, 16 Sep 2024 15:37:40 +0000 Subject: [PATCH 1/5] cleanup(js): replace `fs-extra` with `node:fs` --- packages/js/src/executors/swc/swc.impl.ts | 4 +-- .../src/executors/verdaccio/verdaccio.impl.ts | 2 +- .../release-version/release-version.ts | 4 +-- .../utils/assets/copy-assets-handler.spec.ts | 27 +++++++++---------- .../src/utils/assets/copy-assets-handler.ts | 25 ++++++++++------- packages/js/src/utils/inline.ts | 16 ++++++----- .../utils/package-json/update-package-json.ts | 3 +-- packages/js/src/utils/swc/compile-swc.ts | 6 ++--- .../utils/typescript/run-type-check.spec.ts | 7 +++-- 9 files changed, 50 insertions(+), 44 deletions(-) diff --git a/packages/js/src/executors/swc/swc.impl.ts b/packages/js/src/executors/swc/swc.impl.ts index e5a7e40c8667a..05645d52c1ac1 100644 --- a/packages/js/src/executors/swc/swc.impl.ts +++ b/packages/js/src/executors/swc/swc.impl.ts @@ -1,7 +1,7 @@ import { ExecutorContext, readJsonFile } from '@nx/devkit'; import { assetGlobsToFiles, FileInputOutput } from '../../utils/assets/assets'; -import { removeSync } from 'fs-extra'; import { sync as globSync } from 'fast-glob'; +import { rmSync } from 'node:fs'; import { dirname, join, relative, resolve, normalize } from 'path'; import { copyAssets } from '../../utils/assets'; import { checkDependencies } from '../../utils/check-dependencies'; @@ -208,7 +208,7 @@ function removeTmpSwcrc(swcrcPath: string) { swcrcPath.includes(normalize('tmp/')) && swcrcPath.includes('.generated.swcrc') ) { - removeSync(dirname(swcrcPath)); + rmSync(dirname(swcrcPath), { recursive: true, force: true }); } } diff --git a/packages/js/src/executors/verdaccio/verdaccio.impl.ts b/packages/js/src/executors/verdaccio/verdaccio.impl.ts index 45dd5105af071..9f1d4b6b8f744 100644 --- a/packages/js/src/executors/verdaccio/verdaccio.impl.ts +++ b/packages/js/src/executors/verdaccio/verdaccio.impl.ts @@ -1,7 +1,7 @@ import { ExecutorContext, logger } from '@nx/devkit'; -import { existsSync, rmSync } from 'fs-extra'; import { ChildProcess, execSync, fork } from 'child_process'; import * as detectPort from 'detect-port'; +import { existsSync, rmSync } from 'node:fs'; import { join, resolve } from 'path'; import { VerdaccioExecutorSchema } from './schema'; diff --git a/packages/js/src/generators/release-version/release-version.ts b/packages/js/src/generators/release-version/release-version.ts index 583fdf253877e..bbb221bb0489b 100644 --- a/packages/js/src/generators/release-version/release-version.ts +++ b/packages/js/src/generators/release-version/release-version.ts @@ -10,8 +10,8 @@ import { writeJson, } from '@nx/devkit'; import * as chalk from 'chalk'; -import { remove } from 'fs-extra'; import { exec } from 'node:child_process'; +import { rm } from 'node:fs/promises'; import { join } from 'node:path'; import { IMPLICIT_DEFAULT_RELEASE_GROUP } from 'nx/src/command-line/release/config/config'; import { @@ -530,7 +530,7 @@ To fix this you will either need to add a package.json file at that location, or (options.releaseGroup.resolvedVersionPlans || []).forEach((p) => { deleteVersionPlanCallbacks.push(async (dryRun?: boolean) => { if (!dryRun) { - await remove(p.absolutePath); + await rm(p.absolutePath, { recursive: true, force: true }); // the relative path is easier to digest, so use that for // git operations and logging return [p.relativePath]; diff --git a/packages/js/src/utils/assets/copy-assets-handler.spec.ts b/packages/js/src/utils/assets/copy-assets-handler.spec.ts index 057e567942c7d..a65e5a64811c2 100644 --- a/packages/js/src/utils/assets/copy-assets-handler.spec.ts +++ b/packages/js/src/utils/assets/copy-assets-handler.spec.ts @@ -1,7 +1,6 @@ -import * as fs from 'fs'; +import * as fs from 'node:fs'; import * as path from 'path'; import * as os from 'os'; -import * as fse from 'fs-extra'; import { CopyAssetsHandler } from './copy-assets-handler'; @@ -78,12 +77,12 @@ describe('AssetInputOutputHandler', () => { outputDir = path.join(rootDir, 'dist/mylib'); // Reset temp directory - fse.removeSync(rootDir); - fse.mkdirpSync(path.join(projectDir, 'docs/a/b')); + fs.rmSync(rootDir, { recursive: true, force: true }); + fs.mkdirSync(path.join(projectDir, 'docs/a/b'), { recursive: true }); // Workspace ignore files - fse.writeFileSync(path.join(rootDir, '.gitignore'), `git-ignore.md`); - fse.writeFileSync(path.join(rootDir, '.nxignore'), `nx-ignore.md`); + fs.writeFileSync(path.join(rootDir, '.gitignore'), `git-ignore.md`); + fs.writeFileSync(path.join(rootDir, '.nxignore'), `nx-ignore.md`); sut = new CopyAssetsHandler({ rootDir, @@ -179,14 +178,14 @@ describe('AssetInputOutputHandler', () => { }); test('processAllAssetsOnce', async () => { - fse.writeFileSync(path.join(rootDir, 'LICENSE'), 'license'); - fse.writeFileSync(path.join(projectDir, 'README.md'), 'readme'); - fse.writeFileSync(path.join(projectDir, 'docs/test1.md'), 'test'); - fse.writeFileSync(path.join(projectDir, 'docs/test2.md'), 'test'); - fse.writeFileSync(path.join(projectDir, 'docs/ignore.md'), 'IGNORE ME'); - fse.writeFileSync(path.join(projectDir, 'docs/git-ignore.md'), 'IGNORE ME'); - fse.writeFileSync(path.join(projectDir, 'docs/nx-ignore.md'), 'IGNORE ME'); - fse.writeFileSync( + fs.writeFileSync(path.join(rootDir, 'LICENSE'), 'license'); + fs.writeFileSync(path.join(projectDir, 'README.md'), 'readme'); + fs.writeFileSync(path.join(projectDir, 'docs/test1.md'), 'test'); + fs.writeFileSync(path.join(projectDir, 'docs/test2.md'), 'test'); + fs.writeFileSync(path.join(projectDir, 'docs/ignore.md'), 'IGNORE ME'); + fs.writeFileSync(path.join(projectDir, 'docs/git-ignore.md'), 'IGNORE ME'); + fs.writeFileSync(path.join(projectDir, 'docs/nx-ignore.md'), 'IGNORE ME'); + fs.writeFileSync( path.join(projectDir, 'docs/a/b/nested-ignore.md'), 'IGNORE ME' ); diff --git a/packages/js/src/utils/assets/copy-assets-handler.ts b/packages/js/src/utils/assets/copy-assets-handler.ts index ba74b035d3c0f..1a1545cbf2bc6 100644 --- a/packages/js/src/utils/assets/copy-assets-handler.ts +++ b/packages/js/src/utils/assets/copy-assets-handler.ts @@ -1,7 +1,14 @@ import { minimatch } from 'minimatch'; +import { + copyFileSync, + existsSync, + lstatSync, + mkdirSync, + readFileSync, + rmSync, +} from 'node:fs'; import * as pathPosix from 'node:path/posix'; import * as path from 'node:path'; -import * as fse from 'fs-extra'; import ignore from 'ignore'; import * as fg from 'fast-glob'; import { AssetGlob } from './assets'; @@ -34,14 +41,14 @@ interface AssetEntry { export const defaultFileEventHandler = (events: FileEvent[]) => { const dirs = new Set(events.map((event) => path.dirname(event.dest))); - dirs.forEach((d) => fse.ensureDirSync(d)); + dirs.forEach((d) => mkdirSync(d, { recursive: true })); events.forEach((event) => { if (event.type === 'create' || event.type === 'update') { - if (fse.lstatSync(event.src).isFile()) { - fse.copyFileSync(event.src, event.dest); + if (lstatSync(event.src).isFile()) { + copyFileSync(event.src, event.dest); } } else if (event.type === 'delete') { - fse.removeSync(event.dest); + rmSync(event.dest, { recursive: true, force: true }); } else { logger.error(`Unknown file event: ${event.type}`); } @@ -66,10 +73,10 @@ export class CopyAssetsHandler { this.ignore = ignore(); const gitignore = pathPosix.join(opts.rootDir, '.gitignore'); const nxignore = pathPosix.join(opts.rootDir, '.nxignore'); - if (fse.existsSync(gitignore)) - this.ignore.add(fse.readFileSync(gitignore).toString()); - if (fse.existsSync(nxignore)) - this.ignore.add(fse.readFileSync(nxignore).toString()); + if (existsSync(gitignore)) + this.ignore.add(readFileSync(gitignore).toString()); + if (existsSync(nxignore)) + this.ignore.add(readFileSync(nxignore).toString()); this.assetGlobs = opts.assets.map((f) => { let isGlob = false; diff --git a/packages/js/src/utils/inline.ts b/packages/js/src/utils/inline.ts index 7f418cab97984..2372912f2231d 100644 --- a/packages/js/src/utils/inline.ts +++ b/packages/js/src/utils/inline.ts @@ -1,15 +1,15 @@ import type { ExecutorContext, ProjectGraphProjectNode } from '@nx/devkit'; import { normalizePath, readJsonFile } from '@nx/devkit'; import { - copySync, + cpSync, + existsSync, readdirSync, readFileSync, - removeSync, + rmSync, writeFileSync, -} from 'fs-extra'; +} from 'node:fs'; import { join, relative } from 'path'; import type { NormalizedExecutorOptions } from './schema'; -import { existsSync } from 'fs'; interface InlineProjectNode { name: string; @@ -85,7 +85,7 @@ export function postProcessInlinedDependencies( const isBuildable = !!inlineDependency.buildOutputPath; if (isBuildable) { - copySync(depOutputPath, destDepOutputPath, { overwrite: true }); + cpSync(depOutputPath, destDepOutputPath, { recursive: true }); } else { movePackage(depOutputPath, destDepOutputPath); markedForDeletion.add(depOutputPath); @@ -97,7 +97,9 @@ export function postProcessInlinedDependencies( } } - markedForDeletion.forEach((path) => removeSync(path)); + markedForDeletion.forEach((path) => + rmSync(path, { recursive: true, force: true }) + ); updateImports(outputPath, inlinedDepsDestOutputRecord); } @@ -271,7 +273,7 @@ function buildInlineGraphExternals( function movePackage(from: string, to: string) { if (from === to) return; - copySync(from, to, { overwrite: true }); + cpSync(from, to, { force: true }); } function updateImports( diff --git a/packages/js/src/utils/package-json/update-package-json.ts b/packages/js/src/utils/package-json/update-package-json.ts index 558d254963df4..0e53f7f6b118b 100644 --- a/packages/js/src/utils/package-json/update-package-json.ts +++ b/packages/js/src/utils/package-json/update-package-json.ts @@ -21,11 +21,10 @@ import { writeJsonFile, } from '@nx/devkit'; import { DependentBuildableProjectNode } from '../buildable-libs-utils'; +import { existsSync, writeFileSync } from 'node:fs'; import { basename, join, parse } from 'path'; -import { writeFileSync } from 'fs-extra'; import { fileExists } from 'nx/src/utils/fileutils'; import type { PackageJson } from 'nx/src/utils/package-json'; -import { existsSync } from 'fs'; import { readFileMapCache } from 'nx/src/project-graph/nx-deps-cache'; import { getRelativeDirectoryToProjectRoot } from '../get-main-file-dir'; diff --git a/packages/js/src/utils/swc/compile-swc.ts b/packages/js/src/utils/swc/compile-swc.ts index 90d2fed647f91..3ae235d0adde3 100644 --- a/packages/js/src/utils/swc/compile-swc.ts +++ b/packages/js/src/utils/swc/compile-swc.ts @@ -1,7 +1,7 @@ import { cacheDir, ExecutorContext, logger } from '@nx/devkit'; import { exec, execSync } from 'node:child_process'; +import { existsSync, rmSync } from 'node:fs'; import { join } from 'node:path'; -import { existsSync, removeSync } from 'fs-extra'; import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable'; import { NormalizedSwcExecutorOptions, SwcCliOptions } from '../schema'; import { printDiagnostics } from '../typescript/print-diagnostics'; @@ -79,7 +79,7 @@ export async function compileSwc( logger.log(`Compiling with SWC for ${context.projectName}...`); if (normalizedOptions.clean) { - removeSync(normalizedOptions.outputPath); + rmSync(normalizedOptions.outputPath, { recursive: true, force: true }); } const swcCmdLog = execSync(getSwcCmd(normalizedOptions), { @@ -125,7 +125,7 @@ export async function* compileSwcWatch( let initialPostCompile = true; if (normalizedOptions.clean) { - removeSync(normalizedOptions.outputPath); + rmSync(normalizedOptions.outputPath, { recursive: true, force: true }); } return yield* createAsyncIterable<{ success: boolean; outfile: string }>( diff --git a/packages/js/src/utils/typescript/run-type-check.spec.ts b/packages/js/src/utils/typescript/run-type-check.spec.ts index 54b2896bf7be3..5909c7ef76915 100644 --- a/packages/js/src/utils/typescript/run-type-check.spec.ts +++ b/packages/js/src/utils/typescript/run-type-check.spec.ts @@ -1,8 +1,7 @@ +import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { join } from 'path'; import { tmpdir } from 'os'; -import { mkdirpSync, removeSync, writeFileSync } from 'fs-extra'; import { runTypeCheck } from './run-type-check'; -import { readFileSync } from 'fs'; describe('runTypeCheck', () => { let workspaceRoot: string; @@ -13,7 +12,7 @@ describe('runTypeCheck', () => { workspaceRoot = join(tmpdir(), 'nx-type-check-test'); projectRoot = join(workspaceRoot, 'proj'); tsConfigPath = join(workspaceRoot, 'tsconfig.json'); - mkdirpSync(projectRoot); + mkdirSync(projectRoot, { recursive: true }); writeFileSync( tsConfigPath, JSON.stringify( @@ -34,7 +33,7 @@ describe('runTypeCheck', () => { }); afterEach(() => { - removeSync(workspaceRoot); + rmSync(workspaceRoot, { recursive: true, force: true }); }); it('should find type errors', async () => { From 23b31922b544b3c5549806542b97a6c370edfb15 Mon Sep 17 00:00:00 2001 From: ziebam Date: Mon, 16 Sep 2024 15:43:04 +0000 Subject: [PATCH 2/5] cleanup(js): remove `fs-extra` from `package.json` --- packages/js/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/js/package.json b/packages/js/package.json index 4930de9d6a722..da8494ef20e37 100644 --- a/packages/js/package.json +++ b/packages/js/package.json @@ -48,7 +48,6 @@ "columnify": "^1.6.0", "detect-port": "^1.5.1", "fast-glob": "3.2.7", - "fs-extra": "^11.1.0", "ignore": "^5.0.4", "js-tokens": "^4.0.0", "jsonc-parser": "3.2.0", From 4dc430425c4b1d725e8340e22292774fa281994f Mon Sep 17 00:00:00 2001 From: ziebam Date: Mon, 16 Sep 2024 16:32:32 +0000 Subject: [PATCH 3/5] cleanup(js): correct the property from `force` to `recursive` --- packages/js/src/utils/inline.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/js/src/utils/inline.ts b/packages/js/src/utils/inline.ts index 2372912f2231d..c32a9e785b257 100644 --- a/packages/js/src/utils/inline.ts +++ b/packages/js/src/utils/inline.ts @@ -273,7 +273,7 @@ function buildInlineGraphExternals( function movePackage(from: string, to: string) { if (from === to) return; - cpSync(from, to, { force: true }); + cpSync(from, to, { recursive: true }); } function updateImports( From 21ced462e0f7503791543bcca86f4494a7fbc6c9 Mon Sep 17 00:00:00 2001 From: ziebam Date: Tue, 17 Sep 2024 09:44:32 +0000 Subject: [PATCH 4/5] cleanup(js): add an ESLint rule to aid in flight branches/PRs --- packages/js/.eslintrc.json | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/js/.eslintrc.json b/packages/js/.eslintrc.json index c9137d85c3c1d..9dd98796b2265 100644 --- a/packages/js/.eslintrc.json +++ b/packages/js/.eslintrc.json @@ -56,6 +56,13 @@ "babel-plugin-transform-typescript-metadata" ] } + ], + "no-restricted-imports": [ + "error", + { + "name": "fs-extra", + "message": "Please use equivalent utilities from `node:fs` instead." + } ] } } From 545a2a20f3259fcfd07da558dbdab7cc425ef727 Mon Sep 17 00:00:00 2001 From: ziebam Date: Tue, 17 Sep 2024 09:55:09 +0000 Subject: [PATCH 5/5] cleanup(js): move the rule to the correct place --- packages/js/.eslintrc.json | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/js/.eslintrc.json b/packages/js/.eslintrc.json index 9dd98796b2265..3b0e42dd2a4c6 100644 --- a/packages/js/.eslintrc.json +++ b/packages/js/.eslintrc.json @@ -4,7 +4,15 @@ "overrides": [ { "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], - "rules": {} + "rules": { + "no-restricted-imports": [ + "error", + { + "name": "fs-extra", + "message": "Please use equivalent utilities from `node:fs` instead." + } + ] + } }, { "files": ["*.ts", "*.tsx"], @@ -56,13 +64,6 @@ "babel-plugin-transform-typescript-metadata" ] } - ], - "no-restricted-imports": [ - "error", - { - "name": "fs-extra", - "message": "Please use equivalent utilities from `node:fs` instead." - } ] } }