Skip to content

Commit

Permalink
cleanup(core): replace fs-extra with node:fs (#28019)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziebam authored Sep 25, 2024
1 parent 37523fa commit 7f4c39e
Show file tree
Hide file tree
Showing 30 changed files with 160 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('update-ci-webserver-for-static-serve migration', () => {
};
});

afterEach(() => {
afterAll(() => {
tempFs.reset();
});

Expand Down
10 changes: 9 additions & 1 deletion packages/nx/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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"],
Expand Down
1 change: 0 additions & 1 deletion packages/nx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"figures": "3.2.0",
"flat": "^5.0.2",
"front-matter": "^4.0.2",
"fs-extra": "^11.1.0",
"ignore": "^5.0.4",
"jest-diff": "^29.4.1",
"jsonc-parser": "3.2.0",
Expand Down
15 changes: 11 additions & 4 deletions packages/nx/src/command-line/graph/graph.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { createHash } from 'crypto';
import { execSync } from 'node:child_process';
import { existsSync, readFileSync, statSync, writeFileSync } from 'fs';
import { copySync, ensureDirSync } from 'fs-extra';
import {
cpSync,
existsSync,
mkdirSync,
readFileSync,
statSync,
writeFileSync,
} from 'node:fs';
import * as http from 'http';
import { minimatch } from 'minimatch';
import { URL } from 'node:url';
Expand Down Expand Up @@ -416,14 +422,15 @@ export async function generateGraph(
if (ext === '.html') {
const assetsFolder = join(fileFolderPath, 'static');
const assets: string[] = [];
copySync(join(__dirname, '../../core/graph'), assetsFolder, {
cpSync(join(__dirname, '../../core/graph'), assetsFolder, {
filter: (_src, dest) => {
const isntHtml = !/index\.html/.test(dest);
if (isntHtml && dest.includes('.')) {
assets.push(dest);
}
return isntHtml;
},
recursive: true,
});

const { projectGraphClientResponse } =
Expand Down Expand Up @@ -457,7 +464,7 @@ export async function generateGraph(
bodyLines: [fileFolderPath, ...assets],
});
} else if (ext === '.json') {
ensureDirSync(dirname(fullFilePath));
mkdirSync(dirname(fullFilePath), { recursive: true });

const json = await createJsonOutput(
prunedGraph,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as enquirer from 'enquirer';
import { unlinkSync, writeFileSync } from 'fs-extra';
import { unlinkSync, writeFileSync } from 'node:fs';
import { join } from 'path';
import { InitArgs } from '../init-v1';
import { NxJsonConfiguration } from '../../../config/nx-json';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { removeSync } from 'fs-extra';
import { rmSync } from 'node:fs';
import { readJsonFile, writeJsonFile } from '../../../../utils/fileutils';

export function cleanUpFiles(appName: string, isStandalone: boolean) {
Expand All @@ -21,13 +21,13 @@ export function cleanUpFiles(appName: string, isStandalone: boolean) {
}
writeJsonFile(projectJsonPath, json);

removeSync('temp-workspace');
rmSync('temp-workspace', { recursive: true, force: true });

if (isStandalone) {
removeSync('babel.config.json');
removeSync('jest.preset.js');
removeSync('jest.config.ts');
removeSync('libs');
removeSync('tools');
rmSync('babel.config.json', { recursive: true, force: true });
rmSync('jest.preset.js', { recursive: true, force: true });
rmSync('jest.config.ts', { recursive: true, force: true });
rmSync('libs', { recursive: true, force: true });
rmSync('tools', { recursive: true, force: true });
}
}
34 changes: 21 additions & 13 deletions packages/nx/src/command-line/init/implementation/react/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { execSync } from 'child_process';
import { copySync, moveSync, readdirSync, removeSync } from 'fs-extra';
import { join } from 'path';
import { cpSync, mkdirSync, readdirSync, renameSync, rmSync } from 'node:fs';
import { dirname, join } from 'path';
import { InitArgs } from '../../init-v1';
import {
fileExists,
Expand Down Expand Up @@ -163,7 +163,7 @@ async function reorgnizeWorkspaceStructure(options: NormalizedOptions) {
}

function createTempWorkspace(options: NormalizedOptions) {
removeSync('temp-workspace');
rmSync('temp-workspace', { recursive: true, force: true });

execSync(
`npx ${
Expand All @@ -184,12 +184,16 @@ function createTempWorkspace(options: NormalizedOptions) {

output.log({ title: '🧹 Clearing unused files' });

copySync(
cpSync(
join('temp-workspace', 'apps', options.reactAppName, 'project.json'),
'project.json'
'project.json',
{ recursive: true }
);
removeSync(join('temp-workspace', 'apps', options.reactAppName));
removeSync('node_modules');
rmSync(join('temp-workspace', 'apps', options.reactAppName), {
recursive: true,
force: true,
});
rmSync('node_modules', { recursive: true, force: true });
}

function copyPackageJsonDepsFromTempWorkspace() {
Expand Down Expand Up @@ -239,6 +243,13 @@ function overridePackageDeps(
return base;
}

function moveSync(src: string, dest: string) {
const destParentDir = dirname(dest);
mkdirSync(destParentDir, { recursive: true });
rmSync(dest, { recursive: true, force: true });
return renameSync(src, dest);
}

function moveFilesToTempWorkspace(options: NormalizedOptions) {
output.log({ title: '🚚 Moving your React app in your new Nx workspace' });

Expand Down Expand Up @@ -267,10 +278,7 @@ function moveFilesToTempWorkspace(options: NormalizedOptions) {
f,
options.isStandalone
? join('temp-workspace', f)
: join('temp-workspace', 'apps', options.reactAppName, f),
{
overwrite: true,
}
: join('temp-workspace', 'apps', options.reactAppName, f)
);
} catch (error) {
if (requiredCraFiles.includes(f)) {
Expand Down Expand Up @@ -331,7 +339,7 @@ function copyFromTempWorkspaceToRoot() {
output.log({ title: '🚚 Folder restructuring.' });

readdirSync('temp-workspace').forEach((f) => {
moveSync(join('temp-workspace', f), f, { overwrite: true });
moveSync(join('temp-workspace', f), f);
});
}

Expand All @@ -345,6 +353,6 @@ function cleanUpUnusedFilesAndAddConfigFiles(options: NormalizedOptions) {
setupTsConfig(options.reactAppName, options.isStandalone);

if (options.isStandalone) {
removeSync('apps');
rmSync('apps', { recursive: true, force: true });
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync, renameSync } from 'fs-extra';
import { readFileSync, renameSync } from 'node:fs';
import { globWithWorkspaceContext } from '../../../../utils/workspace-context';
import { fileExists } from '../../../../utils/fileutils';

Expand Down
5 changes: 2 additions & 3 deletions packages/nx/src/command-line/release/changelog.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as chalk from 'chalk';
import { prompt } from 'enquirer';
import { removeSync } from 'fs-extra';
import { readFileSync, writeFileSync } from 'node:fs';
import { readFileSync, rmSync, writeFileSync } from 'node:fs';
import { ReleaseType, valid } from 'semver';
import { dirSync } from 'tmp';
import type { DependencyBump } from '../../../release/changelog-renderer';
Expand Down Expand Up @@ -962,7 +961,7 @@ async function applyChangesAndExit(
if (group.resolvedVersionPlans) {
group.resolvedVersionPlans.forEach((plan) => {
if (!args.dryRun) {
removeSync(plan.absolutePath);
rmSync(plan.absolutePath, { recursive: true, force: true });
if (args.verbose) {
console.log(`Removing ${plan.relativePath}`);
}
Expand Down
7 changes: 3 additions & 4 deletions packages/nx/src/command-line/release/config/version-plans.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync, readdirSync } from 'fs';
import { pathExists, stat } from 'fs-extra';
import { exec } from 'node:child_process';
import { existsSync, readFileSync, readdirSync } from 'node:fs';
import { stat } from 'node:fs/promises';
import { join } from 'path';
import { RELEASE_TYPES, ReleaseType } from 'semver';
import { workspaceRoot } from '../../../utils/workspace-root';
Expand Down Expand Up @@ -54,8 +54,7 @@ const versionPlansDirectory = join('.nx', 'version-plans');

export async function readRawVersionPlans(): Promise<RawVersionPlan[]> {
const versionPlansPath = getVersionPlansAbsolutePath();
const versionPlansPathExists = await pathExists(versionPlansPath);
if (!versionPlansPathExists) {
if (!existsSync(versionPlansPath)) {
return [];
}

Expand Down
5 changes: 3 additions & 2 deletions packages/nx/src/command-line/release/plan.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { prompt } from 'enquirer';
import { ensureDir, readFileSync, writeFile, writeFileSync } from 'fs-extra';
import { readFileSync, writeFileSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { RELEASE_TYPES } from 'semver';
import { dirSync } from 'tmp';
Expand Down Expand Up @@ -299,7 +300,7 @@ async function createVersionPlanFileForBumps(
printDiff('', versionPlanFileContent, 1);

const versionPlansAbsolutePath = getVersionPlansAbsolutePath();
await ensureDir(versionPlansAbsolutePath);
await mkdir(versionPlansAbsolutePath, { recursive: true });
await writeFile(
join(versionPlansAbsolutePath, versionPlanFileName),
versionPlanFileContent
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/release/release.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { prompt } from 'enquirer';
import { removeSync } from 'fs-extra';
import { rmSync } from 'node:fs';
import { NxReleaseConfiguration, readNxJson } from '../../config/nx-json';
import { createProjectFileMapUsingProjectGraph } from '../../project-graph/file-map-utils';
import { createProjectGraphAsync } from '../../project-graph/project-graph';
Expand Down Expand Up @@ -162,7 +162,7 @@ export function createAPI(overrideReleaseConfig: NxReleaseConfiguration) {
}
group.resolvedVersionPlans.forEach((plan) => {
if (!args.dryRun) {
removeSync(plan.absolutePath);
rmSync(plan.absolutePath, { recursive: true, force: true });
if (args.verbose) {
console.log(`Removing ${plan.relativePath}`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/command-line/reset/reset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rmSync } from 'fs-extra';
import { rmSync } from 'node:fs';
import { daemonClient } from '../../daemon/client/client';
import { cacheDir, workspaceDataDirectory } from '../../utils/cache-directory';
import { output } from '../../utils/output';
Expand Down
32 changes: 9 additions & 23 deletions packages/nx/src/daemon/cache.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {
existsSync,
readJson,
readJsonSync,
unlinkSync,
writeJson,
} from 'fs-extra';
import { existsSync, unlinkSync } from 'node:fs';
import { join } from 'path';
import { DAEMON_DIR_FOR_CURRENT_WORKSPACE } from './tmp-dir';
import { readJsonFile, writeJsonFileAsync } from '../utils/fileutils';

export interface DaemonProcessJson {
processId: number;
Expand All @@ -17,11 +12,11 @@ export const serverProcessJsonPath = join(
'server-process.json'
);

export async function readDaemonProcessJsonCache(): Promise<DaemonProcessJson | null> {
export function readDaemonProcessJsonCache(): DaemonProcessJson | null {
if (!existsSync(serverProcessJsonPath)) {
return null;
}
return readJsonSync(serverProcessJsonPath);
return readJsonFile(serverProcessJsonPath);
}

export function deleteDaemonJsonProcessCache(): void {
Expand All @@ -35,11 +30,13 @@ export function deleteDaemonJsonProcessCache(): void {
export async function writeDaemonJsonProcessCache(
daemonJson: DaemonProcessJson
): Promise<void> {
await writeJson(serverProcessJsonPath, daemonJson);
await writeJsonFileAsync(serverProcessJsonPath, daemonJson, {
appendNewLine: true,
});
}

export async function waitForDaemonToExitAndCleanupProcessJson(): Promise<void> {
const daemonProcessJson = await readDaemonProcessJsonCache();
const daemonProcessJson = readDaemonProcessJsonCache();
if (daemonProcessJson && daemonProcessJson.processId) {
await new Promise<void>((resolve, reject) => {
let count = 0;
Expand All @@ -63,24 +60,13 @@ export async function waitForDaemonToExitAndCleanupProcessJson(): Promise<void>
}
}

export async function safelyCleanUpExistingProcess(): Promise<void> {
const daemonProcessJson = await readDaemonProcessJsonCache();
if (daemonProcessJson && daemonProcessJson.processId) {
try {
process.kill(daemonProcessJson.processId);
// we wait for the process to actually shut down before returning
await waitForDaemonToExitAndCleanupProcessJson();
} catch {}
}
}

// Must be sync for the help output use case
export function getDaemonProcessIdSync(): number | null {
if (!existsSync(serverProcessJsonPath)) {
return null;
}
try {
const daemonProcessJson = readJsonSync(serverProcessJsonPath);
const daemonProcessJson = readJsonFile(serverProcessJsonPath);
return daemonProcessJson.processId;
} catch {
return null;
Expand Down
15 changes: 11 additions & 4 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { workspaceRoot } from '../../utils/workspace-root';
import { ChildProcess, spawn } from 'child_process';
import { readFileSync, statSync } from 'fs';
import {
existsSync,
mkdirSync,
readFileSync,
statSync,
writeFileSync,
} from 'node:fs';
import { FileHandle, open } from 'fs/promises';
import { ensureDirSync, ensureFileSync } from 'fs-extra';
import { connect } from 'net';
import { join } from 'path';
import { performance } from 'perf_hooks';
Expand Down Expand Up @@ -577,8 +582,10 @@ export class DaemonClient {
}

async startInBackground(): Promise<ChildProcess['pid']> {
ensureDirSync(DAEMON_DIR_FOR_CURRENT_WORKSPACE);
ensureFileSync(DAEMON_OUTPUT_LOG_FILE);
mkdirSync(DAEMON_DIR_FOR_CURRENT_WORKSPACE, { recursive: true });
if (!existsSync(DAEMON_OUTPUT_LOG_FILE)) {
writeFileSync(DAEMON_OUTPUT_LOG_FILE, '');
}

this._out = await open(DAEMON_OUTPUT_LOG_FILE, 'a');
this._err = await open(DAEMON_OUTPUT_LOG_FILE, 'a');
Expand Down
5 changes: 2 additions & 3 deletions packages/nx/src/daemon/tmp-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* location within the OS's tmp directory where we write log files for background processes
* and where we create the actual unix socket/named pipe for the daemon.
*/
import { statSync, writeFileSync } from 'fs';
import { ensureDirSync, rmSync } from 'fs-extra';
import { mkdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { join } from 'path';
import { workspaceDataDirectory } from '../utils/cache-directory';
import { createHash } from 'crypto';
Expand Down Expand Up @@ -64,7 +63,7 @@ export function getSocketDir(alreadyUnique = false) {
process.env.NX_SOCKET_DIR ??
process.env.NX_DAEMON_SOCKET_DIR ??
(alreadyUnique ? tmpdir : socketDirName());
ensureDirSync(dir);
mkdirSync(dir, { recursive: true });

return dir;
} catch (e) {
Expand Down
Loading

0 comments on commit 7f4c39e

Please sign in to comment.