source: trip-planner-front/node_modules/pacote/lib/bin.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.7 KB
Line 
1#!/usr/bin/env node
2
3const 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
37const version = require('../package.json').version
38const usage = () =>
39`Pacote - The JavaScript Package Handler, v${version}
40
41Usage:
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
60Configuration values all match the names of configs passed to npm, or
61options 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
68For example '--cache=/path/to/folder' will use that folder as the cache.
69`
70
71const shouldJSON = (conf, result) =>
72 conf.json ||
73 !process.stdout.isTTY &&
74 conf.json === undefined &&
75 result &&
76 typeof result === 'object'
77
78const pretty = (conf, result) =>
79 shouldJSON(conf, result) ? JSON.stringify(result, 0, 2) : result
80
81let addedLogListener = false
82const 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
105const 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
117const 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
140if (module === require.main)
141 main(process.argv.slice(2))
142else
143 module.exports = {
144 main,
145 run,
146 usage,
147 parseArg,
148 parse,
149 }
Note: See TracBrowser for help on using the repository browser.