blob: ce242f5beca95c9a20809e6ef892e85fd6fd27c5 [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 *
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 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
Jack Franklinbc302342021-01-18 10:03:3037const _lookUpCaches = new Map(
38 [['chromium', null]],
39);
40/**
41 * This function figures out if we're within a chromium directory, and therefore
42 * we are in the integrated workflow mode, rather than working in a standalone
43 * devtools-frontend repository.
44 */
45function isInChromiumDirectory() {
46 const cached = _lookUpCaches.get('chromium');
47 if (cached) {
48 return cached;
49 }
50
Patrick Brosset5d02bbd2021-04-14 08:32:5051 const normalizedPath = PATH_TO_EXECUTED_FILE.split(path.sep).join('/');
Takuto Ikutaafe8b8e2022-01-26 02:42:0952 const devtoolsPath = 'src/third_party/devtools-frontend';
53 const isInChromium = normalizedPath.includes(devtoolsPath);
John Emau254c8dd2022-06-13 21:40:2454 const potentialChromiumDir = PATH_TO_EXECUTED_FILE.substring(0, normalizedPath.indexOf(devtoolsPath));
Jack Franklinbc302342021-01-18 10:03:3055 const result = {isInChromium, chromiumDirectory: potentialChromiumDir};
56 _lookUpCaches.set('chromium', result);
57 return result;
58}
59/**
60 * Returns the path to the root of the devtools-frontend repository.
61 *
62 * If we're in Chromium, this will be /path/to/chromium/src/third_party/devtools-frontend/src
63 * If it's standalone, it will be /path/to/devtools-frontend
64 */
65function devtoolsRootPath() {
Takuto Ikuta5bfd1022022-02-07 11:02:3266 return path.dirname(__dirname);
Jack Franklinbc302342021-01-18 10:03:3067}
68
69/**
70 * Returns the path to the root of the main repository we're in.
71 * if we're in Chromium, this is /path/to/chromium/src
72 * if we're in standalone, this is /path/to/devtools-frontend
73 *
74 * Note this is different to devtoolsRootPath(), which always returns the path
75 * to the devtools-frontend source code.
76 */
77function rootPath() {
78 const {isInChromium, chromiumDirectory} = isInChromiumDirectory();
79 if (isInChromium) {
80 return path.join(chromiumDirectory, 'src');
81 }
82 return devtoolsRootPath();
83}
84
85/**
86 * Path to the third_party directory. Used because if we're running in Chromium
87 * land we need to use e.g. the Node executable from Chromium's third_party
88 * directory, not from the devtools-frontend third_party directory.
89 */
Jack Franklin65c824b2021-01-14 14:34:2390function thirdPartyPath() {
Jack Franklinbc302342021-01-18 10:03:3091 return path.join(rootPath(), 'third_party');
Jack Franklin65c824b2021-01-14 14:34:2392}
93
94function nodePath() {
95 const paths = {
96 'darwin': path.join('mac', 'node-darwin-x64', 'bin', 'node'),
97 'linux': path.join('linux', 'node-linux-x64', 'bin', 'node'),
98 'win32': path.join('win', 'node.exe'),
99 };
Jack Franklinbc302342021-01-18 10:03:30100 return path.join(thirdPartyPath(), 'node', paths[os.platform()]);
Jack Franklin65c824b2021-01-14 14:34:23101}
102
Jack Franklinbc302342021-01-18 10:03:30103/**
104 * The path to the devtools-frontend node_modules folder.
105 */
Jack Franklin65c824b2021-01-14 14:34:23106function nodeModulesPath() {
107 return path.join(devtoolsRootPath(), 'node_modules');
108}
109
Jack Franklinbc302342021-01-18 10:03:30110function stylelintExecutablePath() {
111 return path.join(nodeModulesPath(), 'stylelint', 'bin', 'stylelint.js');
112}
113
Jack Franklin29bb6312021-03-16 09:27:17114function mochaExecutablePath() {
115 return path.join(nodeModulesPath(), 'mocha', 'bin', 'mocha');
116}
117
Jack Franklin40b5fb62021-02-01 14:06:15118function downloadedChromeBinaryPath() {
119 const paths = {
120 'linux': path.join('chrome-linux', 'chrome'),
Tim van der Lippe51257fc2021-02-24 12:08:09121 'darwin': path.join('chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium'),
Jack Franklin40b5fb62021-02-01 14:06:15122 'win32': path.join('chrome-win', 'chrome.exe'),
123 };
124 return path.join(thirdPartyPath(), 'chrome', paths[os.platform()]);
125}
126
Jack Franklinbc302342021-01-18 10:03:30127module.exports = {
128 thirdPartyPath,
129 nodePath,
130 devtoolsRootPath,
131 nodeModulesPath,
Jack Franklin29bb6312021-03-16 09:27:17132 mochaExecutablePath,
Jack Franklin40b5fb62021-02-01 14:06:15133 stylelintExecutablePath,
134 downloadedChromeBinaryPath
Jack Franklinbc302342021-01-18 10:03:30135};