|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as path from 'path' |
| 3 | +import * as os from 'os' |
| 4 | +import * as fs from 'fs' |
| 5 | +import { exec, spawn } from 'child_process' |
| 6 | + |
| 7 | +export const handle_run_browser = async () => { |
| 8 | + const cwc_dir = path.join(os.homedir(), '.cwc') |
| 9 | + const browsers_dir = path.join(cwc_dir, 'browsers') |
| 10 | + |
| 11 | + if (!fs.existsSync(browsers_dir)) { |
| 12 | + fs.mkdirSync(browsers_dir, { recursive: true }) |
| 13 | + } |
| 14 | + |
| 15 | + const scheme = vscode.env.uriScheme |
| 16 | + const extension_path = path.join( |
| 17 | + cwc_dir, |
| 18 | + `repository-${scheme}`, |
| 19 | + 'apps', |
| 20 | + 'browser', |
| 21 | + 'out' |
| 22 | + ) |
| 23 | + const user_data_dir = path.join(browsers_dir, 'chrome-user-data') |
| 24 | + |
| 25 | + vscode.window.withProgress( |
| 26 | + { |
| 27 | + location: vscode.ProgressLocation.Notification, |
| 28 | + title: 'Installing Browser...', |
| 29 | + cancellable: false |
| 30 | + }, |
| 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 | + } |
| 44 | + } |
| 45 | + |
| 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 | + } |
| 57 | + |
| 58 | + const match = stdout.match(/chrome@\S+\s+(.+)/) |
| 59 | + const executable_path = match ? match[1].trim() : '' |
| 60 | + |
| 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 |
| 67 | + } |
| 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 | + } |
| 79 | + ) |
| 80 | + child.unref() |
| 81 | + resolve() |
| 82 | + } |
| 83 | + ) |
| 84 | + }) |
| 85 | + } |
| 86 | + ) |
| 87 | +} |
0 commit comments