Skip to content

Commit 0e3660e

Browse files
authored
fix(init): allow for spec on scope-only arg (#5206)
1 parent 77bf2e1 commit 0e3660e

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

docs/content/commands/npm-init.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ follows:
3838
* `npm init foo` -> `npm exec create-foo`
3939
* `npm init @usr/foo` -> `npm exec @usr/create-foo`
4040
* `npm init @usr` -> `npm exec @usr/create`
41+
* `npm init @usr@2.0.0` -> `npm exec @usr/create@2.0.0`
42+
* `npm init @usr/foo@2.0.0` -> `npm exec @usr/create-foo@2.0.0`
4143

4244
If the initializer is omitted (by just calling `npm init`), init will fall
4345
back to legacy init behavior. It will ask you a bunch of questions, and

lib/commands/init.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ class Init extends BaseCommand {
8585
const [initerName, ...otherArgs] = args
8686
let packageName = initerName
8787

88+
// Only a scope, possibly with a version
8889
if (/^@[^/]+$/.test(initerName)) {
89-
packageName = initerName + '/create'
90+
const [, scope, version] = initerName.split('@')
91+
packageName = `@${scope}/create`
92+
if (version) {
93+
packageName = `${packageName}@${version}`
94+
}
9095
} else {
9196
const req = npa(initerName)
9297
if (req.type === 'git' && req.hosted) {

test/lib/commands/init.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,44 @@ t.test('npm init @scope/name', async t => {
136136
await init.exec(['@npmcli/something'])
137137
})
138138

139+
t.test('npm init @scope@spec', async t => {
140+
t.plan(1)
141+
npm.localPrefix = t.testdir({})
142+
143+
const Init = t.mock('../../../lib/commands/init.js', {
144+
libnpmexec: ({ args }) => {
145+
t.same(
146+
args,
147+
['@npmcli/create@foo'],
148+
'should npx with scoped packages'
149+
)
150+
},
151+
})
152+
const init = new Init(npm)
153+
154+
process.chdir(npm.localPrefix)
155+
await init.exec(['@npmcli@foo'])
156+
})
157+
158+
t.test('npm init @scope/name@spec', async t => {
159+
t.plan(1)
160+
npm.localPrefix = t.testdir({})
161+
162+
const Init = t.mock('../../../lib/commands/init.js', {
163+
libnpmexec: ({ args }) => {
164+
t.same(
165+
args,
166+
['@npmcli/create-something@foo'],
167+
'should npx with scoped packages'
168+
)
169+
},
170+
})
171+
const init = new Init(npm)
172+
173+
process.chdir(npm.localPrefix)
174+
await init.exec(['@npmcli/something@foo'])
175+
})
176+
139177
t.test('npm init git spec', async t => {
140178
t.plan(1)
141179
npm.localPrefix = t.testdir({})

0 commit comments

Comments
 (0)