@@ -6,25 +6,123 @@ const cli = require('../lib/cli.js')
66process . argv [ 1 ] = require . resolve ( './npm-cli.js' )
77process . argv . splice ( 2 , 0 , 'exec' )
88
9+ // TODO: remove the affordances for removed items in npm v9
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 { types, shorthands } = require ( '../lib/config/defaults.js' )
28+ const npmSwitches = Object . entries ( types )
29+ . filter ( ( [ key , type ] ) => type === Boolean ||
30+ ( Array . isArray ( type ) && type . includes ( Boolean ) ) )
31+ . map ( ( [ key , type ] ) => key )
32+
33+ // things that don't take a value
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+ // things that do take a value
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+
961// break out of loop when we find a positional argument or --
1062// If we find a positional arg, we shove -- in front of it, and
1163// let the normal npm cli handle the rest.
1264let i
65+ let sawRemovedFlags = false
1366for ( i = 3 ; i < process . argv . length ; i ++ ) {
1467 const arg = process . argv [ i ]
1568 if ( arg === '--' ) {
1669 break
1770 } else if ( / ^ - / . test ( arg ) ) {
18- // `--package foo` treated the same as `--package=foo`
19- if ( ! arg . includes ( '=' ) ) {
20- i ++
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+ // resolve shorthands and run again
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+ console . error ( `npx: the --${ key } argument has been removed.` )
102+ sawRemovedFlags = true
103+ process . argv . splice ( i , 1 )
104+ i --
105+ }
106+
107+ if ( v . length === 0 && ! switches . has ( key ) &&
108+ ( opts . has ( key ) || ! / ^ - / . test ( process . argv [ i + 1 ] ) ) ) {
109+ // value will be next argument, skip over it.
110+ if ( removed . has ( key ) ) {
111+ // also remove the value for the cut key.
112+ process . argv . splice ( i + 1 , 1 )
113+ } else {
114+ i ++
115+ }
21116 }
22- continue
23117 } else {
24118 // found a positional arg, put -- in front of it, and we're done
25119 process . argv . splice ( i , 0 , '--' )
26120 break
27121 }
28122}
29123
124+ if ( sawRemovedFlags ) {
125+ console . error ( 'See `npm help exec` for more information' )
126+ }
127+
30128cli ( process )
0 commit comments