From 0f00d4eb545051c9824db80fba1bd8914b5c5f09 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 19 Aug 2022 15:37:33 +0200 Subject: [PATCH] fix: replace non-CLI `rimraf` usage (#13151) --- e2e/Utils.ts | 6 +++--- e2e/__tests__/snapshotMockFs.test.ts | 7 +++---- packages/jest-core/package.json | 4 +--- .../src/__tests__/watchFileChanges.test.ts | 9 ++++----- packages/jest-core/src/cli/index.ts | 13 ++++++++----- scripts/bundleTs.mjs | 16 ++++++++++++---- scripts/cleanE2e.mjs | 4 ++-- scripts/remove-examples.mjs | 6 ++++-- scripts/verifyOldTs.mjs | 3 +-- scripts/verifyPnP.mjs | 3 +-- yarn.lock | 14 +------------- 11 files changed, 40 insertions(+), 45 deletions(-) diff --git a/e2e/Utils.ts b/e2e/Utils.ts index e7bcfb473fb3..9aaecef55e2a 100644 --- a/e2e/Utils.ts +++ b/e2e/Utils.ts @@ -9,7 +9,6 @@ import * as path from 'path'; import dedent = require('dedent'); import {ExecaReturnValue, sync as spawnSync} from 'execa'; import * as fs from 'graceful-fs'; -import rimraf = require('rimraf'); import type {PackageJson} from 'type-fest'; import which = require('which'); import type {Config} from '@jest/types'; @@ -66,7 +65,7 @@ export const linkJestPackage = (packageName: string, cwd: string) => { const packagePath = path.resolve(packagesDir, packageName); const destination = path.resolve(cwd, 'node_modules/', packageName); fs.mkdirSync(destination, {recursive: true}); - rimraf.sync(destination); + fs.rmSync(destination, {force: true, recursive: true}); fs.symlinkSync(packagePath, destination, 'junction'); }; @@ -80,7 +79,8 @@ export const makeTemplate = return values[number - 1]; }); -export const cleanup = (directory: string) => rimraf.sync(directory); +export const cleanup = (directory: string) => + fs.rmSync(directory, {force: true, recursive: true}); /** * Creates a nested directory with files and their contents diff --git a/e2e/__tests__/snapshotMockFs.test.ts b/e2e/__tests__/snapshotMockFs.test.ts index 8622ce90ba53..632b42a7b370 100644 --- a/e2e/__tests__/snapshotMockFs.test.ts +++ b/e2e/__tests__/snapshotMockFs.test.ts @@ -6,16 +6,15 @@ */ import * as path from 'path'; -import rimraf = require('rimraf'); -import {extractSummary} from '../Utils'; +import {cleanup, extractSummary} from '../Utils'; import {json as runJestJson} from '../runJest'; const DIR = path.resolve(__dirname, '../snapshot-mock-fs'); const snapshotDir = path.resolve(DIR, '__tests__/__snapshots__'); const snapshotFile = path.resolve(snapshotDir, 'snapshot.test.js.snap'); -beforeEach(() => rimraf.sync(snapshotDir)); -afterAll(() => rimraf.sync(snapshotDir)); +beforeEach(() => cleanup(snapshotDir)); +afterAll(() => cleanup(snapshotDir)); test('store snapshot even if fs is mocked', () => { const {json, exitCode, stderr} = runJestJson(DIR, ['--ci=false']); diff --git a/packages/jest-core/package.json b/packages/jest-core/package.json index 19e85dd7f666..cb8750159c31 100644 --- a/packages/jest-core/package.json +++ b/packages/jest-core/package.json @@ -38,7 +38,6 @@ "jest-watcher": "workspace:^", "micromatch": "^4.0.4", "pretty-format": "workspace:^", - "rimraf": "^3.0.0", "slash": "^3.0.0", "strip-ansi": "^6.0.0" }, @@ -47,8 +46,7 @@ "@jest/test-utils": "workspace:^", "@types/exit": "^0.1.30", "@types/graceful-fs": "^4.1.3", - "@types/micromatch": "^4.0.1", - "@types/rimraf": "^3.0.0" + "@types/micromatch": "^4.0.1" }, "peerDependencies": { "node-notifier": "^8.0.1 || ^9.0.0 || ^10.0.0" diff --git a/packages/jest-core/src/__tests__/watchFileChanges.test.ts b/packages/jest-core/src/__tests__/watchFileChanges.test.ts index b06095cba4ac..2f736b547d02 100644 --- a/packages/jest-core/src/__tests__/watchFileChanges.test.ts +++ b/packages/jest-core/src/__tests__/watchFileChanges.test.ts @@ -9,7 +9,6 @@ import {tmpdir} from 'os'; import * as path from 'path'; import * as fs from 'graceful-fs'; -import rimraf = require('rimraf'); import type {AggregatedResult} from '@jest/test-result'; import {normalize} from 'jest-config'; import type HasteMap from 'jest-haste-map'; @@ -36,8 +35,8 @@ describe('Watch mode flows with changed files', () => { watch = interopRequireDefault(require('../watch')).default; pipe = {write: jest.fn()} as unknown; stdin = new MockStdin(); - rimraf.sync(cacheDirectory); - rimraf.sync(testDirectory); + fs.rmSync(cacheDirectory, {force: true, recursive: true}); + fs.rmSync(testDirectory, {force: true, recursive: true}); fs.mkdirSync(testDirectory); fs.mkdirSync(cacheDirectory); }); @@ -47,8 +46,8 @@ describe('Watch mode flows with changed files', () => { if (hasteMapInstance) { hasteMapInstance.end(); } - rimraf.sync(cacheDirectory); - rimraf.sync(testDirectory); + fs.rmSync(cacheDirectory, {force: true, recursive: true}); + fs.rmSync(testDirectory, {force: true, recursive: true}); }); it('should correct require new files without legacy cache', async () => { diff --git a/packages/jest-core/src/cli/index.ts b/packages/jest-core/src/cli/index.ts index 29487d5aff3c..6556a919ebb9 100644 --- a/packages/jest-core/src/cli/index.ts +++ b/packages/jest-core/src/cli/index.ts @@ -7,7 +7,7 @@ import chalk = require('chalk'); import exit = require('exit'); -import rimraf = require('rimraf'); +import * as fs from 'graceful-fs'; import {CustomConsole} from '@jest/console'; import type {AggregatedResult, TestContext} from '@jest/test-result'; import type {Config} from '@jest/types'; @@ -63,10 +63,13 @@ export async function runCLI( } if (argv.clearCache) { - configs.forEach(config => { - rimraf.sync(config.cacheDirectory); - process.stdout.write(`Cleared ${config.cacheDirectory}\n`); - }); + // stick in a Set to dedupe the deletions + new Set(configs.map(config => config.cacheDirectory)).forEach( + cacheDirectory => { + fs.rmSync(cacheDirectory, {force: true, recursive: true}); + process.stdout.write(`Cleared ${cacheDirectory}\n`); + }, + ); exit(0); } diff --git a/scripts/bundleTs.mjs b/scripts/bundleTs.mjs index 4961d41ad2c6..6e7b9df4a1e3 100644 --- a/scripts/bundleTs.mjs +++ b/scripts/bundleTs.mjs @@ -17,7 +17,6 @@ import chalk from 'chalk'; import fs from 'graceful-fs'; import {sync as pkgDir} from 'pkg-dir'; import prettier from 'prettier'; -import rimraf from 'rimraf'; import {getPackages} from './buildUtils.mjs'; const prettierConfig = prettier.resolveConfig.sync( @@ -166,10 +165,19 @@ const excludedPackages = new Set(['@jest/globals']); let definitionFile = await fs.promises.readFile(filepath, 'utf8'); - rimraf.sync(path.resolve(packageDir, 'build/**/*.d.ts')); - rimraf.sync(path.resolve(packageDir, 'dist/')); + fs.rmSync(path.resolve(packageDir, 'build/**/*.d.ts'), { + force: true, + recursive: true, + }); + fs.rmSync(path.resolve(packageDir, 'dist/'), { + force: true, + recursive: true, + }); // this is invalid now, so remove it to not confuse `tsc` - rimraf.sync(path.resolve(packageDir, 'tsconfig.tsbuildinfo')); + fs.rmSync(path.resolve(packageDir, 'tsconfig.tsbuildinfo'), { + force: true, + recursive: true, + }); definitionFile = definitionFile.replace(/\r\n/g, '\n'); diff --git a/scripts/cleanE2e.mjs b/scripts/cleanE2e.mjs index 7329b799c5ae..c7ba33119d01 100644 --- a/scripts/cleanE2e.mjs +++ b/scripts/cleanE2e.mjs @@ -8,7 +8,7 @@ import {dirname, normalize, resolve} from 'path'; import {fileURLToPath} from 'url'; import glob from 'glob'; -import rimraf from 'rimraf'; +import fs from 'graceful-fs'; const excludedModules = [ 'e2e/global-setup-node-modules/node_modules/', @@ -30,5 +30,5 @@ const e2eNodeModules = glob.sync('e2e/{*,*/*}/node_modules/', { }); e2eNodeModules.forEach(dir => { - rimraf.sync(dir, {glob: false}); + fs.rmSync(dir, {force: true, recursive: true}); }); diff --git a/scripts/remove-examples.mjs b/scripts/remove-examples.mjs index 662b85bacc10..b94e0f573a1a 100644 --- a/scripts/remove-examples.mjs +++ b/scripts/remove-examples.mjs @@ -8,7 +8,6 @@ import path from 'path'; import {fileURLToPath} from 'url'; import fs from 'graceful-fs'; -import rimraf from 'rimraf'; import config from '../jest.config.mjs'; const dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -22,4 +21,7 @@ fs.writeFileSync( `export default ${JSON.stringify(config, null, 2)};\n`, ); -rimraf.sync(path.resolve(dirname, '../examples/')); +fs.rmSync(path.resolve(dirname, '../examples/'), { + force: true, + recursive: true, +}); diff --git a/scripts/verifyOldTs.mjs b/scripts/verifyOldTs.mjs index 00ec841a1161..fae39e66a4cb 100644 --- a/scripts/verifyOldTs.mjs +++ b/scripts/verifyOldTs.mjs @@ -11,7 +11,6 @@ import {fileURLToPath} from 'url'; import chalk from 'chalk'; import execa from 'execa'; import fs from 'graceful-fs'; -import rimraf from 'rimraf'; import stripJsonComments from 'strip-json-comments'; import tempy from 'tempy'; const require = createRequire(import.meta.url); @@ -70,7 +69,7 @@ function smoketest() { ), ); } finally { - rimraf.sync(cwd); + fs.rmSync(cwd, {force: true, recursive: true}); } } diff --git a/scripts/verifyPnP.mjs b/scripts/verifyPnP.mjs index 294ca451544c..56d3916c6c90 100644 --- a/scripts/verifyPnP.mjs +++ b/scripts/verifyPnP.mjs @@ -12,7 +12,6 @@ import dedent from 'dedent'; import execa from 'execa'; import fs from 'graceful-fs'; import yaml from 'js-yaml'; -import rimraf from 'rimraf'; import tempy from 'tempy'; const rootDirectory = path.resolve( @@ -78,5 +77,5 @@ try { console.log(chalk.inverse.green(' Successfully ran Jest with PnP linker ')); } finally { - rimraf.sync(cwd); + fs.rmSync(cwd, {force: true, recursive: true}); } diff --git a/yarn.lock b/yarn.lock index 815500a8368d..d20435f0e367 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2619,7 +2619,6 @@ __metadata: "@types/graceful-fs": ^4.1.3 "@types/micromatch": ^4.0.1 "@types/node": "*" - "@types/rimraf": ^3.0.0 ansi-escapes: ^4.2.1 chalk: ^4.0.0 ci-info: ^3.2.0 @@ -2640,7 +2639,6 @@ __metadata: jest-watcher: "workspace:^" micromatch: ^4.0.4 pretty-format: "workspace:^" - rimraf: ^3.0.0 slash: ^3.0.0 strip-ansi: ^6.0.0 peerDependencies: @@ -4522,7 +4520,7 @@ __metadata: languageName: node linkType: hard -"@types/glob@npm:*, @types/glob@npm:^7.1.1": +"@types/glob@npm:^7.1.1": version: 7.2.0 resolution: "@types/glob@npm:7.2.0" dependencies: @@ -4920,16 +4918,6 @@ __metadata: languageName: node linkType: hard -"@types/rimraf@npm:^3.0.0": - version: 3.0.2 - resolution: "@types/rimraf@npm:3.0.2" - dependencies: - "@types/glob": "*" - "@types/node": "*" - checksum: b47fa302f46434cba704d20465861ad250df79467d3d289f9d6490d3aeeb41e8cb32dd80bd1a8fd833d1e185ac719fbf9be12e05ad9ce9be094d8ee8f1405347 - languageName: node - linkType: hard - "@types/sax@npm:^1.2.1": version: 1.2.4 resolution: "@types/sax@npm:1.2.4"