Skip to content

Commit

Permalink
perf: spawn new process for removing node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Jan 31, 2024
1 parent d044775 commit 57dda03
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {Config, Errors, Interfaces, ux} from '@oclif/core'
import makeDebug from 'debug'
import {access, mkdir, readFile, rm, writeFile} from 'node:fs/promises'
import {spawn} from 'node:child_process'
import {access, mkdir, readFile, rename, rm, writeFile} from 'node:fs/promises'
import {dirname, join, resolve} from 'node:path'
import {fileURLToPath} from 'node:url'
import {gt, valid, validRange} from 'semver'

import {NPM} from './npm.js'
Expand Down Expand Up @@ -334,11 +336,18 @@ export default class Plugins {
// and node_modules to ensure a clean install or update.
if (await fileExists(join(this.config.dataDir, 'yarn.lock'))) {
this.debug('Found yarn.lock! Removing yarn.lock and node_modules...')
ux.action.status = 'Cleaning up'
await Promise.all([
rename(join(this.config.dataDir, 'node_modules'), join(this.config.dataDir, 'node_modules.old')),
rm(join(this.config.dataDir, 'yarn.lock'), {force: true}),
rm(join(this.config.dataDir, 'node_modules'), {force: true, recursive: true}),
])

// Spawn a new process so that node_modules can be deleted asynchronously.
const rmScript = join(dirname(fileURLToPath(import.meta.url)), 'rm.js')
this.debug(`spawning ${rmScript} to remove node_modules.old`)
spawn(process.argv[0], [rmScript, join(this.config.dataDir, 'node_modules.old')], {
detached: true,
stdio: 'ignore',
}).unref()
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/rm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {rm} from 'node:fs/promises'

const [pathToDelete] = process.argv.slice(2)

await rm(pathToDelete, {force: true, recursive: true})

0 comments on commit 57dda03

Please sign in to comment.