-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
266 lines (231 loc) · 6.19 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import {
dirname,
resolve,
} from "https://deno.land/[email protected]/path/mod.ts";
import { appPaths } from "https://raw.githubusercontent.com/truestamp/deno-app-paths/v1.1.0/mod.ts";
const plainObject = () => Object.create(null);
// Recursive JSON type: https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/#more-recursive-type-aliases
export type Json =
| string
| number
| boolean
| null
| Json[]
| { [key: string]: Json }
export type StoreType = Record<string, Json>;
export interface ConfigParameters {
projectName: string;
configName?: string;
resetInvalidConfig?: boolean;
defaults?: StoreType | null;
}
export default class Config {
private _options: ConfigParameters = {
projectName: "",
configName: "config",
resetInvalidConfig: false,
defaults: null,
};
defaultValues: StoreType = plainObject();
path: string;
constructor (options: ConfigParameters) {
this._options = {
...this._options,
...options,
};
// Were `defaults` provided?
this.defaultValues = this._options.defaults
? this._options.defaults
: plainObject();
if (!this._options.projectName || this._options.projectName.trim() === "") {
throw new Error("the projectName option must be provided and non-empty");
}
this._options.projectName = this._options.projectName.trim();
this.path = resolve(
appPaths(this._options.projectName).config,
`${this._options.configName}.json`,
);
}
// accessor properties (getter/setter)
/**
* Get the number of config items stored.
* @returns {number} The count of config items
*/
get size(): number {
return Object.keys(this.store).length;
}
/**
* Get the path of the config directory.
* @returns {string} The directory portion of the config path
*/
get dir(): string {
return dirname(this.path);
}
/**
* Get the contents of the config store, including defaults if present.
* @returns {StoreType} The config store
*/
get store(): StoreType {
try {
return {
...this.defaultValues,
...JSON.parse(Deno.readTextFileSync(this.path)),
};
} catch (error) {
switch (error.name) {
case "SyntaxError":
// Unable to read the JSON file. Reset it to defaults if that is the
// desired behavior.
if (this._options.resetInvalidConfig) {
this.reset();
return { ...this.defaultValues };
}
break;
case "NotFound":
return { ...this.defaultValues, ...plainObject() };
}
throw error;
}
}
/**
* Set the contents of the config store to an Object.
*
* @param {StoreType} data
* @returns {void}
*/
set store(data: StoreType) {
Deno.mkdirSync(dirname(this.path), { recursive: true });
Deno.writeTextFileSync(this.path, JSON.stringify(data, null, 2));
}
/**
* Get the config store parameters.
*
* @returns {ConfigParameters}
*/
get options(): ConfigParameters {
return this._options;
}
/**
* Returns boolean whether `key` is present in the config store.
*
* @param {string} key The key to search for.
* @returns {boolean} Key exists in config store?
*/
has(key: string): boolean {
return key in this.store;
}
//
/**
* Destructively removes any existing config file and resets all
* keys to defaults if present, writing them to a new config.
*
* If no defaults are present no new config file will be created.
*
* @returns {void}
*/
reset(): void {
Deno.removeSync(this.path, { recursive: true });
// There are no default values. Just exit.
if (Object.keys(this.defaultValues).length === 0) {
return;
}
// There are default values, iterate and save each.
Object.entries(this.defaultValues).forEach(([key, value]) => {
// console.log(`setting ${key}:${value}`);
this.set(key, value);
});
return;
}
/**
* Destructively reset one or more keys to defaults if they exist.
*
* If no defaults are present then this will be a no-op for all
* provided keys.
*
* If defaults are present then each key that matches one in defaults
* will be overwritten with the default value.
*
* @param {string[]} keys An Array of string keys to reset to defaults.
* @returns {void}
*/
resetKeys(keys: string[]): void {
if (Object.keys(this.defaultValues).length === 0) {
return;
}
for (const key of keys) {
if (this.defaultValues && key in this.defaultValues) {
this.set(key, this.defaultValues[key]);
}
}
}
/**
* Destructively remove a single item from the config store.
*
* @param {string} key The key to delete from the config store.
* @returns {void}
*/
delete(key: string): void {
const { store } = this;
if (store && key in store) {
delete store[key];
this.store = store;
}
}
/**
* Get a single item from the config store.
*
* @param {string} key The key to get from the config store.
* @returns {Json} Json.
*/
get(key: string): Json {
if (this.store && key in this.store) {
return this.store[key];
} else if (
this.defaultValues && key in this.defaultValues
) {
return this.defaultValues[key];
} else {
return null;
}
}
/**
* Set a single item into the config store.
*
* @param {string} key The key to write to the config store.
* @param {Json} value The value to write to the config store.
* @returns {void} void.
*/
set(
key: string,
value: Json,
): void {
const { store } = this;
const innerSet = (
key: string,
value: Json,
) => {
store[key] = value;
};
innerSet(key, value);
this.store = store;
}
/**
* Set multiple items into the config store.
*
* @param {StoreType} data The Object to write to the config store.
* @returns {void} void.
*/
setObject(
data: StoreType,
): void {
for (const [key, value] of Object.entries(data)) {
this.set(key, value);
}
}
// Allow Conf instance to be iterable
*[Symbol.iterator]() {
for (const [key, value] of Object.entries(this.store)) {
yield [key, value];
}
}
}