blob: 5a3c95141a2f5029029b3113a70a0dca7a1f6a8e [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright 2016 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
Tim van der Lippeba26b2b2020-03-11 14:40:005const childProcess = require('child_process');
6const fs = require('fs');
7const path = require('path');
8const shell = childProcess.execSync;
Blink Reformat4c46d092018-04-07 15:32:379
Tim van der Lippeba26b2b2020-03-11 14:40:0010const utils = require('../utils');
Blink Reformat4c46d092018-04-07 15:32:3711
Tim van der Lippeba26b2b2020-03-11 14:40:0012const REMOTE_DEBUGGING_PORT = parseInt(process.env.REMOTE_DEBUGGING_PORT, 10) || 9222;
13const SERVER_PORT = parseInt(process.env.PORT, 10) || 8090;
14const CHROMIUM_DEFAULT_PATH = path.resolve(__dirname, '..', '..', 'third_party', 'chrome', 'chrome-linux', 'chrome');
15const CHROME_PROFILE_PATH = path.resolve(__dirname, '..', '..', '.dev_profile');
Blink Reformat4c46d092018-04-07 15:32:3716
Tim van der Lippeba26b2b2020-03-11 14:40:0017const Flags = {
Blink Reformat4c46d092018-04-07 15:32:3718 RESET_PROFILE: '--reset-profile',
19};
20
21if (utils.includes(process.argv, Flags.RESET_PROFILE)) {
Tim van der Lippeba26b2b2020-03-11 14:40:0022 console.log('Removing your dev profile for Chrome Canary / Chromium at:');
Blink Reformat4c46d092018-04-07 15:32:3723 console.log(CHROME_PROFILE_PATH, '\n');
24 utils.removeRecursive(CHROME_PROFILE_PATH);
25}
26
Tim van der Lippeba26b2b2020-03-11 14:40:0027const chromeArgs = [
Blink Reformat4c46d092018-04-07 15:32:3728 `--remote-debugging-port=${REMOTE_DEBUGGING_PORT}`,
Tim van der Lippeba26b2b2020-03-11 14:40:0029 `--custom-devtools-frontend=http://localhost:${SERVER_PORT}/front_end/`, '--no-first-run',
30 `http://localhost:${REMOTE_DEBUGGING_PORT}#custom=true`, 'https://devtools.chrome.com',
Yang Guo49346f12020-02-06 09:52:0231 `--user-data-dir=${CHROME_PROFILE_PATH}`
Blink Reformat4c46d092018-04-07 15:32:3732].concat(process.argv.slice(2));
33
34if (process.platform === 'win32') {
35 launchChromeWindows();
36 return;
37}
38if (process.platform === 'darwin') {
39 launchChromeMac();
40 return;
41}
42if (process.platform === 'linux') {
43 launchChromeLinux();
44 return;
45}
46
47throw new Error(`Unrecognized platform detected: ${process.platform}`);
48
49function launchChromeWindows() {
Tim van der Lippeba26b2b2020-03-11 14:40:0050 let chromeCanaryPath;
Jeff Fisher022a7ad2019-02-22 20:19:0651 if (utils.isFile(process.env.CHROMIUM_PATH)) {
52 chromeCanaryPath = process.env.CHROMIUM_PATH;
53 } else {
Tim van der Lippeba26b2b2020-03-11 14:40:0054 const suffix = '\\Google\\Chrome SxS\\Application\\chrome.exe';
55 const prefixes = [process.env.LOCALAPPDATA, process.env.PROGRAMFILES, process.env['PROGRAMFILES(X86)']];
56 for (let i = 0; i < prefixes.length; i++) {
57 const prefix = prefixes[i];
Jeff Fisher022a7ad2019-02-22 20:19:0658 try {
59 chromeCanaryPath = path.join(prefix, suffix);
60 fs.accessSync(chromeCanaryPath);
61 break;
62 } catch (e) {
63 }
Blink Reformat4c46d092018-04-07 15:32:3764 }
65 }
66 launchChrome(chromeCanaryPath, chromeArgs);
67}
68
69function launchChromeMac() {
Tim van der Lippeba26b2b2020-03-11 14:40:0070 let chromeExecPath;
Blink Reformat4c46d092018-04-07 15:32:3771 if (utils.isFile(process.env.CHROMIUM_PATH)) {
72 chromeExecPath = process.env.CHROMIUM_PATH;
73 } else {
Tim van der Lippeba26b2b2020-03-11 14:40:0074 const lsregister =
Blink Reformat4c46d092018-04-07 15:32:3775 '/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister';
Tim van der Lippeba26b2b2020-03-11 14:40:0076 const chromeCanaryPath = shellOutput(
Blink Reformat4c46d092018-04-07 15:32:3777 `${lsregister} -dump | grep -i 'applications/google chrome canary.app$' | awk '{$1=""; print $0}' | head -n 1`);
78 chromeExecPath = `${chromeCanaryPath}/Contents/MacOS/Google Chrome Canary`;
79 }
80 launchChrome(chromeExecPath, chromeArgs);
81}
82
83function launchChromeLinux() {
Tim van der Lippeba26b2b2020-03-11 14:40:0084 let chromiumPath;
85 if (utils.isFile(process.env.CHROMIUM_PATH)) {
Blink Reformat4c46d092018-04-07 15:32:3786 chromiumPath = process.env.CHROMIUM_PATH;
Tim van der Lippeba26b2b2020-03-11 14:40:0087 } else if (utils.isFile(CHROMIUM_DEFAULT_PATH)) {
Blink Reformat4c46d092018-04-07 15:32:3788 chromiumPath = CHROMIUM_DEFAULT_PATH;
89 } else {
90 onLaunchChromeError();
91 return;
92 }
93 launchChrome(chromiumPath, chromeArgs);
94}
95
96function launchChrome(filePath, chromeArgs) {
97 console.log(`Launching Chrome from ${filePath}`);
98 console.log('Chrome args:', chromeArgs.join(' '), '\n');
Tim van der Lippeba26b2b2020-03-11 14:40:0099 let child;
Blink Reformat4c46d092018-04-07 15:32:37100 try {
101 child = childProcess.spawn(filePath, chromeArgs, {
102 stdio: 'inherit',
103 });
104 } catch (error) {
105 onLaunchChromeError();
106 return;
107 }
108 child.on('error', onLaunchChromeError);
109 child.on('exit', onExit);
110 function onExit(code) {
111 console.log('Exited Chrome with code', code);
112 }
113}
114
115function onLaunchChromeError() {
116 if (process.platform !== 'linux') {
117 console.log('ERROR: Cannot find Chrome Canary on your computer');
118 console.log('Install Chome Canary at:');
119 console.log('https://www.google.com/chrome/browser/canary.html\n');
120 } else {
121 console.log('ERROR: Could not launch Chromium');
122 console.log('The environment variable CHROMIUM_PATH must be set to executable of a build of Chromium');
123 console.log('If you do not have a recent build of chromium, you can get one from:');
124 console.log('https://download-chromium.appspot.com/\n');
125 }
126}
127
Blink Reformat4c46d092018-04-07 15:32:37128function shellOutput(command) {
129 return shell(command).toString().trim();
130}