From 48bd306b623c275ad17b1575ba92b5bb6e31c1c3 Mon Sep 17 00:00:00 2001 From: Torathion <145236102+Torathion@users.noreply.github.com> Date: Fri, 8 Mar 2024 02:08:49 +0100 Subject: [PATCH] Replace rimraf with built-in fs.rm (#1372) Replace rimraf with built-in fs.rm --- package.json | 2 -- src/lib/doctor.ts | 19 +++++++++--------- test/cache.test.ts | 17 ++++++++-------- test/doctor.test.ts | 49 ++++++++++++++++++++++----------------------- test/peer.test.ts | 14 ++++++------- 5 files changed, 48 insertions(+), 53 deletions(-) diff --git a/package.json b/package.json index 908afde8..24adc4f4 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,6 @@ "prompts-ncu": "^3.0.0", "rc-config-loader": "^4.1.3", "remote-git-tags": "^3.0.0", - "rimraf": "^5.0.5", "semver": "^7.5.4", "semver-utils": "^1.1.4", "source-map-support": "^0.5.21", @@ -114,7 +113,6 @@ "@types/progress": "^2.0.6", "@types/prompts": "^2.4.7", "@types/remote-git-tags": "^4.0.1", - "@types/rimraf": "^3.0.2", "@types/semver": "^7.5.4", "@types/semver-utils": "^1.1.2", "@types/sinon": "^17.0.0", diff --git a/src/lib/doctor.ts b/src/lib/doctor.ts index 5edaf211..60f01ab9 100644 --- a/src/lib/doctor.ts +++ b/src/lib/doctor.ts @@ -1,5 +1,4 @@ import fs from 'fs/promises' -import { rimraf } from 'rimraf' import spawn from 'spawn-please' import { printUpgrades } from '../lib/logging' import spawnBun from '../package-managers/bun' @@ -50,10 +49,10 @@ const npm = ( options.packageManager === 'pnpm' ? spawnPnpm : options.packageManager === 'yarn' - ? spawnYarn - : options.packageManager === 'bun' - ? spawnBun - : spawnNpm + ? spawnYarn + : options.packageManager === 'bun' + ? spawnBun + : spawnNpm )(args, npmOptions, spawnOptionsMerged as any) } @@ -95,10 +94,10 @@ const doctor = async (run: Run, options: Options): Promise => { options.packageManager === 'yarn' ? 'yarn.lock' : options.packageManager === 'pnpm' - ? 'pnpm-lock.yaml' - : options.packageManager === 'bun' - ? 'bun.lockb' - : 'package-lock.json' + ? 'pnpm-lock.yaml' + : options.packageManager === 'bun' + ? 'bun.lockb' + : 'package-lock.json' const { pkg, pkgFile }: PackageInfo = await loadPackageFileForDoctor(options) // flatten all deps into one so we can iterate over them @@ -233,7 +232,7 @@ const doctor = async (run: Run, options: Options): Promise => { if (lockFile) { await fs.writeFile(lockFileName, lockFile) } else { - await rimraf(lockFileName) + await fs.rm(lockFileName, { recursive: true, force: true }) } // save the last package file with passing tests diff --git a/test/cache.test.ts b/test/cache.test.ts index ea501a1e..0d9748f7 100644 --- a/test/cache.test.ts +++ b/test/cache.test.ts @@ -1,6 +1,5 @@ import { expect } from 'chai' import fs from 'fs/promises' -import { rimraf } from 'rimraf' import ncu from '../src/' import { CACHE_DELIMITER, resolvedDefaultCacheFile } from '../src/lib/cache' import { CacheData } from '../src/types/Cacher' @@ -36,7 +35,7 @@ describe('cache', () => { [`ncu-test-alpha${CACHE_DELIMITER}latest`]: '1.0.0', }) } finally { - await rimraf(resolvedDefaultCacheFile) + await fs.rm(resolvedDefaultCacheFile, { recursive: true, force: true }) stub.restore() } }) @@ -50,12 +49,12 @@ describe('cache', () => { 'ncu-test-alpha': '1.0.0', } : options.target === 'greatest' - ? { - 'ncu-test-v2': '2.0.0', - 'ncu-test-tag': '1.2.0-dev.0', - 'ncu-test-alpha': '2.0.0-alpha.2', - } - : null, + ? { + 'ncu-test-v2': '2.0.0', + 'ncu-test-tag': '1.2.0-dev.0', + 'ncu-test-alpha': '2.0.0-alpha.2', + } + : null, ) try { const packageData = { @@ -96,7 +95,7 @@ describe('cache', () => { [`ncu-test-alpha${CACHE_DELIMITER}greatest`]: '2.0.0-alpha.2', }) } finally { - await rimraf(resolvedDefaultCacheFile) + await fs.rm(resolvedDefaultCacheFile, { recursive: true, force: true }) stub.restore() } }) diff --git a/test/doctor.test.ts b/test/doctor.test.ts index 47e70e50..447a556f 100644 --- a/test/doctor.test.ts +++ b/test/doctor.test.ts @@ -1,7 +1,6 @@ import fs from 'fs/promises' import os from 'os' import path from 'path' -import { rimraf } from 'rimraf' import spawn from 'spawn-please' import { cliOptionsMap } from '../src/cli-options' import { chalkInit } from '../src/lib/chalk' @@ -37,10 +36,10 @@ const testPass = ({ packageManager }: { packageManager: PackageManagerName }) => packageManager === 'yarn' ? 'yarn.lock' : packageManager === 'pnpm' - ? 'pnpm-lock.yaml' - : packageManager === 'bun' - ? 'bun.lockb' - : 'package-lock.json', + ? 'pnpm-lock.yaml' + : packageManager === 'bun' + ? 'bun.lockb' + : 'package-lock.json', ) const pkgOriginal = await fs.readFile(path.join(cwd, 'package.json'), 'utf-8') let stdout = '' @@ -69,13 +68,13 @@ const testPass = ({ packageManager }: { packageManager: PackageManagerName }) => // cleanup before assertions in case they fail await fs.writeFile(pkgPath, pkgOriginal) - rimraf.sync(nodeModulesPath) - rimraf.sync(lockfilePath) + await fs.rm(nodeModulesPath, { recursive: true, force: true }) + await fs.rm(lockfilePath, { recursive: true, force: true }) // delete yarn cache if (packageManager === 'yarn') { - rimraf.sync(path.join(cwd, '.yarn')) - rimraf.sync(path.join(cwd, '.pnp.js')) + await fs.rm(path.join(cwd, '.yarn'), { recursive: true, force: true }) + await fs.rm(path.join(cwd, '.pnp.js'), { recursive: true, force: true }) } // bun prints the run header to stderr instead of stdout @@ -105,10 +104,10 @@ const testFail = ({ packageManager }: { packageManager: PackageManagerName }) => packageManager === 'yarn' ? 'yarn.lock' : packageManager === 'pnpm' - ? 'pnpm-lock.yaml' - : packageManager === 'bun' - ? 'bun.lockb' - : 'package-lock.json', + ? 'pnpm-lock.yaml' + : packageManager === 'bun' + ? 'bun.lockb' + : 'package-lock.json', ) const pkgOriginal = await fs.readFile(path.join(cwd, 'package.json'), 'utf-8') let stdout = '' @@ -134,13 +133,13 @@ const testFail = ({ packageManager }: { packageManager: PackageManagerName }) => } finally { pkgUpgraded = await fs.readFile(pkgPath, 'utf-8') await fs.writeFile(pkgPath, pkgOriginal) - rimraf.sync(nodeModulesPath) - rimraf.sync(lockfilePath) + await fs.rm(nodeModulesPath, { recursive: true, force: true }) + await fs.rm(lockfilePath, { recursive: true, force: true }) // delete yarn cache if (packageManager === 'yarn') { - rimraf.sync(path.join(cwd, '.yarn')) - rimraf.sync(path.join(cwd, '.pnp.js')) + await fs.rm(path.join(cwd, '.yarn'), { recursive: true, force: true }) + await fs.rm(path.join(cwd, '.pnp.js'), { recursive: true, force: true }) } } @@ -235,8 +234,8 @@ describe('doctor', function () { // cleanup before assertions in case they fail await fs.writeFile(pkgPath, pkgOriginal) - rimraf.sync(lockfilePath) - rimraf.sync(nodeModulesPath) + await fs.rm(lockfilePath, { recursive: true, force: true }) + await fs.rm(nodeModulesPath, { recursive: true, force: true }) // stderr should be empty stderr.should.equal('') @@ -277,8 +276,8 @@ describe('doctor', function () { // cleanup before assertions in case they fail await fs.writeFile(pkgPath, pkgOriginal) - rimraf.sync(lockfilePath) - rimraf.sync(nodeModulesPath) + await fs.rm(lockfilePath, { recursive: true, force: true }) + await fs.rm(nodeModulesPath, { recursive: true, force: true }) // stderr should be empty stderr.should.equal('') @@ -318,8 +317,8 @@ describe('doctor', function () { // cleanup before assertions in case they fail await fs.writeFile(pkgPath, pkgOriginal) - rimraf.sync(lockfilePath) - rimraf.sync(nodeModulesPath) + await fs.rm(lockfilePath, { recursive: true, force: true }) + await fs.rm(nodeModulesPath, { recursive: true, force: true }) // stderr should be empty stderr.should.equal('') @@ -359,8 +358,8 @@ describe('doctor', function () { // cleanup before assertions in case they fail await fs.writeFile(pkgPath, pkgOriginal) - rimraf.sync(lockfilePath) - rimraf.sync(nodeModulesPath) + await fs.rm(lockfilePath, { recursive: true, force: true }) + await fs.rm(nodeModulesPath, { recursive: true, force: true }) // stderr should be empty stderr.should.equal('') diff --git a/test/peer.test.ts b/test/peer.test.ts index 0bdc6c3c..ea9b5b50 100644 --- a/test/peer.test.ts +++ b/test/peer.test.ts @@ -1,5 +1,5 @@ +import fs from 'fs/promises' import path from 'path' -import { rimraf } from 'rimraf' import ncu from '../src/' import spawnNpm from '../src/package-managers/npm' import chaiSetup from './helpers/chaiSetup' @@ -16,8 +16,8 @@ describe('peer dependencies', function () { 'ncu-test-return-version': '2.0.0', }) } finally { - rimraf.sync(path.join(cwd, 'node_modules')) - rimraf.sync(path.join(cwd, 'package-lock.json')) + await fs.rm(path.join(cwd, 'node_modules'), { recursive: true, force: true }) + await fs.rm(path.join(cwd, 'package-lock.json'), { recursive: true, force: true }) } }) @@ -30,8 +30,8 @@ describe('peer dependencies', function () { 'ncu-test-return-version': '1.1.0', }) } finally { - rimraf.sync(path.join(cwd, 'node_modules')) - rimraf.sync(path.join(cwd, 'package-lock.json')) + await fs.rm(path.join(cwd, 'node_modules'), { recursive: true, force: true }) + await fs.rm(path.join(cwd, 'package-lock.json'), { recursive: true, force: true }) } }) @@ -45,8 +45,8 @@ describe('peer dependencies', function () { 'ncu-test-peer-update': '1.1.0', }) } finally { - rimraf.sync(path.join(cwd, 'node_modules')) - rimraf.sync(path.join(cwd, 'package-lock.json')) + await fs.rm(path.join(cwd, 'node_modules'), { recursive: true, force: true }) + await fs.rm(path.join(cwd, 'package-lock.json'), { recursive: true, force: true }) } }) })