UNPKG

6.28 kBTypeScriptView Raw
1import { AsyncOptionalCreatable } from '@salesforce/kit';
2import { AnyJson, JsonMap, Optional } from '@salesforce/ts-types';
3import { Config } from './config';
4/**
5 * Information about a config property.
6 */
7export interface ConfigInfo {
8 /**
9 * key The config key.
10 */
11 key: string;
12 /**
13 * The location of the config property.
14 */
15 location?: ConfigAggregator.Location;
16 /**
17 * The config value.
18 */
19 value?: AnyJson;
20 /**
21 * The path of the config value.
22 */
23 path?: string;
24 /**
25 * `true` if the config property is in the local project.
26 */
27 isLocal: () => boolean;
28 /**
29 * `true` if the config property is in the global space.
30 */
31 isGlobal: () => boolean;
32 /**
33 * `true` if the config property is an environment variable.
34 */
35 isEnvVar: () => boolean;
36}
37/**
38 * Aggregate global and local project config files, as well as environment variables for
39 * `sfdx-config.json`. The resolution happens in the following bottom-up order:
40 *
41 * 1. Environment variables (`SFDX_LOG_LEVEL`)
42 * 1. Workspace settings (`<workspace-root>/.sfdx/sfdx-config.json`)
43 * 1. Global settings (`$HOME/.sfdx/sfdx-config.json`)
44 *
45 * Use {@link ConfigAggregator.create} to instantiate the aggregator.
46 *
47 * ```
48 * const aggregator = await ConfigAggregator.create();
49 * console.log(aggregator.getPropertyValue('defaultusername'));
50 * ```
51 */
52export declare class ConfigAggregator extends AsyncOptionalCreatable<JsonMap> {
53 private static instance;
54 private static encrypted;
55 private allowedProperties;
56 private localConfig?;
57 private globalConfig;
58 private envVars;
59 private get config();
60 /**
61 * **Do not directly construct instances of this class -- use {@link ConfigAggregator.create} instead.**
62 *
63 * @ignore
64 */
65 constructor(options?: JsonMap);
66 /**
67 * Get the static ConfigAggregator instance. If one doesn't exist, one will be created with
68 * the **encrypted** config values. Encrypted config values need to be resolved
69 * asynchronously by calling {@link ConfigAggregator.reload}
70 */
71 static getInstance<P, T extends AsyncOptionalCreatable<P>>(this: new () => T): T;
72 static create<P, T extends AsyncOptionalCreatable<P>>(this: new (options?: P) => T, options?: P): Promise<T>;
73 /**
74 * Get the info for a given key. If the ConfigAggregator was not asynchronously created OR
75 * the {@link ConfigAggregator.reload} was not called, the config value may be encrypted.
76 *
77 * @param key The config key.
78 */
79 static getValue(key: string): ConfigInfo;
80 /**
81 * Initialize this instances async dependencies.
82 */
83 init(): Promise<void>;
84 /**
85 * Get a resolved config property.
86 *
87 * **Throws** *{@link SfdxError}{ name: 'UnknownConfigKey' }* An attempt to get a property that's not supported.
88 *
89 * @param key The key of the property.
90 */
91 getPropertyValue(key: string): Optional<AnyJson>;
92 /**
93 * Get a resolved config property.
94 *
95 * @param key The key of the property.
96 */
97 getInfo(key: string): ConfigInfo;
98 /**
99 * Gets a resolved config property location.
100 *
101 * For example, `getLocation('logLevel')` will return:
102 * 1. `Location.GLOBAL` if resolved to an environment variable.
103 * 1. `Location.LOCAL` if resolved to local project config.
104 * 1. `Location.ENVIRONMENT` if resolved to the global config.
105 *
106 * @param key The key of the property.
107 */
108 getLocation(key: string): Optional<ConfigAggregator.Location>;
109 /**
110 * Get a resolved file path or environment variable name of the property.
111 *
112 * For example, `getPath('logLevel')` will return:
113 * 1. `$SFDX_LOG_LEVEL` if resolved to an environment variable.
114 * 1. `./.sfdx/sfdx-config.json` if resolved to the local config.
115 * 1. `~/.sfdx/sfdx-config.json` if resolved to the global config.
116 * 1. `undefined`, if not resolved.
117 *
118 * **Note:** that the path returned may be the absolute path instead of
119 * relative paths such as `./` and `~/`.
120 *
121 * @param key The key of the property.
122 */
123 getPath(key: string): Optional<string>;
124 /**
125 * Get all resolved config property keys, values, locations, and paths.
126 *
127 * ```
128 * > console.log(aggregator.getConfigInfo());
129 * [
130 * { key: 'logLevel', val: 'INFO', location: 'Environment', path: '$SFDX_LOG_LEVEL'}
131 * { key: 'defaultusername', val: '<username>', location: 'Local', path: './.sfdx/sfdx-config.json'}
132 * ]
133 * ```
134 */
135 getConfigInfo(): ConfigInfo[];
136 /**
137 * Get the local project config instance.
138 */
139 getLocalConfig(): Config | undefined;
140 /**
141 * Get the global config instance.
142 */
143 getGlobalConfig(): Config;
144 /**
145 * Get the resolved config object from the local, global and environment config instances.
146 */
147 getConfig(): JsonMap;
148 /**
149 * Get the config properties that are environment variables.
150 */
151 getEnvVars(): Map<string, string>;
152 /**
153 * Re-read all property configurations from disk.
154 */
155 reload(): Promise<ConfigAggregator>;
156 /**
157 * Loads all the properties and aggregates them according to location.
158 */
159 private loadProperties;
160 /**
161 * Loads all the properties and aggregates them according to location.
162 */
163 private loadPropertiesSync;
164 private resolveProperties;
165 /**
166 * Get the allowed properties.
167 */
168 private getAllowedProperties;
169 /**
170 * Set the allowed properties.
171 *
172 * @param properties The properties to set.
173 */
174 private setAllowedProperties;
175 /**
176 * Sets the env variables.
177 *
178 * @param envVars The env variables to set.
179 */
180 private setEnvVars;
181}
182export declare namespace ConfigAggregator {
183 /**
184 * An enum of all possible locations for a config value.
185 */
186 const enum Location {
187 /**
188 * Represents the global config.
189 */
190 GLOBAL = "Global",
191 /**
192 * Represents the local project config.
193 */
194 LOCAL = "Local",
195 /**
196 * Represents environment variables.
197 */
198 ENVIRONMENT = "Environment"
199 }
200}
201
\No newline at end of file