@@ -11,198 +11,37 @@ import {
1111} from '@puppeteer/browsers'
1212import { t } from '@/i18n'
1313
14- const is_wsl = ( ) =>
15- process . platform === 'linux' &&
16- ( vscode . env . remoteName === 'wsl' || / m i c r o s o f t / i. test ( os . release ( ) ) )
17-
18- const get_log_tail = ( log_path : string ) => {
19- try {
20- return fs
21- . readFileSync ( log_path , 'utf-8' )
22- . trim ( )
23- . split ( '\n' )
24- . slice ( - 5 )
25- . join ( '\n' )
26- } catch {
27- return ''
28- }
29- }
30-
31- const chrome_debian_packages = [
32- 'libnss3' ,
33- 'libatk1.0-0' ,
34- 'libatk-bridge2.0-0' ,
35- 'libcups2' ,
36- 'libdrm2' ,
37- 'libxkbcommon0' ,
38- 'libxcomposite1' ,
39- 'libxdamage1' ,
40- 'libxfixes3' ,
41- 'libxrandr2' ,
42- 'libgbm1'
43- ]
44-
45- const get_distro_ids = ( ) : string [ ] => {
46- try {
47- const content = fs . readFileSync ( '/etc/os-release' , 'utf-8' )
48- const ids : string [ ] = [ ]
49- for ( const line of content . split ( '\n' ) ) {
50- const match = line . match ( / ^ ( I D | I D _ L I K E ) = ( .* ) $ / )
51- if ( match ) {
52- ids . push (
53- ...match [ 2 ]
54- . replace ( / [ " ' ] / g, '' )
55- . trim ( )
56- . toLowerCase ( )
57- . split ( / \s + / )
58- . filter ( Boolean )
59- )
60- }
61- }
62- return ids
63- } catch {
64- return [ ]
65- }
66- }
67-
68- const get_install_command = ( ) : string | undefined => {
69- const ids = get_distro_ids ( )
70- const is_distro = ( ...names : string [ ] ) =>
71- names . some ( ( name ) => ids . includes ( name ) )
72-
73- if ( is_distro ( 'debian' , 'ubuntu' ) ) {
74- const asound_package =
75- '$(apt-cache show libasound2t64 >/dev/null 2>&1 && echo libasound2t64 || echo libasound2)'
76- return `sudo apt-get update && sudo apt-get install -y ${ chrome_debian_packages . join ( ' ' ) } ${ asound_package } `
77- }
78-
79- if ( is_distro ( 'fedora' , 'rhel' , 'centos' ) ) {
80- return 'sudo dnf install -y nss atk at-spi2-atk cups-libs libdrm libxkbcommon libXcomposite libXdamage libXfixes libXrandr mesa-libgbm alsa-lib pango cairo'
81- }
82-
83- if ( is_distro ( 'arch' ) ) {
84- return 'sudo pacman -S --needed --noconfirm nss atk at-spi2-core cups libdrm libxkbcommon libxcomposite libxdamage libxfixes libxrandr mesa alsa-lib pango cairo'
85- }
86-
87- if ( is_distro ( 'opensuse' , 'suse' , 'sles' ) ) {
88- return 'sudo zypper install -y mozilla-nss libatk-1_0-0 libatk-bridge-2_0-0 cups-libs libdrm2 libxkbcommon0 libXcomposite1 libXdamage1 libXfixes3 libXrandr2 libgbm1 libasound2'
89- }
90-
91- return undefined
92- }
93-
94- const run_in_terminal = ( command : string ) => {
95- const terminal = vscode . window . createTerminal ( {
96- name : 'Install browser dependencies'
97- } )
98- terminal . show ( )
99- terminal . sendText ( command )
100- }
101-
102- const show_launch_error = ( reason : string , log_path : string ) => {
103- const log_tail = get_log_tail ( log_path )
104-
105- if ( is_wsl ( ) && / s h a r e d l i b r a r i e s / i. test ( log_tail ) ) {
106- const install_command = get_install_command ( )
107- if ( install_command ) {
108- run_in_terminal ( install_command )
109- return
110- }
111- }
112-
113- const hint = / s h a r e d l i b r a r i e s / i. test ( log_tail )
114- ? '\nSystem libraries required by Chrome are missing. Install them, e.g. on Ubuntu/Debian: sudo apt install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libasound2 (libasound2t64 on newer releases).'
115- : ''
116- vscode . window . showErrorMessage (
117- `Failed to launch Browser: ${ reason } ${ hint } \nLog: ${ log_path } ${
118- log_tail ? `\n${ log_tail } ` : ''
119- } `
120- )
121- }
122-
12314const launch_browser = (
12415 executable_path : string ,
12516 user_data_dir : string ,
12617 extension_path : string
127- ) : Promise < void > => {
18+ ) => {
12819 const args = [
12920 `--user-data-dir=${ user_data_dir } ` ,
13021 `--load-extension=${ extension_path } `
13122 ]
132- const env : NodeJS . ProcessEnv = { ...process . env }
13323
13424 if ( process . platform === 'darwin' ) {
13525 args . push ( '--use-mock-keychain' )
13626 } else if ( process . platform === 'linux' ) {
13727 args . push ( '--password-store=basic' )
138-
139- if ( is_wsl ( ) ) {
140- args . push ( '--no-sandbox' , '--disable-dev-shm-usage' )
141- env . DISPLAY ??= ':0'
142- env . WAYLAND_DISPLAY ??= 'wayland-0'
143- env . XDG_RUNTIME_DIR ??= '/mnt/wslg/runtime-dir'
144- }
14528 }
14629
147- const log_path = path . join ( os . homedir ( ) , '.cwc' , 'browser.log' )
148-
149- return new Promise < void > ( ( resolve ) => {
150- let settled = false
151- let timer : NodeJS . Timeout | undefined
152-
153- const settle = ( ) => {
154- if ( settled ) return
155- settled = true
156- if ( timer ) clearTimeout ( timer )
157- resolve ( )
158- }
159-
160- let log_fd : number
161- try {
162- log_fd = fs . openSync ( log_path , 'w' )
163- } catch {
164- log_fd = - 1
165- }
166-
167- try {
168- const child = spawn ( executable_path , args , {
169- detached : true ,
170- stdio : log_fd >= 0 ? [ 'ignore' , log_fd , log_fd ] : 'ignore' ,
171- env
172- } )
173-
174- child . on ( 'error' , ( error ) => {
175- if ( settled ) return
176- show_launch_error ( error . message , log_path )
177- settle ( )
178- } )
179-
180- child . on ( 'exit' , ( code , signal ) => {
181- if ( settled ) return
182- if ( code !== 0 ) {
183- show_launch_error (
184- `process exited early (${ signal ? `signal ${ signal } ` : `code ${ code } ` } )` ,
185- log_path
186- )
187- }
188- settle ( )
189- } )
190-
191- timer = setTimeout ( ( ) => {
192- // Still running after 3s, so startup succeeded
193- child . unref ( )
194- settle ( )
195- } , 3000 )
196- } catch ( error : any ) {
197- show_launch_error ( error . message , log_path )
198- settle ( )
199- } finally {
200- if ( log_fd >= 0 ) fs . closeSync ( log_fd )
201- }
30+ const child = spawn ( executable_path , args , {
31+ detached : true ,
32+ stdio : 'ignore'
20233 } )
34+ child . unref ( )
20335}
20436
20537export const handle_run_browser = async ( ) => {
38+ if ( vscode . env . remoteName ) {
39+ vscode . window . showWarningMessage (
40+ t ( 'views.prompt.handlers.handle-run-browser.remote-not-supported' )
41+ )
42+ return
43+ }
44+
20645 const cwc_dir = path . join ( os . homedir ( ) , '.cwc' )
20746
20847 if ( ! fs . existsSync ( cwc_dir ) ) {
0 commit comments