|
| 1 | +// Copyright 2025 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import childProcess from 'node:child_process'; |
| 6 | +import path from 'node:path'; |
| 7 | +import yargs from 'yargs'; |
| 8 | +import {hideBin} from 'yargs/helpers'; |
| 9 | + |
| 10 | +import {downloadedChromeBinaryPath, rootPath} from './devtools_paths.js'; |
| 11 | + |
| 12 | +// The list of features that are enabled by default. |
| 13 | +const ENABLE_FEATURES = [ |
| 14 | + 'DevToolsAutomaticFileSystems', |
| 15 | + 'DevToolsCssValueTracing', |
| 16 | + 'DevToolsFreeStyler:patching/true,user_tier/TESTERS', |
| 17 | + 'DevToolsWellKnown', |
| 18 | +]; |
| 19 | + |
| 20 | +// The list of features that are disabled by default. |
| 21 | +const DISABLE_FEATURES = []; |
| 22 | +if (process.platform === 'darwin') { |
| 23 | + DISABLE_FEATURES.push('MediaRouter'); |
| 24 | +} |
| 25 | + |
| 26 | +const argv = yargs(hideBin(process.argv)) |
| 27 | + .option('browser', { |
| 28 | + type: 'string', |
| 29 | + choices: ['cft', 'canary'], |
| 30 | + default: 'cft', |
| 31 | + description: 'Launch in the specified browser', |
| 32 | + }) |
| 33 | + .option('open', { |
| 34 | + type: 'boolean', |
| 35 | + default: true, |
| 36 | + description: 'Automatically open DevTools for new tabs', |
| 37 | + }) |
| 38 | + .option('target', { |
| 39 | + alias: 't', |
| 40 | + type: 'string', |
| 41 | + default: 'Default', |
| 42 | + description: 'Specify the target build subdirectory under //out', |
| 43 | + }) |
| 44 | + .option('verbose', { |
| 45 | + type: 'boolean', |
| 46 | + default: false, |
| 47 | + description: 'Enable verbose logging', |
| 48 | + }) |
| 49 | + .usage('npm start -- [options] [urls...]') |
| 50 | + .help('help') |
| 51 | + .version(false) |
| 52 | + .parseSync(); |
| 53 | + |
| 54 | +const {browser, target, open, verbose} = argv; |
| 55 | +const cwd = process.cwd(); |
| 56 | +const {env} = process; |
| 57 | +const runBuildPath = path.join(import.meta.dirname, 'run_build.mjs'); |
| 58 | + |
| 59 | +function findBrowserBinary() { |
| 60 | + if (browser === 'canary') { |
| 61 | + const binary = ({ |
| 62 | + linux: path.join('/usr', 'bin', 'google-chrome-canary'), |
| 63 | + darwin: path.join('/Applications', 'Google Chrome Canary.app', 'Contents', 'MacOS', 'Google Chrome Canary'), |
| 64 | + })[process.platform]; |
| 65 | + if (verbose) { |
| 66 | + console.debug('Located Chrome Canary binary in %s.', binary); |
| 67 | + } |
| 68 | + return binary; |
| 69 | + } |
| 70 | + const binary = downloadedChromeBinaryPath(); |
| 71 | + if (verbose) { |
| 72 | + console.debug('Located Chrome for Testing binary in %s.', binary); |
| 73 | + } |
| 74 | + return binary; |
| 75 | +} |
| 76 | + |
| 77 | +// Perform the initial build. |
| 78 | +childProcess.spawnSync( |
| 79 | + process.argv[0], |
| 80 | + [ |
| 81 | + runBuildPath, |
| 82 | + `--target=${target}`, |
| 83 | + ], |
| 84 | + {cwd, env, stdio: 'inherit'}, |
| 85 | +); |
| 86 | + |
| 87 | +// Launch Chrome with our custom DevTools front-end. |
| 88 | +function start() { |
| 89 | + const binary = findBrowserBinary(); |
| 90 | + const args = []; |
| 91 | + |
| 92 | + // Custom flags for CfT. |
| 93 | + if (browser === 'cft') { |
| 94 | + args.push('--disable-infobars'); |
| 95 | + } |
| 96 | + |
| 97 | + // Custom flags for macOS. |
| 98 | + if (process.platform === 'darwin') { |
| 99 | + args.push('--use-mock-keychain'); |
| 100 | + } |
| 101 | + |
| 102 | + // Disable/Enable experimental features. |
| 103 | + args.push(`--disable-features=${DISABLE_FEATURES.join(',')}`); |
| 104 | + args.push(`--enable-features=${ENABLE_FEATURES.join(',')}`); |
| 105 | + |
| 106 | + // Open with our freshly built DevTools front-end. |
| 107 | + const customDevToolsFrontEndPath = path.join(rootPath(), 'out', target, 'gen', 'front_end'); |
| 108 | + args.push(`--custom-devtools-frontend=file://${customDevToolsFrontEndPath}`); |
| 109 | + |
| 110 | + // Chrome flags and URLs. |
| 111 | + if (open) { |
| 112 | + args.push('--auto-open-devtools-for-tabs'); |
| 113 | + } |
| 114 | + args.push(...argv._); |
| 115 | + |
| 116 | + // Launch Chrome. |
| 117 | + if (verbose) { |
| 118 | + console.debug('Launch Chrome: %s %s', binary, args.join(' ')); |
| 119 | + } |
| 120 | + childProcess.spawnSync(binary, args, {cwd, env, stdio: 'inherit'}); |
| 121 | +} |
| 122 | + |
| 123 | +// Run build watcher in the background to automatically rebuild |
| 124 | +// devtools-frontend whenever there are changes detected. |
| 125 | +const watcher = childProcess.spawn( |
| 126 | + process.argv[0], |
| 127 | + [ |
| 128 | + runBuildPath, |
| 129 | + '--skip-initial-build', |
| 130 | + `--target=${target}`, |
| 131 | + '--watch', |
| 132 | + ], |
| 133 | + {cwd, env, stdio: 'inherit'}); |
| 134 | +try { |
| 135 | + // Launch chrome. |
| 136 | + start(); |
| 137 | +} finally { |
| 138 | + watcher.kill(); |
| 139 | +} |
0 commit comments