diff --git a/packages/js/.eslintrc.json b/packages/js/.eslintrc.json index c9137d85c3c1d..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"], 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", 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..c32a9e785b257 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, { recursive: 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 () => {