moving
Some checks failed
Installer Smoke / installer-smoke (push) Has been cancelled

This commit is contained in:
Oleg Maslov
2026-09-02 10:10:29 +02:00
commit 0c3e2ead3b
3841 changed files with 970576 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
import { existsSync, readFileSync } from 'node:fs';
import { win32 as pathWin32 } from 'node:path';
export interface ToolCommandInvocation {
binary: string;
args: string[];
windowsVerbatimArguments?: boolean;
}
export interface ToolCommandResolutionDeps {
readTextFile?: (path: string) => string | null;
fileExists?: (path: string) => boolean;
nodeBinary?: string;
}
function defaultReadTextFile(path: string): string | null {
try {
return readFileSync(path, 'utf8');
} catch {
return null;
}
}
function defaultFileExists(path: string): boolean {
return existsSync(path);
}
function npmCmdShimTarget(content: string): string | null {
const match = content.match(/"%_prog%"\s+"%dp0%[\\/]+([^"]+)"/i);
return match?.[1] ?? null;
}
function quoteCmdArg(value: string): string {
return `"${value.replace(/"/g, '""')}"`;
}
export function resolveToolCommandInvocation(
binary: string,
args: string[],
platform: NodeJS.Platform = process.platform,
deps: ToolCommandResolutionDeps = {},
): ToolCommandInvocation {
if (platform === 'win32' && /\.(?:cmd|bat)$/i.test(binary)) {
const readTextFile = deps.readTextFile ?? defaultReadTextFile;
const fileExists = deps.fileExists ?? defaultFileExists;
const content = readTextFile(binary);
const shimTarget = content ? npmCmdShimTarget(content) : null;
if (shimTarget) {
const shimDir = pathWin32.dirname(binary);
const localNode = pathWin32.join(shimDir, 'node.exe');
return {
binary: fileExists(localNode) ? localNode : (deps.nodeBinary ?? 'node'),
args: [pathWin32.join(shimDir, shimTarget), ...args],
};
}
return {
binary: 'cmd.exe',
args: [
'/d',
'/v:off',
'/s',
'/c',
['call', quoteCmdArg(binary), ...args.map(quoteCmdArg)].join(' '),
],
windowsVerbatimArguments: true,
};
}
return { binary, args };
}