UNPKG

6.49 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright (c) 2018, salesforce.com, inc.
4 * All rights reserved.
5 * SPDX-License-Identifier: BSD-3-Clause
6 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
7 */
8Object.defineProperty(exports, "__esModule", { value: true });
9const kit_1 = require("@salesforce/kit");
10const configAggregator_1 = require("./config/configAggregator");
11const configFile_1 = require("./config/configFile");
12const sfdxError_1 = require("./sfdxError");
13const internal_1 = require("./util/internal");
14const sfdc_1 = require("./util/sfdc");
15/**
16 * The sfdx-project.json config object. This file determines if a folder is a valid sfdx project.
17 *
18 * *Note:* Any non-standard (not owned by Salesforce) properties stored in sfdx-project.json should
19 * be in a top level property that represents your project or plugin.
20 *
21 * ```
22 * const project = await SfdxProjectJson.retrieve();
23 * const myPluginProperties = project.get('myplugin') || {};
24 * myPluginProperties.myprop = 'someValue';
25 * project.set('myplugin', myPluginProperties);
26 * await project.write();
27 * ```
28 *
29 * **See** [force:project:create](https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_ws_create_new.htm)
30 */
31class SfdxProjectJson extends configFile_1.ConfigFile {
32 static getFileName() {
33 return internal_1.SFDX_PROJECT_JSON;
34 }
35 static getDefaultOptions(isGlobal = false) {
36 const options = configFile_1.ConfigFile.getDefaultOptions(isGlobal, SfdxProjectJson.getFileName());
37 options.isState = false;
38 return options;
39 }
40 constructor(options) {
41 super(options);
42 }
43 async read() {
44 const contents = await super.read();
45 // Verify that the configObject does not have upper case keys; throw if it does. Must be heads down camel case.
46 const upperCaseKey = sfdc_1.sfdc.findUpperCaseKeys(this.toObject());
47 if (upperCaseKey) {
48 throw sfdxError_1.SfdxError.create('@salesforce/core', 'core', 'InvalidJsonCasing', [upperCaseKey, this.getPath()]);
49 }
50 return contents;
51 }
52 getDefaultOptions(options) {
53 const defaultOptions = {
54 isState: false
55 };
56 Object.assign(defaultOptions, options || {});
57 return defaultOptions;
58 }
59}
60exports.SfdxProjectJson = SfdxProjectJson;
61/**
62 * Represents an SFDX project directory. This directory contains a {@link SfdxProjectJson} config file as well as
63 * a hidden .sfdx folder that contains all the other local project config files.
64 *
65 * ```
66 * const project = await SfdxProject.resolve();
67 * const projectJson = await project.resolveProjectConfig();
68 * console.log(projectJson.sfdcLoginUrl);
69 * ```
70 */
71class SfdxProject {
72 /**
73 * Do not directly construct instances of this class -- use {@link SfdxProject.resolve} instead.
74 *
75 * @ignore
76 */
77 constructor(path) {
78 this.path = path;
79 }
80 /**
81 * Get a Project from a given path or from the working directory.
82 * @param path The path of the project.
83 *
84 * **Throws** *{@link SfdxError}{ name: 'InvalidProjectWorkspace' }* If the current folder is not located in a workspace.
85 */
86 static async resolve(path) {
87 return new SfdxProject(await this.resolveProjectPath(path));
88 }
89 /**
90 * Performs an upward directory search for an sfdx project file. Returns the absolute path to the project.
91 *
92 * @param dir The directory path to start traversing from.
93 *
94 * **Throws** *{@link SfdxError}{ name: 'InvalidProjectWorkspace' }* If the current folder is not located in a workspace.
95 *
96 * **See** {@link traverseForFile}
97 *
98 * **See** [process.cwd()](https://nodejs.org/api/process.html#process_process_cwd)
99 */
100 static async resolveProjectPath(dir) {
101 return internal_1.resolveProjectPath(dir);
102 }
103 /**
104 * Returns the project path.
105 */
106 getPath() {
107 return this.path;
108 }
109 /**
110 * Get the sfdx-project.json config. The global sfdx-project.json is used for user defaults
111 * that are not checked in to the project specific file.
112 *
113 * *Note:* When reading values from {@link SfdxProjectJson}, it is recommended to use
114 * {@link SfdxProject.resolveProjectConfig} instead.
115 *
116 * @param isGlobal True to get the global project file, otherwise the local project config.
117 */
118 async retrieveSfdxProjectJson(isGlobal = false) {
119 const options = SfdxProjectJson.getDefaultOptions(isGlobal);
120 if (isGlobal) {
121 if (!this.sfdxProjectJsonGlobal) {
122 this.sfdxProjectJsonGlobal = await SfdxProjectJson.create(options);
123 }
124 return this.sfdxProjectJsonGlobal;
125 }
126 else {
127 options.rootFolder = this.getPath();
128 if (!this.sfdxProjectJson) {
129 this.sfdxProjectJson = await SfdxProjectJson.create(options);
130 }
131 return this.sfdxProjectJson;
132 }
133 }
134 /**
135 * The project config is resolved from local and global {@link SfdxProjectJson},
136 * {@link ConfigAggregator}, and a set of defaults. It is recommended to use
137 * this when reading values from SfdxProjectJson.
138 * @returns A resolved config object that contains a bunch of different
139 * properties, including some 3rd party custom properties.
140 */
141 async resolveProjectConfig() {
142 if (!this.projectConfig) {
143 // Get sfdx-project.json from the ~/.sfdx directory to provide defaults
144 const global = await this.retrieveSfdxProjectJson(true);
145 const local = await this.retrieveSfdxProjectJson();
146 await global.read();
147 await local.read();
148 const defaultValues = {
149 sfdcLoginUrl: 'https://login.salesforce.com'
150 };
151 this.projectConfig = kit_1.defaults(local.toObject(), global.toObject(), defaultValues);
152 // Add fields in sfdx-config.json
153 Object.assign(this.projectConfig, (await configAggregator_1.ConfigAggregator.create()).getConfig());
154 // LEGACY - Allow override of sfdcLoginUrl via env var FORCE_SFDC_LOGIN_URL
155 if (process.env.FORCE_SFDC_LOGIN_URL) {
156 this.projectConfig.sfdcLoginUrl = process.env.FORCE_SFDC_LOGIN_URL;
157 }
158 }
159 return this.projectConfig;
160 }
161}
162exports.SfdxProject = SfdxProject;
163//# sourceMappingURL=sfdxProject.js.map
\No newline at end of file