feat: add npm init <create-pkg-name> command #20303
Conversation
npm init <create-pkg-name>
.
lib/init.js
Outdated
.then(cb) | ||
.catch(cb) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The promise returned by npx
is a vanilla promise (not a bluebird one) so lacks the nodeify
helper.
The promise then(cb)
of npx
is executed before the npm init
command finishes which causes the process to exit. There is a PR open on npx
(zkat/npx#172) to return a promise so the cb
will be executed after npx
runs its command.
lib/init.js
Outdated
var NPM_PATH = path.resolve(__dirname, '../bin/npm-cli.js') | ||
var initerName = args[0] | ||
var packageName = initerName.replace(/^(@[^/]+\/)?/, '$1create-') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
☝️ That regexp is the key yarn/npm interoperable bit.
Any guidance on unit tests would be appreciated. |
var packageName = /^@[^/]+$/.test(initerName) | ||
? initerName + '/create' | ||
: initerName.replace(/^(@[^/]+\/)?/, '$1create-') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👆 In addition to following yarn's rules,
name expansion also supports transforming @scope
into @scope/create
.
@jdalton as far as unit tests go, I would mock |
Cool! |
The |
2e76172
to
fe31e66
Compare
Added tests! |
lib/init.js
Outdated
var log = require('npmlog') | ||
var npm = require('./npm.js') | ||
var npx = require('libnpx') | ||
var initJson = require('init-package-json') | ||
var output = require('./utils/output.js') | ||
var noProgressTillDone = require('./utils/no-progress-while-running').tillDone | ||
|
||
init.usage = 'npm init [--force|-f|--yes|-y]' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be updated. You should document this feature in doc/cli/npm-init.md
as well.
package.json
Outdated
@@ -74,7 +74,7 @@ | |||
"lazy-property": "~1.0.0", | |||
"libcipm": "^1.6.2", | |||
"libnpmhook": "^4.0.1", | |||
"libnpx": "^10.1.1", | |||
"libnpx": "^10.2.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed -- I've updated libnpx
Added docs and removed the |
Thank you 🎉 |
@@ -4,6 +4,7 @@ var shorthands = { | |||
'rb': 'rebuild', | |||
'list': 'ls', | |||
'ln': 'link', | |||
'create': 'init', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this alias 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks goes to @zkat 👏
The
npm init <create-pkg-name>
command willnpm init
(e.g.
npm init esm
will translate theesm
arg tocreate-esm
)npx create-esm
vialibnpx
Related to zkat/npx#167.
Update:
Okay!
Basic functionality exists in the PR now (thanks to @zkat for getting me unstuck).
At the moment I defer to
npx
for parsing args and handling installs, which means it follows its rules on fresh installing packages and all that.npx
does all the heavy lifting here so this tie-in is pretty minimal.I based
npx
invocation onbin/npx-cli.js
. The first arg beyondnpm init __
is used as the initer to resolve. Additional arguments are forwarded to the initer.I used
args
passed toinit()
as the initial feature gate because it was provided info. Theargs
array lacks extra flags though. So, for saynpm init esm -y
theargs
array toinit()
is['esm']
. This is whyprocess.argv
is used to forward additional args.Erroring is handled by
npx
. So for invalid packages the result looks likeUpdate:
Added
create
as an alias ofinit
and addedinnit
as an affordance ofinit
.