Skip to content

Commit f507095

Browse files
committed
Add close_browser utility to terminate CWC-managed Chrome before updating the extension
- run 'chrome for testing' with --disable-infobars - when updating, is it possible to close 'chrome for testing' opened by `apps/editor/src/views/prompt/backend/message-handlers/handle-run-browser.ts` - is giving a profile name necessary to close all instances of 'chrome for testing' browser?
1 parent 70d5922 commit f507095

3 files changed

Lines changed: 115 additions & 1 deletion

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import * as vscode from 'vscode'
2+
import * as os from 'os'
3+
import * as path from 'path'
4+
import { execFile } from 'child_process'
5+
import { promisify } from 'util'
6+
7+
const exec_file = promisify(execFile)
8+
9+
const wait_for_exit = async (pid: number, timeout_ms: number) => {
10+
const deadline = Date.now() + timeout_ms
11+
while (Date.now() < deadline) {
12+
try {
13+
process.kill(pid, 0)
14+
} catch {
15+
return true
16+
}
17+
await new Promise((resolve) => setTimeout(resolve, 100))
18+
}
19+
return false
20+
}
21+
22+
export const close_browser = async () => {
23+
if (vscode.env.remoteName) {
24+
return
25+
}
26+
27+
const chrome_dir_prefix = path.join(os.homedir(), '.cwc', 'chrome') + path.sep
28+
29+
try {
30+
if (process.platform === 'win32') {
31+
const script = [
32+
'$target = $env:CWC_CHROME_DIR',
33+
'$procs = Get-CimInstance Win32_Process -Filter "Name = \'chrome.exe\'" | Where-Object {',
34+
' $_.ExecutablePath -and',
35+
' $_.ExecutablePath.StartsWith($target, [System.StringComparison]::OrdinalIgnoreCase) -and',
36+
" (-not $_.CommandLine -or $_.CommandLine.IndexOf('--type=', [System.StringComparison]::OrdinalIgnoreCase) -lt 0)",
37+
'}',
38+
'foreach ($p in $procs) {',
39+
' $proc = Get-Process -Id $p.ProcessId -ErrorAction SilentlyContinue',
40+
' if ($proc) {',
41+
' taskkill /PID $p.ProcessId 2>$null | Out-Null',
42+
' if (-not $proc.WaitForExit(3000)) {',
43+
' taskkill /F /T /PID $p.ProcessId 2>$null | Out-Null',
44+
' }',
45+
' }',
46+
'}'
47+
].join('\n')
48+
49+
await exec_file(
50+
'powershell.exe',
51+
['-NoProfile', '-NonInteractive', '-Command', script],
52+
{
53+
env: {
54+
...process.env,
55+
CWC_CHROME_DIR: chrome_dir_prefix
56+
},
57+
windowsHide: true
58+
}
59+
)
60+
} else {
61+
const { stdout } = await exec_file(
62+
'ps',
63+
['-e', '-ww', '-o', 'pid=,args='],
64+
{ maxBuffer: 10 * 1024 * 1024 }
65+
)
66+
67+
const pids = stdout
68+
.split('\n')
69+
.map((line) => line.trim())
70+
.map((line) => {
71+
const match = line.match(/^(\d+)\s+(.*)$/)
72+
if (!match) return null
73+
return {
74+
pid: parseInt(match[1], 10),
75+
args: match[2]
76+
}
77+
})
78+
.filter(
79+
(entry): entry is { pid: number; args: string } =>
80+
entry !== null &&
81+
entry.pid !== process.pid &&
82+
entry.args.startsWith(chrome_dir_prefix) &&
83+
!entry.args.includes('--type=')
84+
)
85+
.map((entry) => entry.pid)
86+
87+
await Promise.all(
88+
pids.map(async (pid) => {
89+
try {
90+
process.kill(pid, 'SIGTERM')
91+
} catch {
92+
return
93+
}
94+
95+
const exited = await wait_for_exit(pid, 3000)
96+
if (!exited) {
97+
try {
98+
process.kill(pid, 'SIGKILL')
99+
} catch {
100+
// Process already exited
101+
}
102+
}
103+
})
104+
)
105+
}
106+
} catch {
107+
// Best-effort cleanup; do not block the update if closing fails
108+
}
109+
}

‎apps/editor/src/utils/update-extension.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import * as vscode from 'vscode'
22
import * as os from 'os'
3+
import { close_browser } from './close-browser'
34

45
export const update_extension = async (params: { tag_name: string }) => {
56
const terminal = vscode.window.createTerminal('CWC Updater')
67
terminal.show()
78

9+
await close_browser()
10+
811
const uri_scheme = vscode.env.uriScheme
912
const lang = vscode.env.language
1013

‎apps/editor/src/views/prompt/backend/message-handlers/handle-run-browser.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ const launch_browser = (
1818
) => {
1919
const args = [
2020
`--user-data-dir=${user_data_dir}`,
21-
`--load-extension=${extension_path}`
21+
`--load-extension=${extension_path}`,
22+
'--disable-infobars',
23+
'--no-first-run'
2224
]
2325

2426
if (process.platform === 'darwin') {

0 commit comments

Comments
 (0)