Skip to content

Commit bdb3c28

Browse files
authored
fix: added check for dry-run (#7274)
* fix: added check for dry-run added a check for dry-run for `npm ci` so that node_modules won't be deleted closes #7239 * chore: added test for ci --dry-run check
1 parent 7f1ab88 commit bdb3c28

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

lib/commands/ci.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,18 @@ class CI extends ArboristWorkspaceCmd {
7575
)
7676
}
7777

78-
// Only remove node_modules after we've successfully loaded the virtual
79-
// tree and validated the lockfile
80-
await this.npm.time('npm-ci:rm', async () => {
81-
const path = `${where}/node_modules`
82-
// get the list of entries so we can skip the glob for performance
83-
const entries = await fs.readdir(path, null).catch(er => [])
84-
return Promise.all(entries.map(f => fs.rm(`${path}/${f}`, { force: true, recursive: true })))
85-
})
78+
const dryRun = this.npm.config.get('dry-run')
79+
if (!dryRun) {
80+
// Only remove node_modules after we've successfully loaded the virtual
81+
// tree and validated the lockfile
82+
await this.npm.time('npm-ci:rm', async () => {
83+
const path = `${where}/node_modules`
84+
// get the list of entries so we can skip the glob for performance
85+
const entries = await fs.readdir(path, null).catch(er => [])
86+
return Promise.all(entries.map(f => fs.rm(`${path}/${f}`,
87+
{ force: true, recursive: true })))
88+
})
89+
}
8690

8791
await arb.reify(opts)
8892

test/lib/commands/ci.js

+20
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ const abbrev = {
5656
test: 'test file',
5757
}
5858

59+
t.test('reifies, but doesn\'t remove node_modules because --dry-run', async t => {
60+
const { npm, joinedOutput } = await loadMockNpm(t, {
61+
config: {
62+
'dry-run': true,
63+
},
64+
prefixDir: {
65+
abbrev: abbrev,
66+
'package.json': JSON.stringify(packageJson),
67+
'package-lock.json': JSON.stringify(packageLock),
68+
node_modules: { test: 'test file that will not be removed' },
69+
},
70+
})
71+
await npm.exec('ci', [])
72+
t.match(joinedOutput(), 'added 1 package, and removed 1 package in')
73+
const nmTest = path.join(npm.prefix, 'node_modules', 'test')
74+
t.equal(fs.existsSync(nmTest), true, 'existing node_modules is not removed')
75+
const nmAbbrev = path.join(npm.prefix, 'node_modules', 'abbrev')
76+
t.equal(fs.existsSync(nmAbbrev), false, 'does not install abbrev')
77+
})
78+
5979
t.test('reifies, audits, removes node_modules', async t => {
6080
const { npm, joinedOutput, registry } = await loadMockNpm(t, {
6181
prefixDir: {

0 commit comments

Comments
 (0)