@commonlyai/cli 0.1.252 → 0.1.253
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/package.json +1 -1
- package/src/lib/windows-command.js +63 -25
package/package.json
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
// runs as itself; anything else is refused with a reason, never handed to a
|
|
17
17
|
// shell. On every other platform this module is a passthrough.
|
|
18
18
|
|
|
19
|
+
import { spawnSync as realSpawnSync } from 'child_process';
|
|
19
20
|
import { existsSync as realExistsSync, readFileSync as realReadFileSync } from 'fs';
|
|
20
21
|
import { win32 } from 'path';
|
|
21
22
|
|
|
@@ -113,44 +114,79 @@ const programIsNode = (token, shimDir, text) => {
|
|
|
113
114
|
return false;
|
|
114
115
|
};
|
|
115
116
|
|
|
116
|
-
const envValue = (env, name) => {
|
|
117
|
-
const key = Object.keys(env || {}).find((k) => k.toLowerCase() === name.toLowerCase());
|
|
118
|
-
return key ? env[key] : undefined;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
117
|
// npm's own `npm.cmd` / `npx.cmd` (npm/bin/*.cmd, which the Node installer
|
|
122
118
|
// copies next to node.exe) is hand-written, not a cmd-shim:
|
|
123
119
|
//
|
|
124
120
|
// SET "NODE_EXE=%~dp0\node.exe" (else `node`)
|
|
121
|
+
// SET "NPM_PREFIX_JS=%~dp0\node_modules\npm\bin\npm-prefix.js"
|
|
125
122
|
// SET "NPX_CLI_JS=%~dp0\node_modules\npm\bin\npx-cli.js"
|
|
126
123
|
// FOR /F … ('CALL "%NODE_EXE%" "%NPM_PREFIX_JS%"') … SET "NPM_PREFIX_NPX_CLI_JS=%%F\node_modules\npm\bin\npx-cli.js"
|
|
127
124
|
// IF EXIST "%NPM_PREFIX_NPX_CLI_JS%" ( SET "NPX_CLI_JS=%NPM_PREFIX_NPX_CLI_JS%" )
|
|
128
125
|
// "%NODE_EXE%" "%NPX_CLI_JS%" %*
|
|
129
126
|
//
|
|
130
|
-
// The FOR line
|
|
131
|
-
// person upgraded with `npm i -g npm` wins over the
|
|
132
|
-
// is %APPDATA%\npm unless an npmrc
|
|
133
|
-
//
|
|
127
|
+
// The FOR line asks npm for its global prefix (older npm: `npm-cli.js prefix
|
|
128
|
+
// -g`), so an npm the person upgraded with `npm i -g npm` wins over the
|
|
129
|
+
// bundled one. That prefix is %APPDATA%\npm unless an npmrc or
|
|
130
|
+
// `npm_config_prefix` says otherwise, and npm's config loader is the only
|
|
131
|
+
// thing that knows which. So the same script is asked the same way, as
|
|
132
|
+
// `node.exe <script>` with no shell, in the child's env, and in its folder
|
|
133
|
+
// when the caller passes `cwd` (a project .npmrc counts there). An answer that names no npm, or no answer at all,
|
|
134
|
+
// leaves the bundled npm, as npm.cmd does.
|
|
134
135
|
const NPM_LAUNCH = /^"%NODE_EXE%"\s+"%(NP[MX]_CLI_JS)%"\s+%\*$/i;
|
|
136
|
+
const NPM_PREFIX_FOR = /^FOR\s+\/F\s+"delims="\s+%%F\s+IN\s+\('CALL\s+"%NODE_EXE%"\s+"%([A-Z_]+)%"((?:\s+[A-Za-z0-9-]+)*)'\)\s+DO\s+\($/i;
|
|
137
|
+
const PREFIX_TIMEOUT_MS = 10000;
|
|
138
|
+
|
|
139
|
+
const declaredBesideShim = (lines, variable, shimDir) => {
|
|
140
|
+
const declared = lines
|
|
141
|
+
.map((line) => line.match(new RegExp(`^SET\\s+"${variable}=([^"]*)"$`, 'i')))
|
|
142
|
+
.find((match) => match && DP0_PREFIX.test(match[1]));
|
|
143
|
+
return declared ? fromShimDir(declared[1], shimDir) : null;
|
|
144
|
+
};
|
|
135
145
|
|
|
136
|
-
|
|
146
|
+
// The npm script npm.cmd would switch to after asking for the prefix, or
|
|
147
|
+
// null to keep the bundled one.
|
|
148
|
+
const npmFromPrefix = (lines, variable, shimDir, node, deps) => {
|
|
149
|
+
const forLine = lines.map((line) => line.match(NPM_PREFIX_FOR)).find(Boolean);
|
|
150
|
+
if (!forLine) return null;
|
|
151
|
+
const script = declaredBesideShim(lines, forLine[1], shimDir);
|
|
152
|
+
if (!script || !deps.existsSync(script)) return null;
|
|
153
|
+
const target = lines
|
|
154
|
+
.map((line) => line.match(new RegExp(`^SET\\s+"NPM_PREFIX_${variable}=%%F\\\\([^"%]+)"$`, 'i')))
|
|
155
|
+
.find(Boolean);
|
|
156
|
+
if (!target) return null;
|
|
157
|
+
let result;
|
|
158
|
+
try {
|
|
159
|
+
result = deps.spawnSync(node, [script, ...forLine[2].trim().split(/\s+/).filter(Boolean)], {
|
|
160
|
+
env: deps.env,
|
|
161
|
+
cwd: deps.cwd,
|
|
162
|
+
encoding: 'utf8',
|
|
163
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
164
|
+
timeout: PREFIX_TIMEOUT_MS,
|
|
165
|
+
windowsHide: true,
|
|
166
|
+
});
|
|
167
|
+
} catch {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
if (!result || result.status !== 0) return null;
|
|
171
|
+
// FOR /F runs its body once per line, so the last line is the one that sticks.
|
|
172
|
+
const prefix = String(result.stdout || '').split(/\r?\n/).map((line) => line.trim()).filter(Boolean).pop();
|
|
173
|
+
if (!prefix || !win32.isAbsolute(prefix)) return null;
|
|
174
|
+
const upgraded = win32.join(prefix, target[1]);
|
|
175
|
+
return deps.existsSync(upgraded) ? upgraded : null;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const parseNpmLauncher = (lines, shimPath, deps) => {
|
|
137
179
|
const launch = lines.map((line) => line.match(NPM_LAUNCH)).find(Boolean);
|
|
138
180
|
if (!launch) return null;
|
|
139
181
|
const variable = launch[1];
|
|
140
182
|
const shimDir = win32.dirname(shimPath);
|
|
141
|
-
const
|
|
142
|
-
.map((line) => line.match(new RegExp(`^SET\\s+"${variable}=([^"]*)"$`, 'i')))
|
|
143
|
-
.find((match) => match && DP0_PREFIX.test(match[1]));
|
|
144
|
-
const bundled = declared ? fromShimDir(declared[1], shimDir) : null;
|
|
183
|
+
const bundled = declaredBesideShim(lines, variable, shimDir);
|
|
145
184
|
if (!bundled) return { command: null, reason: `${shimPath} does not name npm's script beside itself` };
|
|
146
|
-
const appData = envValue(env, 'APPDATA');
|
|
147
|
-
const upgraded = appData
|
|
148
|
-
? win32.join(appData, 'npm', 'node_modules', 'npm', 'bin', win32.basename(bundled))
|
|
149
|
-
: null;
|
|
150
|
-
const script = upgraded && existsSync(upgraded) ? upgraded : bundled;
|
|
151
|
-
if (!existsSync(script)) return { command: null, reason: `${shimPath} points at ${script}, which does not exist` };
|
|
152
185
|
const nodeBeside = win32.join(shimDir, 'node.exe');
|
|
153
|
-
|
|
186
|
+
const node = deps.existsSync(nodeBeside) ? nodeBeside : deps.execPath;
|
|
187
|
+
const script = npmFromPrefix(lines, variable, shimDir, node, deps) || bundled;
|
|
188
|
+
if (!deps.existsSync(script)) return { command: null, reason: `${shimPath} points at ${script}, which does not exist` };
|
|
189
|
+
return { command: node, args: [script] };
|
|
154
190
|
};
|
|
155
191
|
|
|
156
192
|
/**
|
|
@@ -166,14 +202,14 @@ const parseNpmLauncher = (lines, shimPath, { existsSync, execPath, env }) => {
|
|
|
166
202
|
* @returns {{ command: string, args: string[] } | { command: null, reason: string }}
|
|
167
203
|
*/
|
|
168
204
|
export const parseCmdShim = (text, shimPath, {
|
|
169
|
-
existsSync = realExistsSync, execPath = process.execPath, env = {},
|
|
205
|
+
existsSync = realExistsSync, execPath = process.execPath, env = {}, spawnSync = realSpawnSync, cwd,
|
|
170
206
|
} = {}) => {
|
|
171
207
|
const shimDir = win32.dirname(shimPath);
|
|
172
208
|
const lines = String(text)
|
|
173
209
|
.split(/\r?\n/)
|
|
174
210
|
.map((line) => line.trim().replace(/^@/, ''))
|
|
175
211
|
.filter(Boolean);
|
|
176
|
-
const npmLauncher = parseNpmLauncher(lines, shimPath, { existsSync, execPath, env });
|
|
212
|
+
const npmLauncher = parseNpmLauncher(lines, shimPath, { existsSync, execPath, env, spawnSync, cwd });
|
|
177
213
|
if (npmLauncher) return npmLauncher;
|
|
178
214
|
// The launch line is the one that forwards the caller's arguments. The
|
|
179
215
|
// legacy shape has two (node beside the shim, else node on PATH) that name
|
|
@@ -256,7 +292,8 @@ const classify = (file, deps) => {
|
|
|
256
292
|
* it cannot be. On anything but win32, `{ command: name, args: [] }`.
|
|
257
293
|
* On win32, `source` is the file found on PATH (the shim, or the .exe).
|
|
258
294
|
* `cwd` is the child's working directory; relative PATH entries are read
|
|
259
|
-
* against it (unset: this process's, as before)
|
|
295
|
+
* against it (unset: this process's, as before), and npm's own launcher asks
|
|
296
|
+
* npm for its prefix there.
|
|
260
297
|
*
|
|
261
298
|
* @returns {{ command: string, args: string[], source?: string }
|
|
262
299
|
* | { command: null, reason: string, source?: string }}
|
|
@@ -267,10 +304,11 @@ export const resolveWindowsCommandWithReason = (name, {
|
|
|
267
304
|
existsSync = realExistsSync,
|
|
268
305
|
readFileSync = realReadFileSync,
|
|
269
306
|
execPath = process.execPath,
|
|
307
|
+
spawnSync = realSpawnSync,
|
|
270
308
|
cwd,
|
|
271
309
|
} = {}) => {
|
|
272
310
|
if (platform !== 'win32') return { command: name, args: [] };
|
|
273
|
-
const deps = { existsSync, readFileSync, execPath, env };
|
|
311
|
+
const deps = { existsSync, readFileSync, execPath, env, spawnSync, cwd };
|
|
274
312
|
const exts = pathExtensions(env);
|
|
275
313
|
const hasKnownExt = [...BINARY_EXTS, ...SHIM_EXTS].includes(extOf(name));
|
|
276
314
|
// PATHEXT is upper case by convention and NTFS ignores case, so the
|