-
Notifications
You must be signed in to change notification settings - Fork 16
/
loader.ts
231 lines (201 loc) · 6.75 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
import { existsSync } from "node:fs";
import { homedir } from "node:os";
import { resolve, extname, dirname } from "pathe";
import createJiti, { JITI } from "jiti";
import * as rc9 from "rc9";
import { defu } from "defu";
import { findWorkspaceDir } from "pkg-types";
import type { JITIOptions } from "jiti/dist/types";
import { DotenvOptions, setupDotenv } from "./dotenv";
export interface InputConfig extends Record<string, any> {}
export interface ConfigLayer<T extends InputConfig=InputConfig> {
config: T | null
cwd?: string
configFile?: string
}
export interface ResolvedConfig<T extends InputConfig=InputConfig> extends ConfigLayer<T> {
layers?: ConfigLayer<T>[]
cwd?: string
}
export interface ResolveConfigOptions {
cwd: string
}
export interface LoadConfigOptions<T extends InputConfig=InputConfig> {
name?: string
cwd?: string
configFile?: string
rcFile?: false | string
globalRc?: boolean
dotenv?: boolean | DotenvOptions
defaults?: T
defaultConfig?: T
overrides?: T
resolve?: (id: string, options: LoadConfigOptions) => null | ResolvedConfig | Promise<ResolvedConfig | null>
jiti?: JITI
jitiOptions?: JITIOptions,
extend?: false | {
extendKey?: string | string[]
}
}
export async function loadConfig<T extends InputConfig=InputConfig> (options: LoadConfigOptions<T>): Promise<ResolvedConfig<T>> {
// Normalize options
options.cwd = resolve(process.cwd(), options.cwd || ".");
options.name = options.name || "config";
options.configFile = options.configFile ?? ((options.name !== "config") ? `${options.name}.config` : "config");
options.rcFile = options.rcFile ?? (`.${options.name}rc`);
if (options.extend !== false) {
options.extend = {
extendKey: "extends",
...options.extend
};
}
// Create jiti instance
options.jiti = options.jiti || createJiti(undefined, {
interopDefault: true,
requireCache: false,
esmResolve: true,
...options.jitiOptions
});
// Create context
const r: ResolvedConfig<T> = {
config: {} as any,
cwd: options.cwd,
configFile: resolve(options.cwd, options.configFile),
layers: []
};
// Load dotenv
if (options.dotenv) {
await setupDotenv({
cwd: options.cwd,
...(options.dotenv === true ? {} : options.dotenv)
});
}
// Load config file
const { config, configFile } = await resolveConfig(".", options);
if (configFile) {
r.configFile = configFile;
}
// Load rc files
const configRC = {};
if (options.rcFile) {
if (options.globalRc) {
Object.assign(configRC, rc9.readUser({ name: options.rcFile, dir: options.cwd }));
const workspaceDir = await findWorkspaceDir(options.cwd).catch(() => {});
if (workspaceDir) {
Object.assign(configRC, rc9.read({ name: options.rcFile, dir: workspaceDir }));
}
}
Object.assign(configRC, rc9.read({ name: options.rcFile, dir: options.cwd }));
}
// Combine sources
r.config = defu(
options.overrides,
config,
configRC,
options.defaultConfig
) as T;
// Allow extending
if (options.extend) {
await extendConfig(r.config, options);
r.layers = r.config._layers;
delete r.config._layers;
r.config = defu(
r.config,
...r.layers.map(e => e.config)
) as T;
}
// Preserve unmerged sources as layers
const baseLayers = [
options.overrides && { config: options.overrides, configFile: undefined, cwd: undefined },
{ config, configFile: options.configFile, cwd: options.cwd },
options.rcFile && { config: configRC, configFile: options.rcFile }
].filter(l => l && l.config) as ConfigLayer<T>[];
r.layers = [
...baseLayers,
...r.layers
];
// Apply defaults
if (options.defaults) {
r.config = defu(r.config, options.defaults) as T;
}
// Return resolved config
return r;
}
async function extendConfig (config, options: LoadConfigOptions) {
config._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) {
extendSources.push(...(Array.isArray(config[key]) ? config[key] : [config[key]]).filter(Boolean));
delete config[key];
}
for (const extendSource of extendSources) {
if (typeof extendSource !== "string") {
// TODO: Use error in next major versions
// eslint-disable-next-line no-console
console.warn(`Cannot extend config from \`${JSON.stringify(extendSource)}\` (which should be a string) in ${options.cwd}`);
continue;
}
const _config = await resolveConfig(extendSource, options);
if (!_config.config) {
// TODO: Use error in next major versions
// eslint-disable-next-line no-console
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;
}
}
}
const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
// 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 (source: string, options: LoadConfigOptions): Promise<ResolvedConfig> {
// Custom user resolver
if (options.resolve) {
const res = await options.resolve(source, options);
if (res) {
return res;
}
}
// Download git URLs and resolve to local path
if (GIT_PREFIXES.some(prefix => source.startsWith(prefix))) {
const { downloadTemplate } = await import("giget");
const url = new URL(source);
const gitRepo = url.protocol + url.pathname.split("/").slice(0, 2).join("/");
const name = gitRepo.replace(/[#/:@\\]/g, "_");
const tmpDir = process.env.XDG_CACHE_HOME
? resolve(process.env.XDG_CACHE_HOME, "c12", name)
: resolve(homedir(), ".cache/c12", name);
const clonned = await downloadTemplate(source, { dir: tmpDir });
source = clonned.dir;
}
// Try resolving as npm package
if (NPM_PACKAGE_RE.test(source)) {
try {
source = options.jiti.resolve(source, { paths: [options.cwd] });
} catch {}
}
// Import from local fs
const isDir = !extname(source);
const cwd = resolve(options.cwd, isDir ? source : dirname(source));
if (isDir) { source = options.configFile; }
const res: ResolvedConfig = { config: undefined, cwd };
try {
res.configFile = options.jiti.resolve(resolve(cwd, source), { paths: [cwd] });
} catch {}
if (!existsSync(res.configFile)) {
return res;
}
res.config = options.jiti(res.configFile);
if (typeof res.config === "function") {
res.config = await res.config();
}
return res;
}