Skip to content

Commit

Permalink
cleanup(js): replace fs-extra with node:fs (#27932)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziebam authored Sep 17, 2024
1 parent 79443ff commit d38bb78
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 46 deletions.
10 changes: 9 additions & 1 deletion packages/js/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
1 change: 0 additions & 1 deletion packages/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/executors/swc/swc.impl.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 });
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/executors/verdaccio/verdaccio.impl.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/generators/release-version/release-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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];
Expand Down
27 changes: 13 additions & 14 deletions packages/js/src/utils/assets/copy-assets-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'
);
Expand Down
25 changes: 16 additions & 9 deletions packages/js/src/utils/assets/copy-assets-handler.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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}`);
}
Expand All @@ -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;
Expand Down
16 changes: 9 additions & 7 deletions packages/js/src/utils/inline.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -97,7 +97,9 @@ export function postProcessInlinedDependencies(
}
}

markedForDeletion.forEach((path) => removeSync(path));
markedForDeletion.forEach((path) =>
rmSync(path, { recursive: true, force: true })
);
updateImports(outputPath, inlinedDepsDestOutputRecord);
}

Expand Down Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions packages/js/src/utils/package-json/update-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
6 changes: 3 additions & 3 deletions packages/js/src/utils/swc/compile-swc.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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), {
Expand Down Expand Up @@ -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 }>(
Expand Down
7 changes: 3 additions & 4 deletions packages/js/src/utils/typescript/run-type-check.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(
Expand All @@ -34,7 +33,7 @@ describe('runTypeCheck', () => {
});

afterEach(() => {
removeSync(workspaceRoot);
rmSync(workspaceRoot, { recursive: true, force: true });
});

it('should find type errors', async () => {
Expand Down

0 comments on commit d38bb78

Please sign in to comment.