blob: 0f0b4332722a974fbcbc70fc630ba8d0f5336642 [file] [log] [blame]
Jack Franklin65c824b2021-01-14 14:34:23 +00001// 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
18 * has any symlinks resolved. This means we can't use it to tell if the user
19 * is using the external repo with a standalone build setup because the
20 * symlink from chromium/src/third_party/devtools-frontend =>
21 * devtools-frontend repo gets resolved by Node before it gives us __filename.
22 *
23 * Instead we can use process.argv, whose first two arguments are the path to
24 * the Node binary, and then the path to the file being executed, but without
25 * symlinks being resolved. So if this script gets run in the Chromium dir
26 * through a symlink, the path will still contain
27 * /path/to/chromium/src/third-party/devtools-frontend/scripts/... - this is
28 * NOT the case if we were to use __filename.
29 */
30const ABS_PATH_TO_CURRENT_FILE = process.argv[1];
31
32/** Find the root path of the checkout.
33* In the Chromium repository, this is the src/chromium directory.
34* In the external repository, standalone build, this is the devtools-frontend directory.
35* In the external repository, integrated build, this is the src/chromium directory.
36*/
37function rootPath() {
38 const scriptsPath = path.dirname(ABS_PATH_TO_CURRENT_FILE);
39 const devtoolsFrontendPath = path.dirname(scriptsPath);
40 const devtoolsFrontendParentPath = path.dirname(devtoolsFrontendPath);
41
42 if (path.basename(devtoolsFrontendParentPath) === 'devtools-frontend') {
43 // External repository, integrated build
44 // So go up two levels to the src/chromium directory
45 return path.dirname(path.dirname(devtoolsFrontendParentPath));
46 }
47
48 // External repository, standalone build
49 return devtoolsFrontendPath;
50}
51
52function thirdPartyPath() {
53 path.join(rootPath(), 'third_party');
54}
55
56function nodePath() {
57 const paths = {
58 'darwin': path.join('mac', 'node-darwin-x64', 'bin', 'node'),
59 'linux': path.join('linux', 'node-linux-x64', 'bin', 'node'),
60 'win32': path.join('win', 'node.exe'),
61 };
62 return path.join(thirdPartyPath(), 'node', paths[os.platform]);
63}
64
65function devtoolsRootPath() {
66 return path.dirname(path.dirname(ABS_PATH_TO_CURRENT_FILE));
67}
68
69function nodeModulesPath() {
70 return path.join(devtoolsRootPath(), 'node_modules');
71}
72
73export {thirdPartyPath, nodePath, devtoolsRootPath, nodeModulesPath};