@@ -2,14 +2,43 @@ import * as vscode from 'vscode'
22import * as path from 'path'
33import * as os from 'os'
44import * 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
737export 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 ( / c h r o m e @ \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