unwrap-npm-cmd 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/lib/index.js +59 -59
- package/lib/resolve-npm-cmd.js +53 -47
- package/lib/utils.js +12 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,17 +10,17 @@ console.log(unwrapNpmCmd("npm test"));
|
|
|
10
10
|
console.log(unwrapNpmCmd("npx mocha", { relative: true }));
|
|
11
11
|
console.log(unwrapNpmCmd("mocha test", { jsOnly: true }));
|
|
12
12
|
console.log(unwrapNpmCmd(`find "name" package.json`));
|
|
13
|
-
console.log(unwrapNpmCmd("hello world", {
|
|
13
|
+
console.log(unwrapNpmCmd("hello world", { path: __dirname }));
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
Output:
|
|
17
17
|
|
|
18
18
|
```cmd
|
|
19
19
|
"C:\Users\userid\nvm\nodejs\bin\node.exe" "C:\Users\userid\nvm\nodejs\bin\node_modules\npm\bin\npm-cli.js" test
|
|
20
|
-
"C:\Users\userid\nvm\nodejs\bin\node.exe" ..\nvm\nodejs\bin\node_modules\npm\bin\npx-cli.js mocha
|
|
20
|
+
"C:\Users\userid\nvm\nodejs\bin\node.exe" "..\nvm\nodejs\bin\node_modules\npm\bin\npx-cli.js" mocha
|
|
21
21
|
"C:\Users\userid\unwrap-npm-cmd\node_modules\mocha\bin\mocha" test
|
|
22
22
|
"C:\WINDOWS\system32\find.EXE" "name" package.json
|
|
23
|
-
"
|
|
23
|
+
"C:\Users\userid\unwrap-npm-cmd\test\fixtures\hello.CMD" world
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
## Usage
|
package/lib/index.js
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const resolveNmpCmd = require("./resolve-npm-cmd");
|
|
4
|
-
const { quote, relative } = require("./utils");
|
|
5
|
-
|
|
6
|
-
const RESOLVE_CACHE = {};
|
|
7
|
-
|
|
8
|
-
function unwrapExe(exe, options) {
|
|
9
|
-
let pathCache = RESOLVE_CACHE[options.path];
|
|
10
|
-
|
|
11
|
-
if (!pathCache) {
|
|
12
|
-
pathCache = RESOLVE_CACHE[options.path] = {};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
let newExe;
|
|
16
|
-
|
|
17
|
-
if (pathCache[exe]) {
|
|
18
|
-
newExe = pathCache[exe];
|
|
19
|
-
} else {
|
|
20
|
-
try {
|
|
21
|
-
newExe = resolveNmpCmd(exe, options);
|
|
22
|
-
pathCache[exe] = newExe;
|
|
23
|
-
} catch (err) {
|
|
24
|
-
pathCache[exe] = exe;
|
|
25
|
-
return exe;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (typeof newExe === "string") {
|
|
30
|
-
return newExe;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let { jsFile } = newExe;
|
|
34
|
-
|
|
35
|
-
if (options && options.relative) {
|
|
36
|
-
jsFile = relative(jsFile, options.cwd);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (options && options.jsOnly) {
|
|
40
|
-
return quote(jsFile);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return [quote(process.execPath), quote(jsFile)].join(" ");
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
module.exports = function(cmd, options = { path: process.env.PATH }) {
|
|
47
|
-
/* istanbul ignore next */
|
|
48
|
-
if (process.platform !== "win32") {
|
|
49
|
-
return cmd;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const cmdParts = cmd.split(" ");
|
|
53
|
-
const exe = unwrapExe(cmdParts[0], options);
|
|
54
|
-
if (exe !== cmdParts[0]) {
|
|
55
|
-
return [exe].concat(cmdParts.slice(1)).join(" ");
|
|
56
|
-
} else {
|
|
57
|
-
return cmd;
|
|
58
|
-
}
|
|
59
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const resolveNmpCmd = require("./resolve-npm-cmd");
|
|
4
|
+
const { quote, relative } = require("./utils");
|
|
5
|
+
|
|
6
|
+
const RESOLVE_CACHE = {};
|
|
7
|
+
|
|
8
|
+
function unwrapExe(exe, options) {
|
|
9
|
+
let pathCache = RESOLVE_CACHE[options.path];
|
|
10
|
+
|
|
11
|
+
if (!pathCache) {
|
|
12
|
+
pathCache = RESOLVE_CACHE[options.path] = {};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let newExe;
|
|
16
|
+
|
|
17
|
+
if (pathCache[exe]) {
|
|
18
|
+
newExe = pathCache[exe];
|
|
19
|
+
} else {
|
|
20
|
+
try {
|
|
21
|
+
newExe = resolveNmpCmd(exe, options);
|
|
22
|
+
pathCache[exe] = newExe;
|
|
23
|
+
} catch (err) {
|
|
24
|
+
pathCache[exe] = exe;
|
|
25
|
+
return exe;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (typeof newExe === "string") {
|
|
30
|
+
return newExe;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let { jsFile } = newExe;
|
|
34
|
+
|
|
35
|
+
if (options && options.relative) {
|
|
36
|
+
jsFile = relative(jsFile, options.cwd);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (options && options.jsOnly) {
|
|
40
|
+
return quote(jsFile);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return [quote(process.execPath), quote(jsFile)].join(" ");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = function (cmd, options = { path: process.env.PATH }) {
|
|
47
|
+
/* istanbul ignore next */
|
|
48
|
+
if (process.platform !== "win32") {
|
|
49
|
+
return cmd;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const cmdParts = cmd.split(" ");
|
|
53
|
+
const exe = unwrapExe(cmdParts[0], options);
|
|
54
|
+
if (exe !== cmdParts[0]) {
|
|
55
|
+
return [exe].concat(cmdParts.slice(1)).join(" ");
|
|
56
|
+
} else {
|
|
57
|
+
return cmd;
|
|
58
|
+
}
|
|
59
|
+
};
|
package/lib/resolve-npm-cmd.js
CHANGED
|
@@ -1,47 +1,53 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const Fs = require("fs");
|
|
4
|
-
const which = require("which");
|
|
5
|
-
const Path = require("path");
|
|
6
|
-
|
|
7
|
-
const { quote } = require("./utils");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
.
|
|
21
|
-
.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const Fs = require("fs");
|
|
4
|
+
const which = require("which");
|
|
5
|
+
const Path = require("path");
|
|
6
|
+
|
|
7
|
+
const { quote, unquote } = require("./utils");
|
|
8
|
+
const nodeJsVer = parseInt(process.versions.node.split(".")[0]);
|
|
9
|
+
|
|
10
|
+
module.exports = function resolveNpmCmd(exe, options) {
|
|
11
|
+
// look for the windows CMD batch npm generates for JS
|
|
12
|
+
const resolvedExe = which.sync(exe, options);
|
|
13
|
+
if (Path.extname(resolvedExe).toLowerCase() !== ".cmd") {
|
|
14
|
+
// since we already did the work to find it, use found full path
|
|
15
|
+
return quote(resolvedExe);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// read the batch and find the node.exe execution line
|
|
19
|
+
const script = Fs.readFileSync(resolvedExe)
|
|
20
|
+
.toString()
|
|
21
|
+
.split("\n")
|
|
22
|
+
.map((x) => x.trim());
|
|
23
|
+
|
|
24
|
+
const binName = Path.basename(resolvedExe).toLowerCase();
|
|
25
|
+
const resolvedDir = Path.dirname(resolvedExe);
|
|
26
|
+
// handle npm
|
|
27
|
+
let nodeCmd;
|
|
28
|
+
if (binName === "npm.cmd") {
|
|
29
|
+
nodeCmd = script.find((l) => l.startsWith(`SET "NPM_CLI_JS=`)).replace(/NPM_CLI_JS=/, "");
|
|
30
|
+
} else if (binName === "npx.cmd") {
|
|
31
|
+
nodeCmd = script.find((l) => l.startsWith(`SET "NPX_CLI_JS=`)).replace(/NPX_CLI_JS=/, "");
|
|
32
|
+
} else {
|
|
33
|
+
nodeCmd =
|
|
34
|
+
script.find((l) => l.startsWith(`"%~dp0\\node.exe"`)) ||
|
|
35
|
+
script.find((l) => l.startsWith(`"%_prog%"`));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!nodeCmd) {
|
|
39
|
+
return quote(resolvedExe);
|
|
40
|
+
}
|
|
41
|
+
// update JS script from batch file
|
|
42
|
+
const a = nodeCmd.split(" ").filter((x) => x)[1];
|
|
43
|
+
const b = a.replace(`%~dp0`, resolvedDir).replace(`%dp0%`, resolvedDir);
|
|
44
|
+
let jsFile;
|
|
45
|
+
if (nodeJsVer < 18) {
|
|
46
|
+
jsFile = Path.normalize(b);
|
|
47
|
+
} else {
|
|
48
|
+
// starting with version 18, the behavior changed when there're escaping quotes
|
|
49
|
+
jsFile = quote(Path.normalize(unquote(b)));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return { jsFile };
|
|
53
|
+
};
|
package/lib/utils.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const Path = require("path");
|
|
4
|
-
|
|
5
|
-
const unquote = x =>
|
|
6
|
-
const quote = x => (!x.startsWith(`"`) ? `"${x}"` : x);
|
|
7
|
-
const relative = (x, cwd) => {
|
|
8
|
-
const r = Path.relative(cwd || process.cwd(), unquote(x));
|
|
9
|
-
return r.startsWith(".") ? r : `.\\${r}`;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
module.exports = { unquote, quote, relative };
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const Path = require("path");
|
|
4
|
+
|
|
5
|
+
const unquote = (x) => x.trim().replace(/^['"]+|['"]+$/g, "");
|
|
6
|
+
const quote = (x) => (!x.startsWith(`"`) ? `"${x}"` : x);
|
|
7
|
+
const relative = (x, cwd) => {
|
|
8
|
+
const r = Path.relative(cwd || process.cwd(), unquote(x));
|
|
9
|
+
return r.startsWith(".") ? r : `.\\${r}`;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
module.exports = { unquote, quote, relative };
|