init.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // initialize a package.json file
  2. module.exports = init
  3. var path = require('path')
  4. var log = require('npmlog')
  5. var npa = require('npm-package-arg')
  6. var npm = require('./npm.js')
  7. var npx = require('libnpx')
  8. var initJson = require('init-package-json')
  9. var isRegistry = require('./utils/is-registry.js')
  10. var output = require('./utils/output.js')
  11. var noProgressTillDone = require('./utils/no-progress-while-running').tillDone
  12. var usage = require('./utils/usage')
  13. init.usage = usage(
  14. 'init',
  15. '\nnpm init [--force|-f|--yes|-y|--scope]' +
  16. '\nnpm init <@scope> (same as `npx <@scope>/create`)' +
  17. '\nnpm init [<@scope>/]<name> (same as `npx [<@scope>/]create-<name>`)'
  18. )
  19. function init (args, cb) {
  20. if (args.length) {
  21. var NPM_PATH = path.resolve(__dirname, '../bin/npm-cli.js')
  22. var initerName = args[0]
  23. var packageName = initerName
  24. if (/^@[^/]+$/.test(initerName)) {
  25. packageName = initerName + '/create'
  26. } else {
  27. var req = npa(initerName)
  28. if (req.type === 'git' && req.hosted) {
  29. var { user, project } = req.hosted
  30. packageName = initerName
  31. .replace(user + '/' + project, user + '/create-' + project)
  32. } else if (isRegistry(req)) {
  33. packageName = req.name.replace(/^(@[^/]+\/)?/, '$1create-')
  34. if (req.rawSpec) {
  35. packageName += '@' + req.rawSpec
  36. }
  37. } else {
  38. var err = new Error(
  39. 'Unrecognized initializer: ' + initerName +
  40. '\nFor more package binary executing power check out `npx`:' +
  41. '\nhttps://www.npmjs.com/package/npx'
  42. )
  43. err.code = 'EUNSUPPORTED'
  44. throw err
  45. }
  46. }
  47. var npxArgs = [process.argv0, '[fake arg]', '--always-spawn', packageName, ...process.argv.slice(4)]
  48. var parsed = npx.parseArgs(npxArgs, NPM_PATH)
  49. return npx(parsed)
  50. .then(() => cb())
  51. .catch(cb)
  52. }
  53. var dir = process.cwd()
  54. log.pause()
  55. var initFile = npm.config.get('init-module')
  56. if (!initJson.yes(npm.config)) {
  57. output([
  58. 'This utility will walk you through creating a package.json file.',
  59. 'It only covers the most common items, and tries to guess sensible defaults.',
  60. '',
  61. 'See `npm help init` for definitive documentation on these fields',
  62. 'and exactly what they do.',
  63. '',
  64. 'Use `npm install <pkg>` afterwards to install a package and',
  65. 'save it as a dependency in the package.json file.',
  66. '',
  67. 'Press ^C at any time to quit.'
  68. ].join('\n'))
  69. }
  70. initJson(dir, initFile, npm.config, noProgressTillDone(function (er, data) {
  71. log.resume()
  72. log.silly('package data', data)
  73. if (er && er.message === 'canceled') {
  74. log.warn('init', 'canceled')
  75. return cb(null, data)
  76. }
  77. log.info('init', 'written successfully')
  78. cb(er, data)
  79. }))
  80. }