1 | #!/usr/bin/env node
|
2 |
|
3 | const cli = require('../lib/cli.js')
|
4 |
|
5 |
|
6 | process.argv[1] = require.resolve('./npm-cli.js')
|
7 | process.argv.splice(2, 0, 'exec')
|
8 |
|
9 |
|
10 | const removedSwitches = new Set([
|
11 | 'always-spawn',
|
12 | 'ignore-existing',
|
13 | 'shell-auto-fallback',
|
14 | ])
|
15 |
|
16 | const removedOpts = new Set([
|
17 | 'npm',
|
18 | 'node-arg',
|
19 | 'n',
|
20 | ])
|
21 |
|
22 | const removed = new Set([
|
23 | ...removedSwitches,
|
24 | ...removedOpts,
|
25 | ])
|
26 |
|
27 | const { definitions, shorthands } = require('@npmcli/config/lib/definitions')
|
28 | const npmSwitches = Object.entries(definitions)
|
29 | .filter(([, { type }]) => type === Boolean ||
|
30 | (Array.isArray(type) && type.includes(Boolean)))
|
31 | .map(([key]) => key)
|
32 |
|
33 |
|
34 | const switches = new Set([
|
35 | ...removedSwitches,
|
36 | ...npmSwitches,
|
37 | 'no-install',
|
38 | 'quiet',
|
39 | 'q',
|
40 | 'version',
|
41 | 'v',
|
42 | 'help',
|
43 | 'h',
|
44 | ])
|
45 |
|
46 |
|
47 | const opts = new Set([
|
48 | ...removedOpts,
|
49 | 'package',
|
50 | 'p',
|
51 | 'cache',
|
52 | 'userconfig',
|
53 | 'call',
|
54 | 'c',
|
55 | 'shell',
|
56 | 'npm',
|
57 | 'node-arg',
|
58 | 'n',
|
59 | ])
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | let i
|
65 | let sawRemovedFlags = false
|
66 | for (i = 3; i < process.argv.length; i++) {
|
67 | const arg = process.argv[i]
|
68 | if (arg === '--') {
|
69 | break
|
70 | } else if (/^-/.test(arg)) {
|
71 | const [key, ...v] = arg.replace(/^-+/, '').split('=')
|
72 |
|
73 | switch (key) {
|
74 | case 'p':
|
75 | process.argv[i] = ['--package', ...v].join('=')
|
76 | break
|
77 |
|
78 | case 'shell':
|
79 | process.argv[i] = ['--script-shell', ...v].join('=')
|
80 | break
|
81 |
|
82 | case 'no-install':
|
83 | process.argv[i] = '--yes=false'
|
84 | break
|
85 |
|
86 | default:
|
87 |
|
88 | if (shorthands[key] && !removed.has(key)) {
|
89 | const a = [...shorthands[key]]
|
90 | if (v.length) {
|
91 | a.push(v.join('='))
|
92 | }
|
93 | process.argv.splice(i, 1, ...a)
|
94 | i--
|
95 | continue
|
96 | }
|
97 | break
|
98 | }
|
99 |
|
100 | if (removed.has(key)) {
|
101 |
|
102 | console.error(`npx: the --${key} argument has been removed.`)
|
103 | sawRemovedFlags = true
|
104 | process.argv.splice(i, 1)
|
105 | i--
|
106 | }
|
107 |
|
108 | if (v.length === 0 && !switches.has(key) &&
|
109 | (opts.has(key) || !/^-/.test(process.argv[i + 1]))) {
|
110 |
|
111 | if (removed.has(key)) {
|
112 |
|
113 | process.argv.splice(i + 1, 1)
|
114 | } else {
|
115 | i++
|
116 | }
|
117 | }
|
118 | } else {
|
119 |
|
120 | process.argv.splice(i, 0, '--')
|
121 | break
|
122 | }
|
123 | }
|
124 |
|
125 | if (sawRemovedFlags) {
|
126 |
|
127 | console.error('See `npm help exec` for more information')
|
128 | }
|
129 |
|
130 | cli(process)
|