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