Jack Franklin | 65c824b | 2021-01-14 14:34:23 +0000 | [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 | |
| 13 | const path = require('path'); |
| 14 | const os = require('os'); |
| 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 +0000 | [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 +0000 | [diff] [blame] | 22 | * |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [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 | * |
| 30 | * e.g. the user executes a script: scripts/test/run_lint_check_css.js |
| 31 | * |
| 32 | * process.argv[1] = |
| 33 | * /full/path/devtools-frontend/src/scripts/test/run_lint_check_css.js |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 +0000 | [diff] [blame] | 34 | */ |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 35 | const PATH_TO_EXECUTED_FILE = process.argv[1]; |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 +0000 | [diff] [blame] | 36 | |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 37 | function pathIsMostTopLevelPath(filePath) { |
| 38 | /** |
| 39 | * On Linux/Mac, if we do path.dirname(X) as many times as possible, it will |
| 40 | * eventually equal `/`. On Windows, it will end up equalling C:\, and |
| 41 | * path.dirname('C:\') === 'C:\', so we use that to figure out if we've made |
| 42 | * it as far up the tree as we can. |
| 43 | */ |
| 44 | return filePath === path.sep || path.dirname(filePath) === filePath; |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 47 | const _lookUpCaches = new Map( |
| 48 | [['chromium', null]], |
| 49 | ); |
| 50 | /** |
| 51 | * This function figures out if we're within a chromium directory, and therefore |
| 52 | * we are in the integrated workflow mode, rather than working in a standalone |
| 53 | * devtools-frontend repository. |
| 54 | */ |
| 55 | function isInChromiumDirectory() { |
| 56 | const cached = _lookUpCaches.get('chromium'); |
| 57 | if (cached) { |
| 58 | return cached; |
| 59 | } |
| 60 | |
Patrick Brosset | 5d02bbd | 2021-04-14 01:32:50 -0700 | [diff] [blame] | 61 | const normalizedPath = PATH_TO_EXECUTED_FILE.split(path.sep).join('/'); |
Takuto Ikuta | afe8b8e | 2022-01-26 11:42:09 +0900 | [diff] [blame] | 62 | const devtoolsPath = 'src/third_party/devtools-frontend'; |
| 63 | const isInChromium = normalizedPath.includes(devtoolsPath); |
| 64 | const potentialChromiumDir = PATH_TO_EXECUTED_FILE.substring(0, PATH_TO_EXECUTED_FILE.indexOf(devtoolsPath)); |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 65 | const result = {isInChromium, chromiumDirectory: potentialChromiumDir}; |
| 66 | _lookUpCaches.set('chromium', result); |
| 67 | return result; |
| 68 | } |
| 69 | /** |
| 70 | * Returns the path to the root of the devtools-frontend repository. |
| 71 | * |
| 72 | * If we're in Chromium, this will be /path/to/chromium/src/third_party/devtools-frontend/src |
| 73 | * If it's standalone, it will be /path/to/devtools-frontend |
| 74 | */ |
| 75 | function devtoolsRootPath() { |
| 76 | const nodeScriptFileThatIsBeingExecuted = PATH_TO_EXECUTED_FILE; |
| 77 | let devtoolsRootFolder = nodeScriptFileThatIsBeingExecuted; |
| 78 | while (path.basename(devtoolsRootFolder) !== 'devtools-frontend') { |
| 79 | devtoolsRootFolder = path.dirname(devtoolsRootFolder); |
| 80 | // We reached the end and can't find devtools-frontend. |
| 81 | if (pathIsMostTopLevelPath(devtoolsRootFolder)) { |
| 82 | throw new Error( |
| 83 | 'Could not find devtools-frontend in path. If you have cloned the repository to a different directory name, it will not work.'); |
| 84 | } |
| 85 | } |
| 86 | // In Chromium the path to the source code for devtools-frontend is: |
| 87 | // third_party/devtools-frontend/src |
| 88 | const {isInChromium} = isInChromiumDirectory(); |
| 89 | if (isInChromium) { |
| 90 | return path.join(devtoolsRootFolder, 'src'); |
| 91 | } |
| 92 | |
| 93 | // But if you're in a standalone repo it's just the devtools-frontend folder. |
| 94 | return devtoolsRootFolder; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns the path to the root of the main repository we're in. |
| 99 | * if we're in Chromium, this is /path/to/chromium/src |
| 100 | * if we're in standalone, this is /path/to/devtools-frontend |
| 101 | * |
| 102 | * Note this is different to devtoolsRootPath(), which always returns the path |
| 103 | * to the devtools-frontend source code. |
| 104 | */ |
| 105 | function rootPath() { |
| 106 | const {isInChromium, chromiumDirectory} = isInChromiumDirectory(); |
| 107 | if (isInChromium) { |
| 108 | return path.join(chromiumDirectory, 'src'); |
| 109 | } |
| 110 | return devtoolsRootPath(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Path to the third_party directory. Used because if we're running in Chromium |
| 115 | * land we need to use e.g. the Node executable from Chromium's third_party |
| 116 | * directory, not from the devtools-frontend third_party directory. |
| 117 | */ |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 +0000 | [diff] [blame] | 118 | function thirdPartyPath() { |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 119 | return path.join(rootPath(), 'third_party'); |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | function nodePath() { |
| 123 | const paths = { |
| 124 | 'darwin': path.join('mac', 'node-darwin-x64', 'bin', 'node'), |
| 125 | 'linux': path.join('linux', 'node-linux-x64', 'bin', 'node'), |
| 126 | 'win32': path.join('win', 'node.exe'), |
| 127 | }; |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 128 | return path.join(thirdPartyPath(), 'node', paths[os.platform()]); |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 131 | /** |
| 132 | * The path to the devtools-frontend node_modules folder. |
| 133 | */ |
Jack Franklin | 65c824b | 2021-01-14 14:34:23 +0000 | [diff] [blame] | 134 | function nodeModulesPath() { |
| 135 | return path.join(devtoolsRootPath(), 'node_modules'); |
| 136 | } |
| 137 | |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 138 | function stylelintExecutablePath() { |
| 139 | return path.join(nodeModulesPath(), 'stylelint', 'bin', 'stylelint.js'); |
| 140 | } |
| 141 | |
Jack Franklin | 29bb631 | 2021-03-16 09:27:17 +0000 | [diff] [blame] | 142 | function mochaExecutablePath() { |
| 143 | return path.join(nodeModulesPath(), 'mocha', 'bin', 'mocha'); |
| 144 | } |
| 145 | |
Jack Franklin | 40b5fb6 | 2021-02-01 14:06:15 +0000 | [diff] [blame] | 146 | function downloadedChromeBinaryPath() { |
| 147 | const paths = { |
| 148 | 'linux': path.join('chrome-linux', 'chrome'), |
Tim van der Lippe | 51257fc | 2021-02-24 12:08:09 +0000 | [diff] [blame] | 149 | 'darwin': path.join('chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium'), |
Jack Franklin | 40b5fb6 | 2021-02-01 14:06:15 +0000 | [diff] [blame] | 150 | 'win32': path.join('chrome-win', 'chrome.exe'), |
| 151 | }; |
| 152 | return path.join(thirdPartyPath(), 'chrome', paths[os.platform()]); |
| 153 | } |
| 154 | |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 155 | module.exports = { |
| 156 | thirdPartyPath, |
| 157 | nodePath, |
| 158 | devtoolsRootPath, |
| 159 | nodeModulesPath, |
Jack Franklin | 29bb631 | 2021-03-16 09:27:17 +0000 | [diff] [blame] | 160 | mochaExecutablePath, |
Jack Franklin | 40b5fb6 | 2021-02-01 14:06:15 +0000 | [diff] [blame] | 161 | stylelintExecutablePath, |
| 162 | downloadedChromeBinaryPath |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 163 | }; |