blob: 9d0887c51330a9a797d4b09e7b6449bf5602d837 [file] [log] [blame]
Jack Franklin65c824b2021-01-14 14:34:231// 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
13const path = require('path');
14const os = require('os');
15
16/**
17 * You would think we can use __filename here but we cannot because __filename
Jack Franklinbc302342021-01-18 10:03:3018 * 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 Franklin65c824b2021-01-14 14:34:2322 *
Jack Franklinbc302342021-01-18 10:03:3023 * 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 *
Benedikt Meurere6a39dc2024-08-19 15:28:2630 * e.g. the user executes a script: scripts/test/run_lint_check.js
Jack Franklinbc302342021-01-18 10:03:3031 *
32 * process.argv[1] =
Benedikt Meurere6a39dc2024-08-19 15:28:2633 * /full/path/devtools-frontend/src/scripts/test/run_lint_check.js
Jack Franklin65c824b2021-01-14 14:34:2334 */
Jack Franklinbc302342021-01-18 10:03:3035const PATH_TO_EXECUTED_FILE = process.argv[1];
Jack Franklin65c824b2021-01-14 14:34:2336
Nikolay Vitkov55adf672025-01-02 12:07:3337const _lookUpCaches = new Map([['chromium', null]]);
Jack Franklinbc302342021-01-18 10:03:3038/**
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 */
43function isInChromiumDirectory() {
44 const cached = _lookUpCaches.get('chromium');
45 if (cached) {
46 return cached;
47 }
48
Patrick Brosset5d02bbd2021-04-14 08:32:5049 const normalizedPath = PATH_TO_EXECUTED_FILE.split(path.sep).join('/');
Takuto Ikutaafe8b8e2022-01-26 02:42:0950 const devtoolsPath = 'src/third_party/devtools-frontend';
51 const isInChromium = normalizedPath.includes(devtoolsPath);
Nikolay Vitkov55adf672025-01-02 12:07:3352 const potentialChromiumDir = PATH_TO_EXECUTED_FILE.substring(
53 0,
54 normalizedPath.indexOf(devtoolsPath),
55 );
Jack Franklinbc302342021-01-18 10:03:3056 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 */
66function devtoolsRootPath() {
Takuto Ikuta5bfd1022022-02-07 11:02:3267 return path.dirname(__dirname);
Jack Franklinbc302342021-01-18 10:03:3068}
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 */
78function rootPath() {
79 const {isInChromium, chromiumDirectory} = isInChromiumDirectory();
80 if (isInChromium) {
81 return path.join(chromiumDirectory, 'src');
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 Franklin65c824b2021-01-14 14:34:2391function thirdPartyPath() {
Jack Franklinbc302342021-01-18 10:03:3092 return path.join(rootPath(), 'third_party');
Jack Franklin65c824b2021-01-14 14:34:2393}
94
Wolfgang Beyer21656cb2023-09-04 16:38:1395function devToolsThirdPartyPath() {
96 const {isInChromium} = isInChromiumDirectory();
97 if (isInChromium) {
Nikolay Vitkov55adf672025-01-02 12:07:3398 return path.join(
99 rootPath(),
100 'third_party',
101 'devtools-frontend',
102 'src',
103 'third_party',
104 );
Wolfgang Beyer21656cb2023-09-04 16:38:13105 }
106 return thirdPartyPath();
107}
108
Jack Franklin65c824b2021-01-14 14:34:23109function nodePath() {
110 const paths = {
Joshua Thomase2625e32024-10-10 14:21:25111 darwin: path.join(
112 process.arch === 'arm64' ? 'mac_arm64' : 'mac',
Nikolay Vitkov55adf672025-01-02 12:07:33113 process.arch === 'arm64' ? 'node-darwin-arm64' : 'node-darwin-x64',
114 'bin',
115 'node',
116 ),
Nikolay Vitkov47310242024-09-04 10:00:07117 linux: path.join('linux', 'node-linux-x64', 'bin', 'node'),
118 win32: path.join('win', 'node.exe'),
Jack Franklin65c824b2021-01-14 14:34:23119 };
Jack Franklinbc302342021-01-18 10:03:30120 return path.join(thirdPartyPath(), 'node', paths[os.platform()]);
Jack Franklin65c824b2021-01-14 14:34:23121}
122
Jack Franklinbc302342021-01-18 10:03:30123/**
124 * The path to the devtools-frontend node_modules folder.
125 */
Jack Franklin65c824b2021-01-14 14:34:23126function nodeModulesPath() {
127 return path.join(devtoolsRootPath(), 'node_modules');
128}
129
Jack Franklinbc302342021-01-18 10:03:30130function stylelintExecutablePath() {
131 return path.join(nodeModulesPath(), 'stylelint', 'bin', 'stylelint.js');
132}
133
Jack Franklin29bb6312021-03-16 09:27:17134function mochaExecutablePath() {
135 return path.join(nodeModulesPath(), 'mocha', 'bin', 'mocha');
136}
137
Danil Somsikov058b9d82024-10-07 11:22:19138function litAnalyzerExecutablePath() {
139 return path.join(nodeModulesPath(), 'lit-analyzer', 'cli.js');
140}
141
Benedikt Meurer0d4dbae2024-10-23 13:21:11142/**
143 * Computes the path to the toplevel `tsconfig.json`.
144 *
145 * @returns the path to the toplevel `tsconfig.json`.
146 */
147function tsconfigJsonPath() {
148 return path.join(devtoolsRootPath(), 'tsconfig.json');
149}
150
Jack Franklin40b5fb62021-02-01 14:06:15151function downloadedChromeBinaryPath() {
152 const paths = {
Nikolay Vitkov47310242024-09-04 10:00:07153 linux: path.join('chrome-linux', 'chrome'),
Nikolay Vitkov55adf672025-01-02 12:07:33154 darwin: path.join(
155 'chrome-mac',
156 'Google Chrome for Testing.app',
157 'Contents',
158 'MacOS',
159 'Google Chrome for Testing',
160 ),
Nikolay Vitkov47310242024-09-04 10:00:07161 win32: path.join('chrome-win', 'chrome.exe'),
Jack Franklin40b5fb62021-02-01 14:06:15162 };
Wolfgang Beyer21656cb2023-09-04 16:38:13163 return path.join(devToolsThirdPartyPath(), 'chrome', paths[os.platform()]);
Jack Franklin40b5fb62021-02-01 14:06:15164}
165
Jack Franklinbc302342021-01-18 10:03:30166module.exports = {
167 thirdPartyPath,
168 nodePath,
169 devtoolsRootPath,
170 nodeModulesPath,
Jack Franklin29bb6312021-03-16 09:27:17171 mochaExecutablePath,
Jack Franklin40b5fb62021-02-01 14:06:15172 stylelintExecutablePath,
Danil Somsikov058b9d82024-10-07 11:22:19173 downloadedChromeBinaryPath,
Benedikt Meurer0d4dbae2024-10-23 13:21:11174 litAnalyzerExecutablePath,
175 tsconfigJsonPath,
Jack Franklinbc302342021-01-18 10:03:30176};