Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 1 | // Copyright 2020 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 | /** |
| 6 | * This file contains helpers to load the correct path to various scripts and |
| 7 | * directories. It is the Node equivalent of devtools_paths.py. |
| 8 | * |
| 9 | * Note that not all paths implemented in devtools_paths.py are implemented |
| 10 | * here. Please add any paths you need that are missing. |
| 11 | */ |
| 12 | |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 13 | const os = require('os'); |
Nikolay Vitkov | 41c6911 | 2025-01-31 15:20:04 | [diff] [blame] | 14 | const path = require('path'); |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * You would think we can use __filename here but we cannot because __filename |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 18 | * has any symlinks resolved. This means we can't use it to tell if the user is |
| 19 | * using the external repo with a standalone build setup because the symlink |
| 20 | * from chromium/src/third_party/devtools-frontend => devtools-frontend repo |
| 21 | * gets resolved by Node before it gives us __filename. |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 22 | * |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 23 | * We can use process.argv[1], which is the path to the file currently being |
| 24 | * executed without any symlinks resolution. If we assume that file is always in |
| 25 | * the devtools-frontend repository/directory, we can use that file as the |
| 26 | * starting point for figuring out if we're in Chromium or not. until we find |
| 27 | * the scripts directory, at which point we've found this file and can use it |
| 28 | * for all subsequent logic. |
| 29 | * |
Nikolay Vitkov | 2a1b3b3 | 2025-01-03 10:18:46 | [diff] [blame] | 30 | * e.g. the user executes a script: scripts/test/run_lint_check.mjs |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 31 | * |
| 32 | * process.argv[1] = |
Nikolay Vitkov | 2a1b3b3 | 2025-01-03 10:18:46 | [diff] [blame] | 33 | * /full/path/devtools-frontend/src/scripts/test/run_lint_check.mjs |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 34 | */ |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 35 | const PATH_TO_EXECUTED_FILE = process.argv[1]; |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 36 | |
Nikolay Vitkov | 55adf67 | 2025-01-02 12:07:33 | [diff] [blame] | 37 | const _lookUpCaches = new Map([['chromium', null]]); |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 38 | /** |
| 39 | * This function figures out if we're within a chromium directory, and therefore |
| 40 | * we are in the integrated workflow mode, rather than working in a standalone |
| 41 | * devtools-frontend repository. |
| 42 | */ |
| 43 | function isInChromiumDirectory() { |
| 44 | const cached = _lookUpCaches.get('chromium'); |
| 45 | if (cached) { |
| 46 | return cached; |
| 47 | } |
| 48 | |
Patrick Brosset | 5d02bbd | 2021-04-14 08:32:50 | [diff] [blame] | 49 | const normalizedPath = PATH_TO_EXECUTED_FILE.split(path.sep).join('/'); |
Eric Leese | 5f7e033 | 2025-03-27 16:23:47 | [diff] [blame] | 50 | const devtoolsPath = 'third_party/devtools-frontend/src'; |
Takuto Ikuta | afe8b8e | 2022-01-26 02:42:09 | [diff] [blame] | 51 | const isInChromium = normalizedPath.includes(devtoolsPath); |
Nikolay Vitkov | 55adf67 | 2025-01-02 12:07:33 | [diff] [blame] | 52 | const potentialChromiumDir = PATH_TO_EXECUTED_FILE.substring( |
| 53 | 0, |
| 54 | normalizedPath.indexOf(devtoolsPath), |
| 55 | ); |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 56 | const result = {isInChromium, chromiumDirectory: potentialChromiumDir}; |
| 57 | _lookUpCaches.set('chromium', result); |
| 58 | return result; |
| 59 | } |
| 60 | /** |
| 61 | * Returns the path to the root of the devtools-frontend repository. |
| 62 | * |
| 63 | * If we're in Chromium, this will be /path/to/chromium/src/third_party/devtools-frontend/src |
| 64 | * If it's standalone, it will be /path/to/devtools-frontend |
| 65 | */ |
| 66 | function devtoolsRootPath() { |
Takuto Ikuta | 5bfd102 | 2022-02-07 11:02:32 | [diff] [blame] | 67 | return path.dirname(__dirname); |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns the path to the root of the main repository we're in. |
| 72 | * if we're in Chromium, this is /path/to/chromium/src |
| 73 | * if we're in standalone, this is /path/to/devtools-frontend |
| 74 | * |
| 75 | * Note this is different to devtoolsRootPath(), which always returns the path |
| 76 | * to the devtools-frontend source code. |
| 77 | */ |
| 78 | function rootPath() { |
| 79 | const {isInChromium, chromiumDirectory} = isInChromiumDirectory(); |
| 80 | if (isInChromium) { |
Eric Leese | 5f7e033 | 2025-03-27 16:23:47 | [diff] [blame] | 81 | return chromiumDirectory; |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 82 | } |
| 83 | return devtoolsRootPath(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Path to the third_party directory. Used because if we're running in Chromium |
| 88 | * land we need to use e.g. the Node executable from Chromium's third_party |
| 89 | * directory, not from the devtools-frontend third_party directory. |
| 90 | */ |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 91 | function thirdPartyPath() { |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 92 | return path.join(rootPath(), 'third_party'); |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 93 | } |
| 94 | |
Wolfgang Beyer | 21656cb | 2023-09-04 16:38:13 | [diff] [blame] | 95 | function devToolsThirdPartyPath() { |
| 96 | const {isInChromium} = isInChromiumDirectory(); |
| 97 | if (isInChromium) { |
Nikolay Vitkov | 55adf67 | 2025-01-02 12:07:33 | [diff] [blame] | 98 | return path.join( |
| 99 | rootPath(), |
| 100 | 'third_party', |
| 101 | 'devtools-frontend', |
| 102 | 'src', |
| 103 | 'third_party', |
| 104 | ); |
Wolfgang Beyer | 21656cb | 2023-09-04 16:38:13 | [diff] [blame] | 105 | } |
| 106 | return thirdPartyPath(); |
| 107 | } |
| 108 | |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 109 | function nodePath() { |
| 110 | const paths = { |
Joshua Thomas | e2625e3 | 2024-10-10 14:21:25 | [diff] [blame] | 111 | darwin: path.join( |
| 112 | process.arch === 'arm64' ? 'mac_arm64' : 'mac', |
Nikolay Vitkov | 55adf67 | 2025-01-02 12:07:33 | [diff] [blame] | 113 | process.arch === 'arm64' ? 'node-darwin-arm64' : 'node-darwin-x64', |
| 114 | 'bin', |
| 115 | 'node', |
| 116 | ), |
Nikolay Vitkov | 4731024 | 2024-09-04 10:00:07 | [diff] [blame] | 117 | linux: path.join('linux', 'node-linux-x64', 'bin', 'node'), |
| 118 | win32: path.join('win', 'node.exe'), |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 119 | }; |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 120 | return path.join(thirdPartyPath(), 'node', paths[os.platform()]); |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 121 | } |
| 122 | |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 123 | /** |
| 124 | * The path to the devtools-frontend node_modules folder. |
| 125 | */ |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 | [diff] [blame] | 126 | function nodeModulesPath() { |
| 127 | return path.join(devtoolsRootPath(), 'node_modules'); |
| 128 | } |
| 129 | |
Benedikt Meurer | edac67e | 2025-04-24 11:15:07 | [diff] [blame] | 130 | function autoninjaPyPath() { |
| 131 | return path.join(thirdPartyPath(), 'depot_tools', 'autoninja.py'); |
Benedikt Meurer | 8d2ec0b | 2025-04-03 09:43:24 | [diff] [blame] | 132 | } |
| 133 | |
Benedikt Meurer | edac67e | 2025-04-24 11:15:07 | [diff] [blame] | 134 | function vpython3ExecutablePath() { |
| 135 | return path.join(thirdPartyPath(), 'depot_tools', os.platform() === 'win32' ? 'vpython3.bat' : 'vpython3'); |
| 136 | } |
| 137 | |
| 138 | function gnPyPath() { |
| 139 | return path.join(thirdPartyPath(), 'depot_tools', 'gn.py'); |
Benedikt Meurer | 8d2ec0b | 2025-04-03 09:43:24 | [diff] [blame] | 140 | } |
| 141 | |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 142 | function stylelintExecutablePath() { |
| 143 | return path.join(nodeModulesPath(), 'stylelint', 'bin', 'stylelint.js'); |
| 144 | } |
| 145 | |
Jack Franklin | 29bb631 | 2021-03-16 09:27:17 | [diff] [blame] | 146 | function mochaExecutablePath() { |
| 147 | return path.join(nodeModulesPath(), 'mocha', 'bin', 'mocha'); |
| 148 | } |
| 149 | |
Danil Somsikov | 058b9d8 | 2024-10-07 11:22:19 | [diff] [blame] | 150 | function litAnalyzerExecutablePath() { |
| 151 | return path.join(nodeModulesPath(), 'lit-analyzer', 'cli.js'); |
| 152 | } |
| 153 | |
Benedikt Meurer | 0d4dbae | 2024-10-23 13:21:11 | [diff] [blame] | 154 | /** |
| 155 | * Computes the path to the toplevel `tsconfig.json`. |
| 156 | * |
| 157 | * @returns the path to the toplevel `tsconfig.json`. |
| 158 | */ |
| 159 | function tsconfigJsonPath() { |
Nikolay Vitkov | f8abe7d | 2025-05-15 10:06:01 | [diff] [blame^] | 160 | return path.join(devtoolsRootPath(), 'front_end', 'tsconfig.json'); |
Benedikt Meurer | 0d4dbae | 2024-10-23 13:21:11 | [diff] [blame] | 161 | } |
| 162 | |
Jack Franklin | 40b5fb6 | 2021-02-01 14:06:15 | [diff] [blame] | 163 | function downloadedChromeBinaryPath() { |
| 164 | const paths = { |
Nikolay Vitkov | 4731024 | 2024-09-04 10:00:07 | [diff] [blame] | 165 | linux: path.join('chrome-linux', 'chrome'), |
Nikolay Vitkov | 55adf67 | 2025-01-02 12:07:33 | [diff] [blame] | 166 | darwin: path.join( |
| 167 | 'chrome-mac', |
| 168 | 'Google Chrome for Testing.app', |
| 169 | 'Contents', |
| 170 | 'MacOS', |
| 171 | 'Google Chrome for Testing', |
| 172 | ), |
Nikolay Vitkov | 4731024 | 2024-09-04 10:00:07 | [diff] [blame] | 173 | win32: path.join('chrome-win', 'chrome.exe'), |
Jack Franklin | 40b5fb6 | 2021-02-01 14:06:15 | [diff] [blame] | 174 | }; |
Wolfgang Beyer | 21656cb | 2023-09-04 16:38:13 | [diff] [blame] | 175 | return path.join(devToolsThirdPartyPath(), 'chrome', paths[os.platform()]); |
Jack Franklin | 40b5fb6 | 2021-02-01 14:06:15 | [diff] [blame] | 176 | } |
| 177 | |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 178 | module.exports = { |
Benedikt Meurer | edac67e | 2025-04-24 11:15:07 | [diff] [blame] | 179 | autoninjaPyPath, |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 180 | devtoolsRootPath, |
Danil Somsikov | 058b9d8 | 2024-10-07 11:22:19 | [diff] [blame] | 181 | downloadedChromeBinaryPath, |
Eric Leese | 5f7e033 | 2025-03-27 16:23:47 | [diff] [blame] | 182 | isInChromiumDirectory, |
Benedikt Meurer | edac67e | 2025-04-24 11:15:07 | [diff] [blame] | 183 | gnPyPath, |
Benedikt Meurer | 0d4dbae | 2024-10-23 13:21:11 | [diff] [blame] | 184 | litAnalyzerExecutablePath, |
Nikolay Vitkov | 41c6911 | 2025-01-31 15:20:04 | [diff] [blame] | 185 | mochaExecutablePath, |
| 186 | nodeModulesPath, |
| 187 | nodePath, |
Benedikt Meurer | 6b1b791 | 2025-03-25 11:45:38 | [diff] [blame] | 188 | rootPath, |
Nikolay Vitkov | 41c6911 | 2025-01-31 15:20:04 | [diff] [blame] | 189 | stylelintExecutablePath, |
| 190 | thirdPartyPath, |
Benedikt Meurer | 0d4dbae | 2024-10-23 13:21:11 | [diff] [blame] | 191 | tsconfigJsonPath, |
Benedikt Meurer | edac67e | 2025-04-24 11:15:07 | [diff] [blame] | 192 | vpython3ExecutablePath, |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 193 | }; |