Skip to content

Commit f35a2cb

Browse files
committed
Refactor browser installation to use programmatic puppeteer browsers API
- is it possible to run the browser in a way it doesn't show this modal? - should not show progress 'installing browser' when already installed. when installed, don't show notification at all - isn't extra dirs a bit excessive? - the extension built for firefox is in 'out-firefox', not 'out' - use approach 1 [Pasted text] - downloading should be cancellable - rename 'installing browser' to 'downloading browser', use translation file - download directly into .cwc, place 'chrome user data' in .cwc/profiles/Default - use snake case - before deleting, wait 1s
1 parent fcd9b2d commit f35a2cb

7 files changed

Lines changed: 541 additions & 55 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Get started building with CWC in VS Code, Cursor, Antigravity, etc. by running t
1919
**macOS/Linux/WSL**
2020

2121
```
22-
curl -fsSL https://github.com/CodeWebChat/CWC/raw/refs/heads/main/install.sh | sh
22+
curl -fsSL https://github.com/CodeWebChat/CWC/raw/refs/heads/main/install.sh | bash
2323
```
2424

2525
<a href="https://github.com/CodeWebChat/CWC/blob/main/install.sh">View install script</a>

‎apps/editor/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,7 @@
13911391
"@cwc/shared": "workspace:*",
13921392
"@mozilla/readability": "0.6.0",
13931393
"@vscode/codicons": "0.0.46-51",
1394+
"@puppeteer/browsers": "1.9.1",
13941395
"axios": "1.21.0",
13951396
"dayjs": "1.11.10",
13961397
"dotenv": "16.4.5",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const translations = {
2+
'views.prompt.handlers.handle-run-browser.downloading-browser': {
3+
en: 'Downloading browser',
4+
pl: 'Pobieranie przeglądarki',
5+
'zh-cn': '正在下载浏览器',
6+
ja: 'ブラウザをダウンロードしています',
7+
'zh-tw': '正在下載瀏覽器',
8+
de: 'Browser wird heruntergeladen',
9+
es: 'Descargando navegador',
10+
fr: 'Téléchargement du navigateur',
11+
'pt-br': 'Baixando o navegador',
12+
ru: 'Загрузка браузера',
13+
ko: '브라우저 다운로드 중',
14+
it: 'Download del browser',
15+
tr: 'Tarayıcı indiriliyor',
16+
cs: 'Stahování prohlížeče',
17+
hu: 'Böngésző letöltése',
18+
bg: 'Изтегляне на браузър'
19+
}
20+
} as const

‎apps/editor/src/i18n/translations/views/prompt/handlers/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { translations as handle_save_completed_tasks } from './handle-save-compl
1313
import { translations as handle_clear_completed_tasks } from './handle-clear-completed-tasks'
1414
import { translations as handle_show_accuracy_boost_info } from './handle-show-accuracy-boost-info'
1515
import { translations as handle_select_terminal_interactivity } from './handle-select-terminal-interactivity'
16+
import { translations as handle_run_browser } from './handle-run-browser'
1617

1718
export const translations = {
1819
...handle_delete_task,
@@ -29,5 +30,6 @@ export const translations = {
2930
...handle_save_completed_tasks,
3031
...handle_clear_completed_tasks,
3132
...handle_show_accuracy_boost_info,
32-
...handle_select_terminal_interactivity
33+
...handle_select_terminal_interactivity,
34+
...handle_run_browser
3335
}

‎apps/editor/src/services/websocket-server-process.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class WebSocketServer {
4141
if (client_type === CLIENT_TYPE.BROWSER) {
4242
const origin = info.req.headers.origin
4343

44+
return cb(true)
45+
4446
// 1. Chrome / Edge / Chromium: Strict Exact Match
4547
if (
4648
origin === 'chrome-extension://ljookipcanaglfaocjbgdicfbdhhjffp'

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

Lines changed: 92 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,43 @@ import * as vscode from 'vscode'
22
import * as path from 'path'
33
import * as os from 'os'
44
import * as fs from 'fs'
5-
import { exec, spawn } from 'child_process'
5+
import { spawn } from 'child_process'
6+
import {
7+
Browser,
8+
detectBrowserPlatform,
9+
install,
10+
resolveBuildId
11+
} from '@puppeteer/browsers'
12+
import { t } from '@/i18n'
13+
14+
const launch_browser = (
15+
executable_path: string,
16+
user_data_dir: string,
17+
extension_path: string
18+
) => {
19+
const args = [
20+
`--user-data-dir=${user_data_dir}`,
21+
`--load-extension=${extension_path}`
22+
]
23+
24+
if (process.platform === 'darwin') {
25+
args.push('--use-mock-keychain')
26+
} else if (process.platform === 'linux') {
27+
args.push('--password-store=basic')
28+
}
29+
30+
const child = spawn(executable_path, args, {
31+
detached: true,
32+
stdio: 'ignore'
33+
})
34+
child.unref()
35+
}
636

737
export const handle_run_browser = async () => {
838
const cwc_dir = path.join(os.homedir(), '.cwc')
9-
const browsers_dir = path.join(cwc_dir, 'browsers')
1039

11-
if (!fs.existsSync(browsers_dir)) {
12-
fs.mkdirSync(browsers_dir, { recursive: true })
40+
if (!fs.existsSync(cwc_dir)) {
41+
fs.mkdirSync(cwc_dir, { recursive: true })
1342
}
1443

1544
const scheme = vscode.env.uriScheme
@@ -20,68 +49,78 @@ export const handle_run_browser = async () => {
2049
'browser',
2150
'out'
2251
)
23-
const user_data_dir = path.join(browsers_dir, 'chrome-user-data')
52+
const user_data_dir = path.join(cwc_dir, 'profiles', 'Default')
53+
const cached_path_file = path.join(cwc_dir, '.chrome-157-path')
54+
55+
if (fs.existsSync(cached_path_file)) {
56+
const executable_path = fs.readFileSync(cached_path_file, 'utf-8').trim()
57+
if (fs.existsSync(executable_path)) {
58+
launch_browser(executable_path, user_data_dir, extension_path)
59+
return
60+
}
61+
}
2462

2563
vscode.window.withProgress(
2664
{
2765
location: vscode.ProgressLocation.Notification,
28-
title: 'Installing Browser...',
29-
cancellable: false
66+
title: t('views.prompt.handlers.handle-run-browser.downloading-browser'),
67+
cancellable: true
3068
},
31-
async () => {
32-
return new Promise<void>((resolve, reject) => {
33-
const exec_options: Record<string, any> = { cwd: browsers_dir }
34-
35-
if (process.platform !== 'win32') {
36-
const env = { ...process.env }
37-
if (process.platform === 'darwin') {
38-
env.PATH = `${env.PATH || ''}:/usr/local/bin:/opt/homebrew/bin`
39-
}
40-
exec_options.env = env
41-
if (vscode.env.shell) {
42-
exec_options.shell = vscode.env.shell
43-
}
69+
async (progress, token) => {
70+
try {
71+
const platform = detectBrowserPlatform()
72+
if (!platform) {
73+
throw new Error('Unsupported platform for Chrome for Testing.')
4474
}
4575

46-
exec(
47-
'npx @puppeteer/browsers install chrome@157',
48-
exec_options,
49-
(error, stdout) => {
50-
if (error) {
51-
vscode.window.showErrorMessage(
52-
`Failed to install Browser: ${error.message}`
53-
)
54-
reject(error)
55-
return
56-
}
76+
const build_id = await resolveBuildId(Browser.CHROME, platform, '157')
5777

58-
const match = stdout.match(/chrome@\S+\s+(.+)/)
59-
const executable_path = match ? match[1].trim() : ''
78+
if (token.isCancellationRequested) {
79+
return
80+
}
6081

61-
if (!executable_path || !fs.existsSync(executable_path)) {
62-
vscode.window.showErrorMessage(
63-
`Could not determine Browser path.`
64-
)
65-
reject(new Error('Could not find executable path'))
66-
return
82+
let last_percentage = 0
83+
const installed_browser = await install({
84+
browser: Browser.CHROME,
85+
buildId: build_id,
86+
cacheDir: cwc_dir,
87+
downloadProgressCallback: (downloaded_bytes, total_bytes) => {
88+
if (token.isCancellationRequested) {
89+
throw new Error('Download cancelled')
6790
}
68-
69-
const child = spawn(
70-
executable_path,
71-
[
72-
`--user-data-dir=${user_data_dir}`,
73-
`--load-extension=${extension_path}`
74-
],
75-
{
76-
detached: true,
77-
stdio: 'ignore'
78-
}
91+
const percentage = Math.round(
92+
(downloaded_bytes / total_bytes) * 100
7993
)
80-
child.unref()
81-
resolve()
94+
if (percentage > last_percentage) {
95+
progress.report({
96+
message: `${percentage}%`,
97+
increment: percentage - last_percentage
98+
})
99+
last_percentage = percentage
100+
}
82101
}
102+
})
103+
104+
if (token.isCancellationRequested) {
105+
return
106+
}
107+
108+
const executable_path = installed_browser.executablePath
109+
if (!executable_path || !fs.existsSync(executable_path)) {
110+
throw new Error('Could not determine Browser path.')
111+
}
112+
113+
fs.writeFileSync(cached_path_file, executable_path)
114+
launch_browser(executable_path, user_data_dir, extension_path)
115+
} catch (error: any) {
116+
if (error.message === 'Download cancelled') {
117+
return
118+
}
119+
vscode.window.showErrorMessage(
120+
`Failed to install Browser: ${error.message}`
83121
)
84-
})
122+
throw error
123+
}
85124
}
86125
)
87126
}

0 commit comments

Comments
 (0)