UNPKG

4.47 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright (c) 2020, salesforce.com, inc.
4 * All rights reserved.
5 * Licensed under the BSD 3-Clause license.
6 * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
7 */
8Object.defineProperty(exports, "__esModule", { value: true });
9exports.traverse = exports.resolveProjectPathSync = exports.resolveProjectPath = exports.SFDX_PROJECT_JSON = void 0;
10const fs = require("fs");
11const path_1 = require("path");
12const messages_1 = require("../messages");
13messages_1.Messages.importMessagesDirectory(__dirname);
14const messages = messages_1.Messages.load('@salesforce/core', 'config', ['invalidProjectWorkspace']);
15/**
16 * The name of the project config file.
17 *
18 * @ignore
19 */
20// This has to be defined on util to prevent circular deps with project and configFile.
21exports.SFDX_PROJECT_JSON = 'sfdx-project.json';
22/**
23 * Performs an upward directory search for an sfdx project file. Returns the absolute path to the project.
24 *
25 * **See** {@link SFDX_PROJECT_JSON}
26 *
27 * **See** {@link traverseForFile}
28 *
29 * **Throws** *{@link SfError}{ name: 'InvalidProjectWorkspaceError' }* If the current folder is not located in a workspace.
30 *
31 * @param dir The directory path to start traversing from.
32 * @ignore
33 */
34async function resolveProjectPath(dir = process.cwd()) {
35 const projectPath = await exports.traverse.forFile(dir, exports.SFDX_PROJECT_JSON);
36 if (!projectPath) {
37 throw messages.createError('invalidProjectWorkspace');
38 }
39 return projectPath;
40}
41exports.resolveProjectPath = resolveProjectPath;
42/**
43 * Performs a synchronous upward directory search for an sfdx project file. Returns the absolute path to the project.
44 *
45 * **See** {@link SFDX_PROJECT_JSON}
46 *
47 * **See** {@link traverseForFile}
48 *
49 * **Throws** *{@link SfError}{ name: 'InvalidProjectWorkspaceError' }* If the current folder is not located in a workspace.
50 *
51 * @param dir The directory path to start traversing from.
52 * @ignore
53 */
54function resolveProjectPathSync(dir = process.cwd()) {
55 const projectPath = exports.traverse.forFileSync(dir, exports.SFDX_PROJECT_JSON);
56 if (!projectPath) {
57 throw messages.createError('invalidProjectWorkspace');
58 }
59 return projectPath;
60}
61exports.resolveProjectPathSync = resolveProjectPathSync;
62/**
63 * These methods were moved from the deprecated 'fs' module in v2 and are only used in sfdx-core above
64 *
65 * They were migrated into the 'traverse' constant in order to stub them in unit tests
66 */
67exports.traverse = {
68 /**
69 * Searches a file path in an ascending manner (until reaching the filesystem root) for the first occurrence a
70 * specific file name. Resolves with the directory path containing the located file, or `null` if the file was
71 * not found.
72 *
73 * @param dir The directory path in which to start the upward search.
74 * @param file The file name to look for.
75 */
76 forFile: async (dir, file) => {
77 let foundProjectDir;
78 try {
79 fs.statSync((0, path_1.join)(dir, file));
80 foundProjectDir = dir;
81 }
82 catch (err) {
83 if (err && err.code === 'ENOENT') {
84 const nextDir = (0, path_1.resolve)(dir, '..');
85 if (nextDir !== dir) {
86 // stop at root
87 foundProjectDir = await exports.traverse.forFile(nextDir, file);
88 }
89 }
90 }
91 return foundProjectDir;
92 },
93 /**
94 * Searches a file path synchronously in an ascending manner (until reaching the filesystem root) for the first occurrence a
95 * specific file name. Resolves with the directory path containing the located file, or `null` if the file was
96 * not found.
97 *
98 * @param dir The directory path in which to start the upward search.
99 * @param file The file name to look for.
100 */
101 forFileSync: (dir, file) => {
102 let foundProjectDir;
103 try {
104 fs.statSync((0, path_1.join)(dir, file));
105 foundProjectDir = dir;
106 }
107 catch (err) {
108 if (err && err.code === 'ENOENT') {
109 const nextDir = (0, path_1.resolve)(dir, '..');
110 if (nextDir !== dir) {
111 // stop at root
112 foundProjectDir = exports.traverse.forFileSync(nextDir, file);
113 }
114 }
115 }
116 return foundProjectDir;
117 },
118};
119//# sourceMappingURL=internal.js.map
\No newline at end of file