-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathloader.ts
426 lines (380 loc) · 11.6 KB
/
loader.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import { existsSync } from "node:fs";
import { readFile, rm } from "node:fs/promises";
import { homedir } from "node:os";
import { resolve, extname, dirname, basename, join } from "pathe";
import { createJiti } from "jiti";
import { fileURLToPath } from "mlly";
import * as rc9 from "rc9";
import { defu } from "defu";
import { hash } from "ohash";
import { findWorkspaceDir, readPackageJSON } from "pkg-types";
import { setupDotenv } from "./dotenv";
import type {
UserInputConfig,
ConfigLayerMeta,
LoadConfigOptions,
ResolvedConfig,
ResolvableConfig,
ConfigLayer,
SourceOptions,
InputConfig,
ResolvableConfigContext,
} from "./types";
const _normalize = (p?: string) => p?.replace(/\\/g, "/");
const ASYNC_LOADERS = {
".yaml": () => import("confbox/yaml").then((r) => r.parseYAML),
".yml": () => import("confbox/yaml").then((r) => r.parseYAML),
".jsonc": () => import("confbox/jsonc").then((r) => r.parseJSONC),
".json5": () => import("confbox/json5").then((r) => r.parseJSON5),
".toml": () => import("confbox/toml").then((r) => r.parseTOML),
} as const;
export const SUPPORTED_EXTENSIONS = [
// with jiti
".js",
".ts",
".mjs",
".cjs",
".mts",
".cts",
".json",
// with confbox
".jsonc",
".json5",
".yaml",
".yml",
".toml",
] as const;
export async function loadConfig<
T extends UserInputConfig = UserInputConfig,
MT extends ConfigLayerMeta = ConfigLayerMeta,
>(options: LoadConfigOptions<T, MT>): Promise<ResolvedConfig<T, MT>> {
// Normalize options
options.cwd = resolve(process.cwd(), options.cwd || ".");
options.name = options.name || "config";
options.envName = options.envName ?? process.env.NODE_ENV;
options.configFile =
options.configFile ??
(options.name === "config" ? "config" : `${options.name}.config`);
options.rcFile = options.rcFile ?? `.${options.name}rc`;
if (options.extend !== false) {
options.extend = {
extendKey: "extends",
...options.extend,
};
}
// Custom merger
const _merger = options.merger || defu;
// Create jiti instance
options.jiti =
options.jiti ||
createJiti(join(options.cwd, options.configFile), {
interopDefault: true,
moduleCache: false,
extensions: [...SUPPORTED_EXTENSIONS],
...options.jitiOptions,
});
// Create context
const r: ResolvedConfig<T, MT> = {
config: {} as any,
cwd: options.cwd,
configFile: resolve(options.cwd, options.configFile),
layers: [],
};
// prettier-ignore
type _ConfigName = keyof ResolvableConfigContext["configs"]
const _configs: Record<_ConfigName, ResolvableConfig<T> | null | undefined> =
{
overrides: options.overrides,
main: undefined,
rc: undefined,
packageJson: undefined,
defaultConfig: options.defaultConfig,
};
// Load dotenv
if (options.dotenv) {
await setupDotenv({
cwd: options.cwd,
...(options.dotenv === true ? {} : options.dotenv),
});
}
// Load main config file
const _mainConfig = await resolveConfig(".", options);
if (_mainConfig.configFile) {
_configs.main = _mainConfig.config;
r.configFile = _mainConfig.configFile;
}
if (_mainConfig.meta) {
r.meta = _mainConfig.meta;
}
// Load rc files
if (options.rcFile) {
const rcSources: T[] = [];
// 1. cwd
rcSources.push(rc9.read({ name: options.rcFile, dir: options.cwd }));
if (options.globalRc) {
// 2. workspace
const workspaceDir = await findWorkspaceDir(options.cwd).catch(() => {});
if (workspaceDir) {
rcSources.push(rc9.read({ name: options.rcFile, dir: workspaceDir }));
}
// 3. user home
rcSources.push(rc9.readUser({ name: options.rcFile, dir: options.cwd }));
}
_configs.rc = _merger({} as T, ...rcSources);
}
// Load config from package.json
if (options.packageJson) {
const keys = (
Array.isArray(options.packageJson)
? options.packageJson
: [
typeof options.packageJson === "string"
? options.packageJson
: options.name,
]
).filter((t) => t && typeof t === "string");
const pkgJsonFile = await readPackageJSON(options.cwd).catch(() => {});
const values = keys.map((key) => pkgJsonFile?.[key]);
_configs.packageJson = _merger({} as T, ...values);
}
// Resolve config sources
const configs = {} as Record<_ConfigName, T | null | undefined>;
for (const key in _configs) {
const value = _configs[key as _ConfigName];
configs[key as _ConfigName] = await (typeof value === "function"
? value({ configs })
: value);
}
// Combine sources
r.config = _merger(
configs.overrides,
configs.main,
configs.rc,
configs.packageJson,
configs.defaultConfig,
) as T;
// Allow extending
if (options.extend) {
await extendConfig(r.config, options);
r.layers = r.config._layers;
delete r.config._layers;
r.config = _merger(r.config, ...r.layers!.map((e) => e.config)) as T;
}
// Preserve unmerged sources as layers
const baseLayers: ConfigLayer<T, MT>[] = [
configs.overrides && {
config: configs.overrides,
configFile: undefined,
cwd: undefined,
},
{ config: configs.main, configFile: options.configFile, cwd: options.cwd },
configs.rc && { config: configs.rc, configFile: options.rcFile },
configs.packageJson && {
config: configs.packageJson,
configFile: "package.json",
},
].filter((l) => l && l.config) as ConfigLayer<T, MT>[];
r.layers = [...baseLayers, ...r.layers!];
// Apply defaults
if (options.defaults) {
r.config = _merger(r.config, options.defaults) as T;
}
// Remove environment-specific and built-in keys start with $
if (options.omit$Keys) {
for (const key in r.config) {
if (key.startsWith("$")) {
delete r.config[key];
}
}
}
// Return resolved config
return r;
}
async function extendConfig<
T extends UserInputConfig = UserInputConfig,
MT extends ConfigLayerMeta = ConfigLayerMeta,
>(config: InputConfig<T, MT>, options: LoadConfigOptions<T, MT>) {
(config as any)._layers = config._layers || [];
if (!options.extend) {
return;
}
let keys = options.extend.extendKey;
if (typeof keys === "string") {
keys = [keys];
}
const extendSources = [];
for (const key of keys as string[]) {
extendSources.push(
...(Array.isArray(config[key]) ? config[key] : [config[key]]).filter(
Boolean,
),
);
delete config[key];
}
for (let extendSource of extendSources) {
const originalExtendSource = extendSource;
let sourceOptions = {};
if (extendSource.source) {
sourceOptions = extendSource.options || {};
extendSource = extendSource.source;
}
if (Array.isArray(extendSource)) {
sourceOptions = extendSource[1] || {};
extendSource = extendSource[0];
}
if (typeof extendSource !== "string") {
// TODO: Use error in next major versions
console.warn(
`Cannot extend config from \`${JSON.stringify(
originalExtendSource,
)}\` in ${options.cwd}`,
);
continue;
}
const _config = await resolveConfig(extendSource, options, sourceOptions);
if (!_config.config) {
// TODO: Use error in next major versions
console.warn(
`Cannot extend config from \`${extendSource}\` in ${options.cwd}`,
);
continue;
}
await extendConfig(_config.config, { ...options, cwd: _config.cwd });
config._layers.push(_config);
if (_config.config._layers) {
config._layers.push(..._config.config._layers);
delete _config.config._layers;
}
}
}
// TODO: Either expose from giget directly or redirect all non file:// protocols to giget
const GIGET_PREFIXES = [
"gh:",
"github:",
"gitlab:",
"bitbucket:",
"https://",
"http://",
];
// https://github.com/dword-design/package-name-regex
const NPM_PACKAGE_RE =
/^(@[\da-z~-][\d._a-z~-]*\/)?[\da-z~-][\d._a-z~-]*($|\/.*)/;
async function resolveConfig<
T extends UserInputConfig = UserInputConfig,
MT extends ConfigLayerMeta = ConfigLayerMeta,
>(
source: string,
options: LoadConfigOptions<T, MT>,
sourceOptions: SourceOptions<T, MT> = {},
): Promise<ResolvedConfig<T, MT>> {
// Custom user resolver
if (options.resolve) {
const res = await options.resolve(source, options);
if (res) {
return res;
}
}
// Custom merger
const _merger = options.merger || defu;
// Download giget URIs and resolve to local path
if (
options.giget !== false &&
GIGET_PREFIXES.some((prefix) => source.startsWith(prefix))
) {
const { downloadTemplate } = await import("giget");
const cloneName =
source.replace(/\W+/g, "_").split("_").splice(0, 3).join("_") +
"_" +
hash(source);
let cloneDir: string;
const localNodeModules = resolve(options.cwd!, "node_modules");
const parentDir = dirname(options.cwd!);
if (basename(parentDir) === ".c12") {
cloneDir = join(parentDir, cloneName);
} else if (existsSync(localNodeModules)) {
cloneDir = join(localNodeModules, ".c12", cloneName);
} else {
cloneDir = process.env.XDG_CACHE_HOME
? resolve(process.env.XDG_CACHE_HOME, "c12", cloneName)
: resolve(homedir(), ".cache/c12", cloneName);
}
if (existsSync(cloneDir) && !sourceOptions.install) {
await rm(cloneDir, { recursive: true });
}
const cloned = await downloadTemplate(source, {
dir: cloneDir,
install: sourceOptions.install,
force: sourceOptions.install,
auth: sourceOptions.auth,
...options.giget,
...sourceOptions.giget,
});
source = cloned.dir;
}
// Util to try resolving a module
const tryResolve = (id: string) => {
const resolved = options.jiti!.esmResolve(id, { try: true });
return resolved ? fileURLToPath(resolved) : undefined;
};
// Try resolving as npm package
if (NPM_PACKAGE_RE.test(source)) {
source = tryResolve(source) || source;
}
// Import from local fs
const ext = extname(source);
const isDir = !ext || ext === basename(source); /* #71 */
const cwd = resolve(options.cwd!, isDir ? source : dirname(source));
if (isDir) {
source = options.configFile!;
}
const res: ResolvedConfig<T, MT> = {
config: undefined as unknown as T,
configFile: undefined,
cwd,
source,
sourceOptions,
};
res.configFile =
tryResolve(resolve(cwd, source)) ||
tryResolve(resolve(cwd, ".config", source.replace(/\.config$/, ""))) ||
tryResolve(resolve(cwd, ".config", source)) ||
source;
if (!existsSync(res.configFile!)) {
return res;
}
const configFileExt = extname(res.configFile!) || "";
if (configFileExt in ASYNC_LOADERS) {
const asyncLoader =
await ASYNC_LOADERS[configFileExt as keyof typeof ASYNC_LOADERS]();
const contents = await readFile(res.configFile!, "utf8");
res.config = asyncLoader(contents);
} else {
res.config = (await options.jiti!.import(res.configFile!, {
default: true,
})) as T;
}
if (res.config instanceof Function) {
res.config = await res.config();
}
// Extend env specific config
if (options.envName) {
const envConfig = {
...res.config!["$" + options.envName],
...res.config!.$env?.[options.envName],
};
if (Object.keys(envConfig).length > 0) {
res.config = _merger(envConfig, res.config);
}
}
// Meta
res.meta = defu(res.sourceOptions!.meta, res.config!.$meta) as MT;
delete res.config!.$meta;
// Overrides
if (res.sourceOptions!.overrides) {
res.config = _merger(res.sourceOptions!.overrides, res.config) as T;
}
// Always windows paths
res.configFile = _normalize(res.configFile);
res.source = _normalize(res.source);
return res;
}