-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.ts
249 lines (226 loc) · 6.21 KB
/
config.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
// deno-lint-ignore-file no-explicit-any
import { JSONc, path, TOML, YAML } from "./deps.ts";
import { Join, NestedKeys, NestedValue, Split } from "./lib/types.ts";
import { z } from "./z.ts";
/**
* Add a type-safe configuration file to your CLI context. This is
* useful for storing things like user preferences.
*
* @param schema - The schema for the config.
* @param options - Configuration options
*/
export function config<Schema extends z.ZodRawShape>(
/**
* The schema for the config.
*/
schema: Schema,
options: ConfigOptions<Schema>,
): Config<Schema> {
const schemaObject = z.object(schema);
const execPath = Deno.execPath();
const basename = path.basename(execPath, path.extname(execPath));
const name = basename === "deno" ? "zcli-dev" : basename;
const {
path: userConfigPath,
format = "toml",
mode = 0o600,
defaultConfig = {},
} = options;
const defaultConfigPath = path.join(
Deno.env.get("HOME")!,
`.${name}`,
`config.${format}`,
);
const configPath = userConfigPath ?? defaultConfigPath;
const configDir = path.dirname(configPath);
const parser = parsers[format];
let cached: z.infer<typeof schemaObject> | undefined;
return {
async set(key, value) {
const config = await this.get();
configUtil.set(config, key.split("."), value);
await this.write(config);
},
async get(key?: any): Promise<any> {
if (cached) return key ? configUtil.get(cached, key.split(".")) : cached;
const config = await this.read();
return key ? configUtil.get(config, key.split(".")) : config;
},
async delete(key) {
const config = await this.get();
const path = key.split(".");
configUtil.delete(config, path);
// Prevent knowingly failing validation
if (configUtil.get(defaultConfig, path) !== undefined) {
configUtil.set(config, path, configUtil.get(defaultConfig, path));
}
await this.write(config);
},
async clear() {
await this.write(defaultConfig as any);
},
async write(config: z.infer<typeof schemaObject>) {
try {
Deno.statSync(configDir);
} catch (_err) {
await Deno.mkdir(configDir, { recursive: true });
}
config = await schemaObject.parseAsync(config);
await Deno.writeTextFile(configPath, parser.stringify(config), {
mode,
});
cached = config;
},
async read() {
try {
Deno.statSync(configPath);
} catch (_err) {
return schemaObject.parseAsync(defaultConfig);
}
try {
const config = parser.parse(await Deno.readTextFile(configPath));
return (cached = await schemaObject.parseAsync(config));
} catch (_err) {
const nextConfig = await schemaObject.parseAsync(defaultConfig);
await this.write(nextConfig);
return (cached = nextConfig);
}
},
};
}
export const parsers = {
jsonc: {
stringify: (value: unknown) => JSON.stringify(value, null, 2),
parse: JSONc.parse,
},
yaml: YAML,
toml: TOML,
} as const;
export const configUtil = {
/**
* Recursively set a value on an object using a path.
*
* @param target - The object to set the value on.
* @param paths - The path to the value.
* @param value - The value to set.
*/
set(target: any, paths: readonly string[] | string[], value: unknown): void {
if (paths.length === 1) {
target[paths[0]] = value;
return target;
}
let next = target;
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
if (i === paths.length - 1) {
next[path] = value;
} else {
const current = next[path];
next = next[path] = current ?? (isNaN(paths[i + 1] as any) ? {} : []);
}
}
},
/**
* Recursively get a value from an object using a path.
*
* @param target - The object to get the value from.
* @param paths - The path to the value.
*/
get<Returns>(target: any, paths: readonly string[] | string[]): Returns {
let next = target;
for (const path of paths) {
next = next[path];
if (next == null) {
return next;
}
}
return next;
},
/**
* Delete a deep property from an object.
*
* @param target - The object to delete the property from.
* @param paths - The path to the property.
*/
delete(target: any, paths: readonly string[] | string[]): void {
if (paths.length === 1) {
delete target[paths[0]];
return;
}
let next = target;
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
if (i === paths.length - 1) {
delete next[path];
} else {
next = next[path];
}
}
},
};
export type ConfigOptions<Schema extends z.ZodRawShape> = {
/**
* A default config to use if the config file doesn't exist.
*/
defaultConfig: z.infer<z.ZodObject<Schema>>;
/**
* The path to the config file.
* @default "$HOME/.<name>/config.<format>"
*/
path?: string;
/**
* @default "toml"
*/
format?: "jsonc" | "yaml" | "toml";
/**
* The write mode for the config file.
* @default 0o600
*/
mode?: number;
};
export type Config<
Schema extends z.ZodRawShape,
Inferred extends z.infer<z.ZodObject<any>> = z.infer<z.ZodObject<Schema>>,
> = {
/**
* Set a value in the config.
*
* @param key - The path to the value.
* @param value - The value to set.
*/
set<Key extends Join<NestedKeys<Inferred>>>(
key: Key,
value: NestedValue<Inferred, Split<Key>>,
): Promise<void>;
/**
* Get a value from the config.
*
* @param key - The path to the value.
*/
get<Keys extends Join<NestedKeys<Inferred>>>(
key: Keys,
): Promise<NestedValue<Inferred, Split<Keys>>>;
/**
* Get the entire config.
*/
get(): Promise<Inferred>;
/**
* Delete a value from the config.
*
* @param key - The path to the value.
*/
delete<Key extends Join<NestedKeys<Inferred>>>(key: Key): Promise<void>;
/**
* Clear the entire config and reset it to its default values.
*/
clear(): Promise<void>;
/**
* Write a new config to disk.
*/
write(config: Inferred): Promise<void>;
/**
* Read the config from disk.
* @returns The config.
*/
read(): Promise<Inferred>;
};