Skip to content

Commit 6b1b791

Browse files
bmeurerDevtools-frontend LUCI CQ
authored andcommitted
[npm] Port npm start to JavaScript.
There's no point in writing this one script in Python. It's much better to have it in JavaScript, just like `npm run build`. Bug: 404192426 Change-Id: Ic19eaeb5b1d97ca750ae4253963b8434072d00fd Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6387801 Auto-Submit: Benedikt Meurer <[email protected]> Commit-Queue: Benedikt Meurer <[email protected]> Reviewed-by: Nikolay Vitkov <[email protected]>
1 parent 6662b21 commit 6b1b791

File tree

6 files changed

+144
-154
lines changed

6 files changed

+144
-154
lines changed

docs/get_the_code.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ source files for changes while Chrome is running and automatically trigger a reb
150150
By default, `npm start` will automatically open DevTools for every new tab, you can use
151151

152152
```bash
153-
npm start -- --no-auto-open-devtools-for-tabs
153+
npm start -- --no-open
154154
```
155155

156156
to disable this behavior. You can also use
157157

158158
```bash
159-
npm start -- --canary
159+
npm start -- --browser=canary
160160
```
161161

162162
to run in Chrome Canary instead of Chrome for Testing; this requires you to install Chrome Canary manually first

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"lint": "vpython3 third_party/node/node.py --output scripts/test/run_lint_check.mjs",
2929
"prebuild": "gn gen out/Default",
3030
"rdb": "rdb stream -new -realm chrome:public --",
31-
"start": "vpython3 scripts/run_start.py",
31+
"start": "vpython3 third_party/node/node.py --output scripts/run_start.mjs",
3232
"webtest": "vpython3 third_party/node/node.py --output scripts/npm_test.js",
3333
"watch": "vpython3 third_party/node/node.py --output scripts/watch_build.js",
3434
"test": "vpython3 third_party/node/node.py --output scripts/run_on_target.mjs gen/test/run.js"

scripts/devtools_paths.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ module.exports = {
170170
mochaExecutablePath,
171171
nodeModulesPath,
172172
nodePath,
173+
rootPath,
173174
stylelintExecutablePath,
174175
thirdPartyPath,
175176
tsconfigJsonPath,

scripts/run_build.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const {target, watch, skipInitialBuild} = argv;
3636
const cwd = process.cwd();
3737
const {env} = process;
3838

39-
// Create and initiative the `out/<target>` directory as needed.
39+
// Create and initialize the `out/<target>` directory as needed.
4040
const outDir = path.join('out', target);
4141
if (!fs.existsSync(outDir)) {
4242
const gnExe = path.join(cwd, 'third_party', 'depot_tools', 'gn');

scripts/run_start.mjs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
}

scripts/run_start.py

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)