Skip to content

Commit b06e89f

Browse files
ruyadornolukekarrys
authored andcommitted
fix(install): do not install invalid package name
Throws an usage error if finding an invalid argument in global install. Fixes: #3029
1 parent f3d7fff commit b06e89f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/commands/install.js

+6
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ class Install extends ArboristWorkspaceCmd {
139139
args = ['.']
140140
}
141141

142+
// throw usage error if trying to install empty package
143+
// name to global space, e.g: `npm i -g ""`
144+
if (where === globalTop && !args.every(Boolean)) {
145+
throw this.usageError()
146+
}
147+
142148
const opts = {
143149
...this.npm.flatOptions,
144150
auditLevel: null,

test/lib/commands/install.js

+17
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ t.test('should install globally using Arborist', async t => {
139139
t.strictSame(SCRIPTS, [], 'no scripts when installing globally')
140140
})
141141

142+
t.test('should not install invalid global package name', async t => {
143+
const { npm } = await loadMockNpm(t, {
144+
'@npmcli/run-script': () => {},
145+
'../../lib/utils/reify-finish.js': async () => {},
146+
'@npmcli/arborist': function (args) {
147+
throw new Error('should not reify')
148+
},
149+
})
150+
npm.config.set('global', true)
151+
npm.globalPrefix = path.resolve(t.testdir({}))
152+
await t.rejects(
153+
npm.exec('install', ['']),
154+
/Usage:/,
155+
'should not install invalid package name'
156+
)
157+
})
158+
142159
t.test('npm i -g npm engines check success', async t => {
143160
const { npm } = await loadMockNpm(t, {
144161
'../../lib/utils/reify-finish.js': async () => {},

0 commit comments

Comments
 (0)