UNPKG

4.95 kBTypeScriptView Raw
1import { Dictionary, JsonMap, Optional } from '@salesforce/ts-types';
2import { ConfigFile } from './configFile';
3import { ConfigContents, ConfigEntry, ConfigValue } from './configStore';
4/**
5 * A config file that stores config values in groups. e.g. to store different config
6 * values for different commands, without having manually manipulate the config.
7 *
8 * **Note:** All config methods are overwritten to use the {@link ConfigGroup.setDefaultGroup}.
9 *
10 * ```
11 * class MyPluginConfig extends ConfigGroup<ConfigGroup.Options> {
12 * public static getFileName(): string {
13 * return 'myPluginConfigFilename.json';
14 * }
15 * }
16 * const myConfig = await MyPluginConfig.create(ConfigGroup.getOptions('all'));
17 * myConfig.setDefaultGroup('myCommand'); // Can be set in your command's init.
18 * myConfig.set('mykey', 'myvalue'); // Sets 'myKey' for the 'myCommand' group.
19 * myConfig.setInGroup('myKey', 'myvalue', 'all'); // Manually set in another group.
20 * await myConfig.write();
21 * ```
22 */
23export declare class ConfigGroup<T extends ConfigGroup.Options> extends ConfigFile<T> {
24 protected defaultGroup: string;
25 /**
26 * Get ConfigGroup specific options, such as the default group.
27 *
28 * @param defaultGroup The default group to use when creating the config.
29 * @param filename The filename of the config file. Uses the static {@link getFileName} by default.
30 */
31 static getOptions(defaultGroup: string, filename?: string): ConfigGroup.Options;
32 /**
33 * Sets the default group for all {@link BaseConfigStore} methods to use.
34 * **Throws** *{@link SfdxError}{ name: 'MissingGroupName' }* The group parameter is null or undefined.
35 *
36 * @param group The group.
37 */
38 setDefaultGroup(group: string): void;
39 /**
40 * Set a group of entries in a bulk save. Returns The new properties that were saved.
41 *
42 * @param newEntries An object representing the aliases to set.
43 * @param group The group the property belongs to.
44 */
45 updateValues(newEntries: Dictionary<ConfigValue>, group?: string): Promise<Dictionary<ConfigValue>>;
46 /**
47 * Set a value on a group. Returns the promise resolved when the value is set.
48 *
49 * @param key The key.
50 * @param value The value.
51 * @param group The group.
52 */
53 updateValue(key: string, value: ConfigValue, group?: string): Promise<void>;
54 /**
55 * Gets an array of key value pairs.
56 */
57 entries(): ConfigEntry[];
58 /**
59 * Returns a specified element from ConfigGroup. Returns the associated value.
60 *
61 * @param key The key.
62 */
63 get(key: string): Optional<ConfigValue>;
64 /**
65 * Returns a boolean if an element with the specified key exists in the default group.
66 *
67 * @param {string} key The key.
68 */
69 has(key: string): boolean;
70 /**
71 * Returns an array of the keys from the default group.
72 */
73 keys(): string[];
74 /**
75 * Returns an array of the values from the default group.
76 */
77 values(): ConfigValue[];
78 /**
79 * Add or updates an element with the specified key in the default group.
80 *
81 * @param key The key.
82 * @param value The value.
83 */
84 set(key: string, value: ConfigValue): ConfigContents;
85 /**
86 * Removes an element with the specified key from the default group. Returns `true` if the item was deleted.
87 *
88 * @param key The key.
89 */
90 unset(key: string): boolean;
91 /**
92 * Remove all key value pairs from the default group.
93 */
94 clear(): void;
95 /**
96 * Get all config contents for a group.
97 *
98 * @param {string} [group = 'default'] The group.
99 */
100 getGroup(group?: string): Optional<ConfigContents>;
101 /**
102 * Returns the value associated to the key and group, or undefined if there is none.
103 *
104 * @param key The key.
105 * @param group The group. Defaults to the default group.
106 */
107 getInGroup(key: string, group?: string): Optional<ConfigValue>;
108 /**
109 * Convert the config object to a json object.
110 */
111 toObject(): JsonMap;
112 /**
113 * Convert an object to a {@link ConfigContents} and set it as the config contents.
114 *
115 * @param {object} obj The object.
116 */
117 setContentsFromObject<U extends object>(obj: U): void;
118 /**
119 * Sets the value for the key and group in the config object.
120 *
121 * @param key The key.
122 * @param value The value.
123 * @param group The group. Uses the default group if not specified.
124 */
125 setInGroup(key: string, value?: ConfigValue, group?: string): ConfigContents;
126 /**
127 * Initialize the asynchronous dependencies.
128 */
129 init(): Promise<void>;
130}
131export declare namespace ConfigGroup {
132 /**
133 * Options when creating the config file.
134 */
135 interface Options extends ConfigFile.Options {
136 /**
137 * The default group for properties to go into.
138 */
139 defaultGroup?: string;
140 }
141}