(
argv: readonly string[],
opts: { platform?: NodeJS.Platform; env?: NodeJS.ProcessEnv } = {},
)
| 44 | const DEFAULT_PATHEXT = '.COM;.EXE;.BAT;.CMD'; |
| 45 | |
| 46 | export function resolveLaunchCommand( |
| 47 | argv: readonly string[], |
| 48 | opts: { platform?: NodeJS.Platform; env?: NodeJS.ProcessEnv } = {}, |
| 49 | ): ResolvedCommand { |
| 50 | const platform = opts.platform ?? process.platform; |
| 51 | const env = opts.env ?? process.env; |
| 52 | if (platform !== 'win32' || argv.length === 0) return { argv, viaShell: false }; |
| 53 | |
| 54 | const [name, ...rest] = argv; |
| 55 | if (!name) return { argv, viaShell: false }; |
| 56 | // Caller gave an explicit path or extension → trust it, don't re-resolve. |
| 57 | if (name.includes('/') || name.includes('\\') || /\.[^.\\/]+$/.test(name)) { |
| 58 | return { argv, viaShell: false }; |
| 59 | } |
| 60 | |
| 61 | const resolved = lookupOnWindowsPath(name, env); |
| 62 | if (!resolved) return { argv, viaShell: false }; // fail loudly with original name |
| 63 | |
| 64 | const dot = resolved.lastIndexOf('.'); |
| 65 | const ext = dot >= 0 ? resolved.slice(dot).toLowerCase() : ''; |
| 66 | if (ext === '.cmd' || ext === '.bat') { |
| 67 | const comspec = env['ComSpec'] || env['COMSPEC'] || 'cmd.exe'; |
| 68 | // /d skips any AutoRun registry command; /c runs then exits. The shim path |
| 69 | // is a single arg (node-pty/Node quote it if it contains spaces); cmd's |
| 70 | // default rule preserves a single quoted-executable + bare args correctly. |
| 71 | return { argv: [comspec, '/d', '/c', resolved, ...rest], viaShell: true }; |
| 72 | } |
| 73 | return { argv: [resolved, ...rest], viaShell: false }; |
| 74 | } |
| 75 | |
| 76 | function lookupOnWindowsPath(name: string, env: NodeJS.ProcessEnv): string | null { |
| 77 | const exts = (env['PATHEXT'] ?? DEFAULT_PATHEXT) |
no test coverage detected