|
18 | 18 | import { spawnSync } from 'node:child_process'; |
19 | 19 | import { appendFileSync, copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'; |
20 | 20 | import { homedir, hostname } from 'node:os'; |
21 | | -import { basename, dirname, join } from 'node:path'; |
| 21 | +import { basename, delimiter, dirname, isAbsolute, join } from 'node:path'; |
22 | 22 | import { fileURLToPath } from 'node:url'; |
23 | 23 |
|
24 | 24 | const STAGES = ['refine', 'connect', 'check_state', 'provision', 'content_model', 'design', 'frontend', 'validate', 'deploy', 'done']; |
@@ -106,14 +106,27 @@ const LOCK_FRESH_MS = 30 * 60 * 1000; |
106 | 106 |
|
107 | 107 | const processName = (name) => basename(name.replace(/\\/g, '/')).replace(/^-/, '').replace(/\.exe$/i, '').toLowerCase(); |
108 | 108 |
|
| 109 | +// This script runs in the user's project, maybe a cloned repository. Windows looks a bare program name |
| 110 | +// up in the working directory before PATH, as does a relative PATH entry elsewhere, so the process list |
| 111 | +// is read from home with only absolute, non-node_modules PATH entries (as connect/launch.mjs runs npx), |
| 112 | +// and Windows PowerShell is named by its full path. |
| 113 | +const helperEnv = { ...process.env, LC_ALL: 'C', TZ: 'UTC' }; |
| 114 | +for (const key of Object.keys(helperEnv).filter((k) => k.toUpperCase() === 'PATH')) { |
| 115 | + helperEnv[key] = helperEnv[key].split(delimiter) |
| 116 | + .filter((p) => isAbsolute(p) && !p.split(/[\\/]/).includes('node_modules')) |
| 117 | + .join(delimiter); |
| 118 | +} |
| 119 | +const systemRoot = isAbsolute(process.env.SystemRoot ?? '') ? process.env.SystemRoot : 'C:\\Windows'; |
| 120 | +const POWERSHELL = join(systemRoot, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'); |
| 121 | + |
109 | 122 | // pid -> { ppid, started, name }; null when the process list can't be read. The start time tells a |
110 | 123 | // reused PID from the process that held the lock. |
111 | 124 | function processTable() { |
112 | 125 | const windows = process.platform === 'win32'; |
113 | 126 | const r = windows |
114 | | - ? spawnSync('powershell', ['-NoProfile', '-NonInteractive', '-Command', |
115 | | - 'Get-CimInstance Win32_Process | ForEach-Object { "$($_.ProcessId)`t$($_.ParentProcessId)`t$(if ($_.CreationDate) { $_.CreationDate.ToUniversalTime().ToString(\'o\') })`t$($_.Name)" }'], { encoding: 'utf8' }) |
116 | | - : spawnSync('ps', ['-A', '-o', 'pid=,ppid=,lstart=,comm='], { encoding: 'utf8', env: { ...process.env, LC_ALL: 'C', TZ: 'UTC' } }); |
| 127 | + ? spawnSync(POWERSHELL, ['-NoProfile', '-NonInteractive', '-Command', |
| 128 | + 'Get-CimInstance Win32_Process | ForEach-Object { "$($_.ProcessId)`t$($_.ParentProcessId)`t$(if ($_.CreationDate) { $_.CreationDate.ToUniversalTime().ToString(\'o\') })`t$($_.Name)" }'], { encoding: 'utf8', cwd: homedir(), env: helperEnv }) |
| 129 | + : spawnSync('ps', ['-A', '-o', 'pid=,ppid=,lstart=,comm='], { encoding: 'utf8', cwd: homedir(), env: helperEnv }); |
117 | 130 | if (r.status !== 0 || !r.stdout) return null; |
118 | 131 | const table = new Map(); |
119 | 132 | for (const line of r.stdout.split(/\r?\n/)) { |
|
0 commit comments