Skip to content

Commit

Permalink
fix(init): allow for spec on scope-only arg (#5206)
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jul 27, 2022
1 parent 77bf2e1 commit 0e3660e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/content/commands/npm-init.md
Expand Up @@ -38,6 +38,8 @@ follows:
* `npm init foo` -> `npm exec create-foo`
* `npm init @usr/foo` -> `npm exec @usr/create-foo`
* `npm init @usr` -> `npm exec @usr/create`
* `npm init @usr@2.0.0` -> `npm exec @usr/create@2.0.0`
* `npm init @usr/foo@2.0.0` -> `npm exec @usr/create-foo@2.0.0`

If the initializer is omitted (by just calling `npm init`), init will fall
back to legacy init behavior. It will ask you a bunch of questions, and
Expand Down
7 changes: 6 additions & 1 deletion lib/commands/init.js
Expand Up @@ -85,8 +85,13 @@ class Init extends BaseCommand {
const [initerName, ...otherArgs] = args
let packageName = initerName

// Only a scope, possibly with a version
if (/^@[^/]+$/.test(initerName)) {
packageName = initerName + '/create'
const [, scope, version] = initerName.split('@')
packageName = `@${scope}/create`
if (version) {
packageName = `${packageName}@${version}`
}
} else {
const req = npa(initerName)
if (req.type === 'git' && req.hosted) {
Expand Down
38 changes: 38 additions & 0 deletions test/lib/commands/init.js
Expand Up @@ -136,6 +136,44 @@ t.test('npm init @scope/name', async t => {
await init.exec(['@npmcli/something'])
})

t.test('npm init @scope@spec', async t => {
t.plan(1)
npm.localPrefix = t.testdir({})

const Init = t.mock('../../../lib/commands/init.js', {
libnpmexec: ({ args }) => {
t.same(
args,
['@npmcli/create@foo'],
'should npx with scoped packages'
)
},
})
const init = new Init(npm)

process.chdir(npm.localPrefix)
await init.exec(['@npmcli@foo'])
})

t.test('npm init @scope/name@spec', async t => {
t.plan(1)
npm.localPrefix = t.testdir({})

const Init = t.mock('../../../lib/commands/init.js', {
libnpmexec: ({ args }) => {
t.same(
args,
['@npmcli/create-something@foo'],
'should npx with scoped packages'
)
},
})
const init = new Init(npm)

process.chdir(npm.localPrefix)
await init.exec(['@npmcli/something@foo'])
})

t.test('npm init git spec', async t => {
t.plan(1)
npm.localPrefix = t.testdir({})
Expand Down

0 comments on commit 0e3660e

Please sign in to comment.