Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

feat: add npm init <create-pkg-name> command #20303

Merged
merged 1 commit into from Apr 18, 2018
Merged

feat: add npm init <create-pkg-name> command #20303

merged 1 commit into from Apr 18, 2018

Conversation

jdalton
Copy link
Contributor

@jdalton jdalton commented Apr 11, 2018

The npm init <create-pkg-name> command will

  • detect the argument after npm init
  • append the appropriate prefix to it
    (e.g. npm init esm will translate the esm arg to create-esm)
  • invoke the equiv of npx create-esm via libnpx

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 on bin/npx-cli.js. The first arg beyond npm init __ is used as the initer to resolve. Additional arguments are forwarded to the initer.

I used args passed to init() as the initial feature gate because it was provided info. The args array lacks extra flags though. So, for say npm init esm -y the args array to init() is ['esm']. This is why process.argv is used to forward additional args.

Erroring is handled by npx. So for invalid packages the result looks like

npm init b -y
npm ERR! code E404
npm ERR! 404 Not Found: create-b@latest

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/jdalton/.npm/_logs/2018-04-11T23_57_32_908Z-debug.log
Install for create-b@latest failed with code 1

Update:

Added create as an alias of init and added innit as an affordance of init.

@jdalton jdalton requested a review from a team as a code owner April 11, 2018 06:39
@jdalton jdalton changed the title Add support for npm init <create-pkg-name>. feat: add npm init <create-pkg-name> Apr 11, 2018
@jdalton jdalton changed the title feat: add npm init <create-pkg-name> feat: add npm init <create-pkg-name> command Apr 11, 2018
lib/init.js Outdated
.then(cb)
.catch(cb)
}

Copy link
Contributor Author

@jdalton jdalton Apr 11, 2018

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-')

Copy link
Contributor Author

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.

@jdalton
Copy link
Contributor Author

jdalton commented Apr 12, 2018

Any guidance on unit tests would be appreciated.

var packageName = /^@[^/]+$/.test(initerName)
? initerName + '/create'
: initerName.replace(/^(@[^/]+\/)?/, '$1create-')

Copy link
Contributor Author

@jdalton jdalton Apr 12, 2018

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.

@zkat
Copy link
Contributor

zkat commented Apr 12, 2018

@jdalton as far as unit tests go, I would mock bin/npx-cli.js and just make sure it's being called with the right arguments.

@jdalton
Copy link
Contributor Author

jdalton commented Apr 12, 2018

@zkat

as far as unit tests go, I would mock bin/npx-cli.js and just make sure it's being called with the right arguments.

Cool!

@jdalton
Copy link
Contributor Author

jdalton commented Apr 12, 2018

Note: This PR depends on zkat/npx#172 (however it is resolved).

The npx issue is resolved 😋

@iarna iarna force-pushed the release-next branch 3 times, most recently from 2e76172 to fe31e66 Compare April 13, 2018 20:20
@jdalton
Copy link
Contributor Author

jdalton commented Apr 18, 2018

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]'
Copy link
Contributor

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",
Copy link
Contributor

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

@jdalton
Copy link
Contributor Author

jdalton commented Apr 18, 2018

Added docs and removed the libnpx bump in the package.json.

@zkat zkat merged commit 008a836 into npm:release-next Apr 18, 2018
@jdalton jdalton deleted the init branch April 18, 2018 21:05
@jdalton
Copy link
Contributor Author

jdalton commented Apr 18, 2018

Thank you 🎉

@@ -4,6 +4,7 @@ var shorthands = {
'rb': 'rebuild',
'list': 'ls',
'ln': 'link',
'create': 'init',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this alias 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks goes to @zkat 👏

iarna pushed a commit that referenced this pull request May 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants