bin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env node
  2. const run = conf => {
  3. const pacote = require('../')
  4. switch (conf._[0]) {
  5. case 'resolve':
  6. if (conf.long)
  7. return pacote.manifest(conf._[1], conf).then(mani => ({
  8. resolved: mani._resolved,
  9. integrity: mani._integrity,
  10. from: mani._from,
  11. }))
  12. case 'manifest':
  13. case 'packument':
  14. return pacote[conf._[0]](conf._[1], conf)
  15. case 'tarball':
  16. if (!conf._[2] || conf._[2] === '-') {
  17. return pacote.tarball.stream(conf._[1], stream => {
  18. stream.pipe(conf.testStdout ||
  19. /* istanbul ignore next */ process.stdout)
  20. // make sure it resolves something falsey
  21. return stream.promise().then(() => {})
  22. }, conf)
  23. } else
  24. return pacote.tarball.file(conf._[1], conf._[2], conf)
  25. case 'extract':
  26. return pacote.extract(conf._[1], conf._[2], conf)
  27. default: /* istanbul ignore next */ {
  28. throw new Error(`bad command: ${conf._[0]}`)
  29. }
  30. }
  31. }
  32. const version = require('../package.json').version
  33. const usage = () =>
  34. `Pacote - The JavaScript Package Handler, v${version}
  35. Usage:
  36. pacote resolve <spec>
  37. Resolve a specifier and output the fully resolved target
  38. Returns integrity and from if '--long' flag is set.
  39. pacote manifest <spec>
  40. Fetch a manifest and print to stdout
  41. pacote packument <spec>
  42. Fetch a full packument and print to stdout
  43. pacote tarball <spec> [<filename>]
  44. Fetch a package tarball and save to <filename>
  45. If <filename> is missing or '-', the tarball will be streamed to stdout.
  46. pacote extract <spec> <folder>
  47. Extract a package to the destination folder.
  48. Configuration values all match the names of configs passed to npm, or
  49. options passed to Pacote. Additional flags for this executable:
  50. --long Print an object from 'resolve', including integrity and spec.
  51. --json Print result objects as JSON rather than node's default.
  52. (This is the default if stdout is not a TTY.)
  53. --help -h Print this helpful text.
  54. For example '--cache=/path/to/folder' will use that folder as the cache.
  55. `
  56. const shouldJSON = (conf, result) =>
  57. conf.json ||
  58. !process.stdout.isTTY &&
  59. conf.json === undefined &&
  60. result &&
  61. typeof result === 'object'
  62. const pretty = (conf, result) =>
  63. shouldJSON(conf, result) ? JSON.stringify(result, 0, 2) : result
  64. let addedLogListener = false
  65. const main = args => {
  66. const conf = parse(args)
  67. if (conf.help || conf.h)
  68. return console.log(usage())
  69. if (!addedLogListener) {
  70. process.on('log', console.error)
  71. addedLogListener = true
  72. }
  73. try {
  74. return run(conf)
  75. .then(result => result && console.log(pretty(conf, result)))
  76. .catch(er => {
  77. console.error(er)
  78. process.exit(1)
  79. })
  80. } catch (er) {
  81. console.error(er.message)
  82. console.error(usage())
  83. }
  84. }
  85. const parseArg = arg => {
  86. const split = arg.slice(2).split('=')
  87. const k = split.shift()
  88. const v = split.join('=')
  89. const no = /^no-/.test(k) && !v
  90. const key = (no ? k.substr(3) : k)
  91. .replace(/^tag$/, 'defaultTag')
  92. .replace(/-([a-z])/g, (_, c) => c.toUpperCase())
  93. const value = v ? v.replace(/^~/, process.env.HOME) : !no
  94. return { key, value }
  95. }
  96. const parse = args => {
  97. const conf = {
  98. _: [],
  99. cache: process.env.HOME + '/.npm/_cacache',
  100. }
  101. let dashdash = false
  102. args.forEach(arg => {
  103. if (dashdash)
  104. conf._.push(arg)
  105. else if (arg === '--')
  106. dashdash = true
  107. else if (arg === '-h')
  108. conf.help = true
  109. else if (/^--/.test(arg)) {
  110. const {key, value} = parseArg(arg)
  111. conf[key] = value
  112. } else {
  113. conf._.push(arg)
  114. }
  115. })
  116. return conf
  117. }
  118. if (module === require.main)
  119. main(process.argv.slice(2))
  120. else
  121. module.exports = {
  122. main,
  123. run,
  124. usage,
  125. parseArg,
  126. parse,
  127. }