UNPKG

15.7 kBTypeScriptView Raw
1import { Dictionary, JsonMap, Nullable, Optional } from '@salesforce/ts-types';
2import { PackageDir, ProjectJson as ProjectJsonSchema, PackagePackageDir } from '@salesforce/schemas';
3import { ConfigFile } from './config/configFile';
4import { ConfigContents } from './config/configStackTypes';
5type NameAndFullPath = {
6 /**
7 * The [normalized](https://nodejs.org/api/path.html#path_path_normalize_path) path used as the package name.
8 */
9 name: string;
10 /**
11 * The absolute path of the package.
12 */
13 fullPath: string;
14};
15export type NamedPackagingDir = PackagePackageDir & NameAndFullPath;
16export type NamedPackageDir = PackageDir & NameAndFullPath;
17export type ProjectJson = ConfigContents & ProjectJsonSchema;
18/**
19 * The sfdx-project.json config object. This file determines if a folder is a valid sfdx project.
20 *
21 * *Note:* Any non-standard (not owned by Salesforce) properties stored in sfdx-project.json should
22 * be in a top level property that represents your project.
23 * Plugins should store their configuration @see SfProject.getPluginConfiguration and @see SfProject.setPluginConfiguration
24 *
25 * @example reading a standard property
26 * ```
27 * const project = await SfProject.resolve();
28 * const projectJson = await project.resolveProjectConfig();
29 * const namespace = projectJson.get('namespace');
30 * ```
31 *
32 * ```
33 * @example writing
34 * const project = await SfProject.resolve();
35 * const projectJson = await project.resolveProjectConfig();
36 * projectJson.set('namespace', 'new');
37 * await projectJson.write();
38 * ```
39 *
40 * **See** [force:project:create](https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_ws_create_new.htm)
41 */
42export declare class SfProjectJson extends ConfigFile<ConfigFile.Options, ProjectJson> {
43 /** json properties that are uppercase, or allow uppercase keys inside them */
44 static BLOCKLIST: string[];
45 static getFileName(): string;
46 static getDefaultOptions(isGlobal?: boolean): ConfigFile.Options;
47 read(): Promise<ProjectJson>;
48 /** force a reread of the project contents if you know they may have been modified */
49 refreshSync(): SfProjectJson;
50 readSync(): ProjectJson;
51 write(): Promise<ProjectJson>;
52 writeSync(): ProjectJson;
53 getDefaultOptions(options?: ConfigFile.Options): ConfigFile.Options;
54 /**
55 * Validates sfdx-project.json against the schema.
56 *
57 * Set the `SFDX_PROJECT_JSON_VALIDATION` environment variable to `true` to throw an error when schema validation fails.
58 * A warning is logged by default when the file is invalid.
59 *
60 * ***See*** [sfdx-project.schema.json] ((https://github.com/forcedotcom/schemas/blob/main/sfdx-project.schema.json)
61 */
62 schemaValidate(): Promise<void>;
63 /**
64 * Returns the `packageDirectories` within sfdx-project.json, first reading
65 * and validating the file if necessary.
66 */
67 getPackageDirectories(): Promise<PackageDir[]>;
68 /**
69 * Validates sfdx-project.json against the schema.
70 *
71 * Set the `SFDX_PROJECT_JSON_VALIDATION` environment variable to `true` to throw an error when schema validation fails.
72 * A warning is logged by default when the file is invalid.
73 *
74 * ***See*** [sfdx-project.schema.json] ((https://github.com/forcedotcom/schemas/blob/main/sfdx-project.schema.json)
75 */
76 schemaValidateSync(): void;
77 /**
78 * Returns a read-only list of `packageDirectories` within sfdx-project.json, first reading
79 * and validating the file if necessary. i.e. modifying this array will not affect the
80 * sfdx-project.json file.
81 */
82 getPackageDirectoriesSync(): NamedPackageDir[];
83 /**
84 * Returns a read-only list of `packageDirectories` within sfdx-project.json, first reading
85 * and validating the file if necessary. i.e. modifying this array will not affect the
86 * sfdx-project.json file.
87 *
88 * There can be multiple packages in packageDirectories that point to the same directory.
89 * This method only returns one packageDirectory entry per unique directory path. This is
90 * useful when doing source operations based on directories but probably not as useful
91 * for packaging operations that want to do something for each package entry.
92 */
93 getUniquePackageDirectories(): NamedPackageDir[];
94 /**
95 * Get a list of the unique package names from within sfdx-project.json. Use {@link SfProject.getUniquePackageDirectories}
96 * for data other than the names.
97 */
98 getUniquePackageNames(): string[];
99 /**
100 * Has package directories defined in the project.
101 */
102 hasPackages(): boolean;
103 /**
104 * Has multiple package directories (MPD) defined in the project.
105 */
106 hasMultiplePackages(): boolean;
107 /**
108 * Has at least one package alias defined in the project.
109 */
110 hasPackageAliases(): Promise<boolean>;
111 /**
112 * Get package aliases defined in the project.
113 */
114 getPackageAliases(): Nullable<Dictionary<string>>;
115 /**
116 * Add a package alias to the project.
117 * If the alias already exists, it will be overwritten.
118 *
119 * @param alias
120 * @param id
121 */
122 addPackageAlias(alias: string, id: string): void;
123 /**
124 * Add a package directory to the project.
125 * If the package directory already exists, the new directory
126 * properties will be merged with the existing properties.
127 *
128 * @param packageDir
129 */
130 addPackageDirectory(packageDir: PackageDir): void;
131 private doesPackageExist;
132 private validateKeys;
133}
134/**
135 * Represents an SFDX project directory. This directory contains a {@link SfProjectJson} config file as well as
136 * a hidden .sfdx folder that contains all the other local project config files.
137 *
138 * ```
139 * const project = await SfProject.resolve();
140 * const projectJson = await project.resolveProjectConfig();
141 * console.log(projectJson.sfdcLoginUrl);
142 * ```
143 */
144export declare class SfProject {
145 private path;
146 private static instances;
147 private projectConfig;
148 private sfProjectJson;
149 private sfProjectJsonGlobal;
150 private packageDirectories?;
151 private activePackage;
152 private packageAliases;
153 /**
154 * Do not directly construct instances of this class -- use {@link SfProject.resolve} instead.
155 *
156 * @ignore
157 */
158 protected constructor(path: string);
159 /**
160 * Clear the cache to force reading from disk.
161 *
162 * *NOTE: Only call this method if you must and you know what you are doing.*
163 */
164 static clearInstances(): void;
165 /**
166 * Get a Project from a given path or from the working directory.
167 *
168 * @param path The path of the project.
169 *
170 * **Throws** *{@link SfError}{ name: 'InvalidProjectWorkspaceError' }* If the current folder is not located in a workspace.
171 */
172 static resolve(path?: string): Promise<SfProject>;
173 /**
174 * Get a Project from a given path or from the working directory.
175 *
176 * @param path The path of the project.
177 *
178 * **Throws** *{@link SfError}{ name: 'InvalidProjectWorkspaceError' }* If the current folder is not located in a workspace.
179 */
180 static getInstance(path?: string): SfProject;
181 /**
182 * Performs an upward directory search for an sfdx project file. Returns the absolute path to the project.
183 *
184 * @param dir The directory path to start traversing from.
185 *
186 * **Throws** *{@link SfError}{ name: 'InvalidProjectWorkspaceError' }* If the current folder is not located in a workspace.
187 *
188 * **See** {@link traverseForFile}
189 *
190 * **See** [process.cwd()](https://nodejs.org/api/process.html#process_process_cwd)
191 */
192 static resolveProjectPath(dir?: string): Promise<string>;
193 /**
194 * Performs a synchronous upward directory search for an sfdx project file. Returns the absolute path to the project.
195 *
196 * @param dir The directory path to start traversing from.
197 *
198 * **Throws** *{@link SfError}{ name: 'InvalidProjectWorkspaceError' }* If the current folder is not located in a workspace.
199 *
200 * **See** {@link traverseForFileSync}
201 *
202 * **See** [process.cwd()](https://nodejs.org/api/process.html#process_process_cwd)
203 */
204 static resolveProjectPathSync(dir?: string): string;
205 /** shared method for resolve and getInstance.
206 * Cannot be a module-level function because instances is private */
207 private static getMemoizedInstance;
208 /**
209 * Returns the project path.
210 */
211 getPath(): string;
212 /**
213 * Get the sfdx-project.json config. The global sfdx-project.json is used for user defaults
214 * that are not checked in to the project specific file.
215 *
216 * *Note:* When reading values from {@link SfProjectJson}, it is recommended to use
217 * {@link SfProject.resolveProjectConfig} instead.
218 *
219 * @param isGlobal True to get the global project file, otherwise the local project config.
220 */
221 retrieveSfProjectJson(isGlobal?: boolean): Promise<SfProjectJson>;
222 /**
223 * Get the sfdx-project.json config. The global sfdx-project.json is used for user defaults
224 * that are not checked in to the project specific file.
225 *
226 * *Note:* When reading values from {@link SfProjectJson}, it is recommended to use
227 * {@link SfProject.resolveProjectConfig} instead.
228 *
229 * This is the sync method of {@link SfProject.resolveSfProjectJson}
230 *
231 * @param isGlobal True to get the global project file, otherwise the local project config.
232 */
233 getSfProjectJson(isGlobal?: boolean): SfProjectJson;
234 /**
235 * Returns a read-only list of `packageDirectories` within sfdx-project.json, first reading
236 * and validating the file if necessary. i.e. modifying this array will not affect the
237 * sfdx-project.json file.
238 */
239 getPackageDirectories(): NamedPackageDir[];
240 /**
241 * Returns a read-only list of `packageDirectories` within sfdx-project.json, first reading
242 * and validating the file if necessary. i.e. modifying this array will not affect the
243 * sfdx-project.json file.
244 *
245 * There can be multiple packages in packageDirectories that point to the same directory.
246 * This method only returns one packageDirectory entry per unique directory path. This is
247 * useful when doing source operations based on directories but probably not as useful
248 * for packaging operations that want to do something for each package entry.
249 */
250 getUniquePackageDirectories(): NamedPackageDir[];
251 /**
252 * Get a list of the unique package names from within sfdx-project.json. Use {@link SfProject.getUniquePackageDirectories}
253 * for data other than the names.
254 */
255 getUniquePackageNames(): string[];
256 /**
257 * Returns the package from a file path.
258 *
259 * @param path A file path. E.g. /Users/jsmith/projects/ebikes-lwc/force-app/apex/my-cls.cls
260 */
261 getPackageFromPath(path: string): Optional<NamedPackageDir>;
262 /**
263 * Returns the package name, E.g. 'force-app', from a file path.
264 *
265 * @param path A file path. E.g. /Users/jsmith/projects/ebikes-lwc/force-app/apex/my-cls.cls
266 */
267 getPackageNameFromPath(path: string): Optional<string>;
268 /**
269 * Returns the package directory.
270 *
271 * @param packageName Name of the package directory. E.g., 'force-app'
272 */
273 getPackage(packageName: string): Optional<NamedPackageDir>;
274 /**
275 * Returns the package directory.
276 *
277 * @param packageName Name of the package directory. E.g., 'force-app'
278 */
279 findPackage(predicate: (packageDir: NamedPackageDir) => boolean): Optional<NamedPackageDir>;
280 /**
281 * Returns the absolute path of the package directory ending with the path separator.
282 * E.g., /Users/jsmith/projects/ebikes-lwc/force-app/
283 *
284 * @param packageName Name of the package directory. E.g., 'force-app'
285 */
286 getPackagePath(packageName: string): Optional<string>;
287 /**
288 * Has package directories defined in the project.
289 */
290 hasPackages(): boolean;
291 /**
292 * Has multiple package directories (MPD) defined in the project.
293 */
294 hasMultiplePackages(): boolean;
295 /**
296 * Get the currently activated package on the project. This has no implication on sfdx-project.json
297 * but is useful for keeping track of package and source specific options in a process.
298 */
299 getActivePackage(): Nullable<NamedPackageDir>;
300 /**
301 * Set the currently activated package on the project. This has no implication on sfdx-project.json
302 * but is useful for keeping track of package and source specific options in a process.
303 *
304 * @param packageName The package name to activate. E.g. 'force-app'
305 */
306 setActivePackage(packageName: Nullable<string>): void;
307 /**
308 * Get the project's default package directory defined in sfdx-project.json using first 'default: true'
309 * found. The first entry is returned if no default is specified.
310 */
311 getDefaultPackage(): NamedPackageDir;
312 /**
313 * The project config is resolved from local and global {@link SfProjectJson},
314 * {@link ConfigAggregator}, and a set of defaults. It is recommended to use
315 * this when reading values from SfProjectJson.
316 *
317 * The global {@link SfProjectJson} is used to allow the user to provide default values they
318 * may not want checked into their project's source.
319 *
320 * @returns A resolved config object that contains a bunch of different
321 * properties, including some 3rd party custom properties.
322 */
323 resolveProjectConfig(): Promise<JsonMap>;
324 hasPackageAliases(): Promise<boolean>;
325 /**
326 * Returns a read-only list of `packageDirectories` within sfdx-project.json, first reading
327 * and validating the file if necessary. i.e. modifying this array will not affect the
328 * sfdx-project.json file.
329 */
330 getPackageAliases(): Nullable<Dictionary<string>>;
331 getPackageIdFromAlias(alias: string): Optional<string>;
332 getAliasesFromPackageId(id: string): string[];
333 /**
334 * retrieve the configuration for a named plugin from sfdx-project.json.plugins.pluginName
335 *
336 * @example
337 * ```
338 * const project = await SfProject.resolve();
339 * const pluginConfig = await project.getPluginConfiguration('myPlugin');
340 * ```
341 *
342 * optionally pass a type parameter for your plugin configuration's schema
343 * */
344 getPluginConfiguration<T extends Record<string, unknown>>(pluginName: string): Promise<Readonly<T>>;
345 /**
346 * set the configuration for a named plugin from sfdx-project.json.plugins.pluginName, overwriting existing configuration
347 *
348 * @example
349 * ```
350 * const project = await SfProject.resolve();
351 * const pluginConfig = await project.setPluginConfiguration('myPlugin', {foo: 'bar', myLimit: 25});
352 * ```
353 *
354 * optionally pass a type parameter for your plugin configuration's schema
355 * */
356 setPluginConfiguration<T extends Record<string, unknown>>(pluginName: string, config: T): Promise<void>;
357}
358/** differentiate between the Base PackageDir (path, maybe default) and the Packaging version (package and maybe a LOT of other fields) by whether is has the `package` property */
359export declare const isPackagingDirectory: (packageDir: PackageDir) => packageDir is PackagePackageDir;
360/** differentiate between the Base PackageDir (path, maybe default) and the Packaging version (package and maybe a LOT of other fields) by whether is has the `package` property */
361export declare const isNamedPackagingDirectory: (packageDir: NamedPackageDir) => packageDir is NamedPackagingDir;
362export {};