Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // 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 Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 5 | const childProcess = require('child_process'); |
| 6 | const fs = require('fs'); |
| 7 | const path = require('path'); |
| 8 | const shell = childProcess.execSync; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 9 | |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 10 | const utils = require('../utils'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 11 | |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 12 | const REMOTE_DEBUGGING_PORT = parseInt(process.env.REMOTE_DEBUGGING_PORT, 10) || 9222; |
| 13 | const SERVER_PORT = parseInt(process.env.PORT, 10) || 8090; |
| 14 | const CHROMIUM_DEFAULT_PATH = path.resolve(__dirname, '..', '..', 'third_party', 'chrome', 'chrome-linux', 'chrome'); |
| 15 | const CHROME_PROFILE_PATH = path.resolve(__dirname, '..', '..', '.dev_profile'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 16 | |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 17 | const Flags = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 18 | RESET_PROFILE: '--reset-profile', |
| 19 | }; |
| 20 | |
| 21 | if (utils.includes(process.argv, Flags.RESET_PROFILE)) { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 22 | console.log('Removing your dev profile for Chrome Canary / Chromium at:'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 23 | console.log(CHROME_PROFILE_PATH, '\n'); |
| 24 | utils.removeRecursive(CHROME_PROFILE_PATH); |
| 25 | } |
| 26 | |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 27 | const chromeArgs = [ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 28 | `--remote-debugging-port=${REMOTE_DEBUGGING_PORT}`, |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 29 | `--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 Guo | 49346f1 | 2020-02-06 09:52:02 | [diff] [blame] | 31 | `--user-data-dir=${CHROME_PROFILE_PATH}` |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 32 | ].concat(process.argv.slice(2)); |
| 33 | |
| 34 | if (process.platform === 'win32') { |
| 35 | launchChromeWindows(); |
| 36 | return; |
| 37 | } |
| 38 | if (process.platform === 'darwin') { |
| 39 | launchChromeMac(); |
| 40 | return; |
| 41 | } |
| 42 | if (process.platform === 'linux') { |
| 43 | launchChromeLinux(); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | throw new Error(`Unrecognized platform detected: ${process.platform}`); |
| 48 | |
| 49 | function launchChromeWindows() { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 50 | let chromeCanaryPath; |
Jeff Fisher | 022a7ad | 2019-02-22 20:19:06 | [diff] [blame] | 51 | if (utils.isFile(process.env.CHROMIUM_PATH)) { |
| 52 | chromeCanaryPath = process.env.CHROMIUM_PATH; |
| 53 | } else { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 54 | 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 Fisher | 022a7ad | 2019-02-22 20:19:06 | [diff] [blame] | 58 | try { |
| 59 | chromeCanaryPath = path.join(prefix, suffix); |
| 60 | fs.accessSync(chromeCanaryPath); |
| 61 | break; |
| 62 | } catch (e) { |
| 63 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | launchChrome(chromeCanaryPath, chromeArgs); |
| 67 | } |
| 68 | |
| 69 | function launchChromeMac() { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 70 | let chromeExecPath; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 71 | if (utils.isFile(process.env.CHROMIUM_PATH)) { |
| 72 | chromeExecPath = process.env.CHROMIUM_PATH; |
| 73 | } else { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 74 | const lsregister = |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 75 | '/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister'; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 76 | const chromeCanaryPath = shellOutput( |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | `${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 | |
| 83 | function launchChromeLinux() { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 84 | let chromiumPath; |
| 85 | if (utils.isFile(process.env.CHROMIUM_PATH)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 86 | chromiumPath = process.env.CHROMIUM_PATH; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 87 | } else if (utils.isFile(CHROMIUM_DEFAULT_PATH)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 88 | chromiumPath = CHROMIUM_DEFAULT_PATH; |
| 89 | } else { |
| 90 | onLaunchChromeError(); |
| 91 | return; |
| 92 | } |
| 93 | launchChrome(chromiumPath, chromeArgs); |
| 94 | } |
| 95 | |
| 96 | function launchChrome(filePath, chromeArgs) { |
| 97 | console.log(`Launching Chrome from ${filePath}`); |
| 98 | console.log('Chrome args:', chromeArgs.join(' '), '\n'); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 99 | let child; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 100 | 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 | |
| 115 | function 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 128 | function shellOutput(command) { |
| 129 | return shell(command).toString().trim(); |
| 130 | } |