init.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. const fs = require('fs')
  2. const { relative, resolve } = require('path')
  3. const mkdirp = require('mkdirp-infer-owner')
  4. const initJson = require('init-package-json')
  5. const npa = require('npm-package-arg')
  6. const rpj = require('read-package-json-fast')
  7. const libexec = require('libnpmexec')
  8. const mapWorkspaces = require('@npmcli/map-workspaces')
  9. const PackageJson = require('@npmcli/package-json')
  10. const getLocationMsg = require('./exec/get-workspace-location-msg.js')
  11. const BaseCommand = require('./base-command.js')
  12. class Init extends BaseCommand {
  13. /* istanbul ignore next - see test/lib/load-all-commands.js */
  14. static get description () {
  15. return 'Create a package.json file'
  16. }
  17. /* istanbul ignore next - see test/lib/load-all-commands.js */
  18. static get params () {
  19. return ['yes', 'force', 'workspace', 'workspaces', 'include-workspace-root']
  20. }
  21. /* istanbul ignore next - see test/lib/load-all-commands.js */
  22. static get name () {
  23. return 'init'
  24. }
  25. /* istanbul ignore next - see test/lib/load-all-commands.js */
  26. static get usage () {
  27. return [
  28. '[--force|-f|--yes|-y|--scope]',
  29. '<@scope> (same as `npx <@scope>/create`)',
  30. '[<@scope>/]<name> (same as `npx [<@scope>/]create-<name>`)',
  31. ]
  32. }
  33. exec (args, cb) {
  34. this.init(args).then(() => cb()).catch(cb)
  35. }
  36. execWorkspaces (args, filters, cb) {
  37. this.initWorkspaces(args, filters).then(() => cb()).catch(cb)
  38. }
  39. async init (args) {
  40. // npm exec style
  41. if (args.length)
  42. return (await this.execCreate({ args, path: process.cwd() }))
  43. // no args, uses classic init-package-json boilerplate
  44. await this.template()
  45. }
  46. async initWorkspaces (args, filters) {
  47. // if the root package is uninitiated, take care of it first
  48. if (this.npm.flatOptions.includeWorkspaceRoot)
  49. await this.init(args)
  50. // reads package.json for the top-level folder first, by doing this we
  51. // ensure the command throw if no package.json is found before trying
  52. // to create a workspace package.json file or its folders
  53. const pkg = await rpj(resolve(this.npm.localPrefix, 'package.json'))
  54. const wPath = filterArg => resolve(this.npm.localPrefix, filterArg)
  55. // npm-exec style, runs in the context of each workspace filter
  56. if (args.length) {
  57. for (const filterArg of filters) {
  58. const path = wPath(filterArg)
  59. await mkdirp(path)
  60. await this.execCreate({ args, path })
  61. await this.setWorkspace({ pkg, workspacePath: path })
  62. }
  63. return
  64. }
  65. // no args, uses classic init-package-json boilerplate
  66. for (const filterArg of filters) {
  67. const path = wPath(filterArg)
  68. await mkdirp(path)
  69. await this.template(path)
  70. await this.setWorkspace({ pkg, workspacePath: path })
  71. }
  72. }
  73. async execCreate ({ args, path }) {
  74. const [initerName, ...otherArgs] = args
  75. let packageName = initerName
  76. if (/^@[^/]+$/.test(initerName))
  77. packageName = initerName + '/create'
  78. else {
  79. const req = npa(initerName)
  80. if (req.type === 'git' && req.hosted) {
  81. const { user, project } = req.hosted
  82. packageName = initerName
  83. .replace(user + '/' + project, user + '/create-' + project)
  84. } else if (req.registry) {
  85. packageName = req.name.replace(/^(@[^/]+\/)?/, '$1create-')
  86. if (req.rawSpec)
  87. packageName += '@' + req.rawSpec
  88. } else {
  89. throw Object.assign(new Error(
  90. 'Unrecognized initializer: ' + initerName +
  91. '\nFor more package binary executing power check out `npx`:' +
  92. '\nhttps://www.npmjs.com/package/npx'
  93. ), { code: 'EUNSUPPORTED' })
  94. }
  95. }
  96. const newArgs = [packageName, ...otherArgs]
  97. const { color } = this.npm.flatOptions
  98. const {
  99. flatOptions,
  100. localBin,
  101. log,
  102. globalBin,
  103. } = this.npm
  104. // this function is definitely called. But because of coverage map stuff
  105. // it ends up both uncovered, and the coverage report doesn't even mention.
  106. // the tests do assert that some output happens, so we know this line is
  107. // being hit.
  108. /* istanbul ignore next */
  109. const output = (...outputArgs) => this.npm.output(...outputArgs)
  110. const locationMsg = await getLocationMsg({ color, path })
  111. const runPath = path
  112. const scriptShell = this.npm.config.get('script-shell') || undefined
  113. const yes = this.npm.config.get('yes')
  114. await libexec({
  115. ...flatOptions,
  116. args: newArgs,
  117. color,
  118. localBin,
  119. locationMsg,
  120. log,
  121. globalBin,
  122. output,
  123. path,
  124. runPath,
  125. scriptShell,
  126. yes,
  127. })
  128. }
  129. async template (path = process.cwd()) {
  130. this.npm.log.pause()
  131. this.npm.log.disableProgress()
  132. const initFile = this.npm.config.get('init-module')
  133. if (!this.npm.config.get('yes') && !this.npm.config.get('force')) {
  134. this.npm.output([
  135. 'This utility will walk you through creating a package.json file.',
  136. 'It only covers the most common items, and tries to guess sensible defaults.',
  137. '',
  138. 'See `npm help init` for definitive documentation on these fields',
  139. 'and exactly what they do.',
  140. '',
  141. 'Use `npm install <pkg>` afterwards to install a package and',
  142. 'save it as a dependency in the package.json file.',
  143. '',
  144. 'Press ^C at any time to quit.',
  145. ].join('\n'))
  146. }
  147. // XXX promisify init-package-json
  148. await new Promise((res, rej) => {
  149. initJson(path, initFile, this.npm.config, (er, data) => {
  150. this.npm.log.resume()
  151. this.npm.log.enableProgress()
  152. this.npm.log.silly('package data', data)
  153. if (er && er.message === 'canceled') {
  154. this.npm.log.warn('init', 'canceled')
  155. return res()
  156. }
  157. if (er)
  158. rej(er)
  159. else {
  160. this.npm.log.info('init', 'written successfully')
  161. res(data)
  162. }
  163. })
  164. })
  165. }
  166. async setWorkspace ({ pkg, workspacePath }) {
  167. const workspaces = await mapWorkspaces({ cwd: this.npm.localPrefix, pkg })
  168. // skip setting workspace if current package.json glob already satisfies it
  169. for (const wPath of workspaces.values()) {
  170. if (wPath === workspacePath)
  171. return
  172. }
  173. // if a create-pkg didn't generate a package.json at the workspace
  174. // folder level, it might not be recognized as a workspace by
  175. // mapWorkspaces, so we're just going to avoid touching the
  176. // top-level package.json
  177. try {
  178. fs.statSync(resolve(workspacePath, 'package.json'))
  179. } catch (err) {
  180. return
  181. }
  182. const pkgJson = await PackageJson.load(this.npm.localPrefix)
  183. pkgJson.update({
  184. workspaces: [
  185. ...(pkgJson.content.workspaces || []),
  186. relative(this.npm.localPrefix, workspacePath),
  187. ],
  188. })
  189. await pkgJson.save()
  190. }
  191. }
  192. module.exports = Init