Debian Codegraph @

spawn-wrap/lib/mungers/cmd.js

raw
1 'use strict';
2
3 const path = require("path")
4 const whichOrUndefined = require("../which-or-undefined")
5
6 /**
7 * Intercepts Node and npm processes spawned through Windows' `cmd.exe`.
8 *
9 * @param workingDir {string} Absolute system-dependent path to the directory containing the shim files.
10 * @param options {import("../munge").InternalSpawnOptions} Original internal spawn options.
11 * @return {import("../munge").InternalSpawnOptions} Updated internal spawn options.
12 */
13 function mungeCmd(workingDir, options) {
14 const cmdi = options.args.indexOf('/c')
15 if (cmdi === -1) {
16 return {...options}
17 }
18
19 const re = /^\s*("*)([^"]*?\bnode(?:\.exe|\.EXE)?)("*)( .*)?$/
20 const npmre = /^\s*("*)([^"]*?\b(?:npm))("*)( |$)/
21
22 const command = options.args[cmdi + 1]
23 if (command === undefined) {
24 return {...options}
25 }
26
27 let newArgs = [...options.args];
28 // Remember the original Node command to use it in the shim
29 let originalNode;
30
31 let m = command.match(re)
32 let replace
33 if (m) {
34 originalNode = m[2]
35 // TODO: Remove `replace`: seems unused
36 replace = m[1] + path.join(workingDir, 'node.cmd') + m[3] + m[4]
37 newArgs[cmdi + 1] = m[1] + m[2] + m[3] +
38 ' "' + path.join(workingDir, 'node') + '"' + m[4]
39 } else {
40 // XXX probably not a good idea to rewrite to the first npm in the
41 // path if it's a full path to npm. And if it's not a full path to
42 // npm, then the dirname will not work properly!
43 m = command.match(npmre)
44 if (m === null) {
45 return {...options}
46 }
47
48 let npmPath = whichOrUndefined('npm') || 'npm'
49 npmPath = path.join(path.dirname(npmPath), 'node_modules', 'npm', 'bin', 'npm-cli.js')
50 replace = m[1] + '"' + path.join(workingDir, 'node.cmd') + '"' +
51 ' "' + npmPath + '"' +
52 m[3] + m[4]
53 newArgs[cmdi + 1] = command.replace(npmre, replace)
54 }
55
56 return {...options, args: newArgs, originalNode};
57 }
58
59 module.exports = mungeCmd