c12 0.2.13 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/index.cjs +82 -88
- package/dist/index.d.ts +3 -3
- package/dist/index.mjs +75 -74
- package/package.json +17 -16
package/LICENSE
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -1,65 +1,58 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const fs = require('fs');
|
|
3
|
+
const node_fs = require('node:fs');
|
|
6
4
|
const pathe = require('pathe');
|
|
7
5
|
const dotenv = require('dotenv');
|
|
8
|
-
const os = require('os');
|
|
6
|
+
const os = require('node:os');
|
|
9
7
|
const createJiti = require('jiti');
|
|
10
8
|
const rc9 = require('rc9');
|
|
11
9
|
const defu = require('defu');
|
|
12
10
|
const pkgTypes = require('pkg-types');
|
|
13
11
|
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
function _interopNamespace(e) {
|
|
17
|
-
if (e && e.__esModule) return e;
|
|
12
|
+
function _interopNamespaceDefault(e) {
|
|
18
13
|
const n = Object.create(null);
|
|
19
14
|
if (e) {
|
|
20
15
|
for (const k in e) {
|
|
21
16
|
n[k] = e[k];
|
|
22
17
|
}
|
|
23
18
|
}
|
|
24
|
-
n
|
|
19
|
+
n.default = e;
|
|
25
20
|
return n;
|
|
26
21
|
}
|
|
27
22
|
|
|
28
|
-
const dotenv__namespace = /*#__PURE__*/
|
|
29
|
-
const
|
|
30
|
-
const createJiti__default = /*#__PURE__*/_interopDefaultLegacy(createJiti);
|
|
31
|
-
const rc9__namespace = /*#__PURE__*/_interopNamespace(rc9);
|
|
23
|
+
const dotenv__namespace = /*#__PURE__*/_interopNamespaceDefault(dotenv);
|
|
24
|
+
const rc9__namespace = /*#__PURE__*/_interopNamespaceDefault(rc9);
|
|
32
25
|
|
|
33
26
|
async function setupDotenv(options) {
|
|
34
|
-
const
|
|
35
|
-
const
|
|
27
|
+
const targetEnvironment = options.env ?? process.env;
|
|
28
|
+
const environment = await loadDotenv({
|
|
36
29
|
cwd: options.cwd,
|
|
37
30
|
fileName: options.fileName ?? ".env",
|
|
38
|
-
env:
|
|
31
|
+
env: targetEnvironment,
|
|
39
32
|
interpolate: options.interpolate ?? true
|
|
40
33
|
});
|
|
41
|
-
for (const key in
|
|
42
|
-
if (!key.startsWith("_") &&
|
|
43
|
-
|
|
34
|
+
for (const key in environment) {
|
|
35
|
+
if (!key.startsWith("_") && targetEnvironment[key] === void 0) {
|
|
36
|
+
targetEnvironment[key] = environment[key];
|
|
44
37
|
}
|
|
45
38
|
}
|
|
46
|
-
return
|
|
39
|
+
return environment;
|
|
47
40
|
}
|
|
48
|
-
async function loadDotenv(
|
|
49
|
-
const
|
|
50
|
-
const dotenvFile = pathe.resolve(
|
|
51
|
-
if (
|
|
52
|
-
const parsed = dotenv__namespace.parse(await
|
|
53
|
-
Object.assign(
|
|
41
|
+
async function loadDotenv(options) {
|
|
42
|
+
const environment = /* @__PURE__ */ Object.create(null);
|
|
43
|
+
const dotenvFile = pathe.resolve(options.cwd, options.fileName);
|
|
44
|
+
if (node_fs.existsSync(dotenvFile)) {
|
|
45
|
+
const parsed = dotenv__namespace.parse(await node_fs.promises.readFile(dotenvFile, "utf8"));
|
|
46
|
+
Object.assign(environment, parsed);
|
|
54
47
|
}
|
|
55
|
-
if (!
|
|
56
|
-
Object.assign(
|
|
57
|
-
|
|
48
|
+
if (!options.env._applied) {
|
|
49
|
+
Object.assign(environment, options.env);
|
|
50
|
+
environment._applied = true;
|
|
58
51
|
}
|
|
59
|
-
if (
|
|
60
|
-
interpolate(
|
|
52
|
+
if (options.interpolate) {
|
|
53
|
+
interpolate(environment);
|
|
61
54
|
}
|
|
62
|
-
return
|
|
55
|
+
return environment;
|
|
63
56
|
}
|
|
64
57
|
function interpolate(target, source = {}, parse = (v) => v) {
|
|
65
58
|
function getValue(key) {
|
|
@@ -69,9 +62,9 @@ function interpolate(target, source = {}, parse = (v) => v) {
|
|
|
69
62
|
if (typeof value !== "string") {
|
|
70
63
|
return value;
|
|
71
64
|
}
|
|
72
|
-
const matches = value.match(/(.?\${?(?:[
|
|
65
|
+
const matches = value.match(/(.?\${?(?:[\w:]+)?}?)/g) || [];
|
|
73
66
|
return parse(matches.reduce((newValue, match) => {
|
|
74
|
-
const parts = /(.?)\${?([
|
|
67
|
+
const parts = /(.?)\${?([\w:]+)?}?/g.exec(match);
|
|
75
68
|
const prefix = parts[1];
|
|
76
69
|
let value2, replacePart;
|
|
77
70
|
if (prefix === "\\") {
|
|
@@ -79,7 +72,7 @@ function interpolate(target, source = {}, parse = (v) => v) {
|
|
|
79
72
|
value2 = replacePart.replace("\\$", "$");
|
|
80
73
|
} else {
|
|
81
74
|
const key = parts[2];
|
|
82
|
-
replacePart = parts[0].
|
|
75
|
+
replacePart = parts[0].slice(prefix.length);
|
|
83
76
|
if (parents.includes(key)) {
|
|
84
77
|
console.warn(`Please avoid recursive environment variables ( loop: ${parents.join(" > ")} > ${key} )`);
|
|
85
78
|
return "";
|
|
@@ -95,58 +88,59 @@ function interpolate(target, source = {}, parse = (v) => v) {
|
|
|
95
88
|
}
|
|
96
89
|
}
|
|
97
90
|
|
|
98
|
-
async function loadConfig(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (
|
|
104
|
-
|
|
91
|
+
async function loadConfig(options) {
|
|
92
|
+
options.cwd = pathe.resolve(process.cwd(), options.cwd || ".");
|
|
93
|
+
options.name = options.name || "config";
|
|
94
|
+
options.configFile = options.configFile ?? (options.name !== "config" ? `${options.name}.config` : "config");
|
|
95
|
+
options.rcFile = options.rcFile ?? `.${options.name}rc`;
|
|
96
|
+
if (options.extend !== false) {
|
|
97
|
+
options.extend = {
|
|
105
98
|
extendKey: "extends",
|
|
106
|
-
...
|
|
99
|
+
...options.extend
|
|
107
100
|
};
|
|
108
101
|
}
|
|
109
|
-
|
|
102
|
+
options.jiti = options.jiti || createJiti(void 0, {
|
|
110
103
|
interopDefault: true,
|
|
111
104
|
requireCache: false,
|
|
112
105
|
esmResolve: true,
|
|
113
|
-
...
|
|
106
|
+
...options.jitiOptions
|
|
114
107
|
});
|
|
115
108
|
const r = {
|
|
116
109
|
config: {},
|
|
117
|
-
cwd:
|
|
118
|
-
configFile: pathe.resolve(
|
|
110
|
+
cwd: options.cwd,
|
|
111
|
+
configFile: pathe.resolve(options.cwd, options.configFile),
|
|
119
112
|
layers: []
|
|
120
113
|
};
|
|
121
|
-
if (
|
|
114
|
+
if (options.dotenv) {
|
|
122
115
|
await setupDotenv({
|
|
123
|
-
cwd:
|
|
124
|
-
...
|
|
116
|
+
cwd: options.cwd,
|
|
117
|
+
...options.dotenv === true ? {} : options.dotenv
|
|
125
118
|
});
|
|
126
119
|
}
|
|
127
|
-
const { config, configFile } = await resolveConfig(".",
|
|
120
|
+
const { config, configFile } = await resolveConfig(".", options);
|
|
128
121
|
if (configFile) {
|
|
129
122
|
r.configFile = configFile;
|
|
130
123
|
}
|
|
131
124
|
const configRC = {};
|
|
132
|
-
if (
|
|
133
|
-
if (
|
|
134
|
-
Object.assign(configRC, rc9__namespace.readUser({ name:
|
|
135
|
-
const workspaceDir = await pkgTypes.findWorkspaceDir(
|
|
125
|
+
if (options.rcFile) {
|
|
126
|
+
if (options.globalRc) {
|
|
127
|
+
Object.assign(configRC, rc9__namespace.readUser({ name: options.rcFile, dir: options.cwd }));
|
|
128
|
+
const workspaceDir = await pkgTypes.findWorkspaceDir(options.cwd).catch(() => {
|
|
129
|
+
});
|
|
136
130
|
if (workspaceDir) {
|
|
137
|
-
Object.assign(configRC, rc9__namespace.read({ name:
|
|
131
|
+
Object.assign(configRC, rc9__namespace.read({ name: options.rcFile, dir: workspaceDir }));
|
|
138
132
|
}
|
|
139
133
|
}
|
|
140
|
-
Object.assign(configRC, rc9__namespace.read({ name:
|
|
134
|
+
Object.assign(configRC, rc9__namespace.read({ name: options.rcFile, dir: options.cwd }));
|
|
141
135
|
}
|
|
142
136
|
r.config = defu.defu(
|
|
143
|
-
|
|
137
|
+
options.overrides,
|
|
144
138
|
config,
|
|
145
139
|
configRC,
|
|
146
|
-
|
|
140
|
+
options.defaultConfig
|
|
147
141
|
);
|
|
148
|
-
if (
|
|
149
|
-
await extendConfig(r.config,
|
|
142
|
+
if (options.extend) {
|
|
143
|
+
await extendConfig(r.config, options);
|
|
150
144
|
r.layers = r.config._layers;
|
|
151
145
|
delete r.config._layers;
|
|
152
146
|
r.config = defu.defu(
|
|
@@ -155,25 +149,25 @@ async function loadConfig(opts) {
|
|
|
155
149
|
);
|
|
156
150
|
}
|
|
157
151
|
const baseLayers = [
|
|
158
|
-
|
|
159
|
-
{ config, configFile:
|
|
160
|
-
|
|
152
|
+
options.overrides && { config: options.overrides, configFile: void 0, cwd: void 0 },
|
|
153
|
+
{ config, configFile: options.configFile, cwd: options.cwd },
|
|
154
|
+
options.rcFile && { config: configRC, configFile: options.rcFile }
|
|
161
155
|
].filter((l) => l && l.config);
|
|
162
156
|
r.layers = [
|
|
163
157
|
...baseLayers,
|
|
164
158
|
...r.layers
|
|
165
159
|
];
|
|
166
|
-
if (
|
|
167
|
-
r.config = defu.defu(r.config,
|
|
160
|
+
if (options.defaults) {
|
|
161
|
+
r.config = defu.defu(r.config, options.defaults);
|
|
168
162
|
}
|
|
169
163
|
return r;
|
|
170
164
|
}
|
|
171
|
-
async function extendConfig(config,
|
|
165
|
+
async function extendConfig(config, options) {
|
|
172
166
|
config._layers = config._layers || [];
|
|
173
|
-
if (!
|
|
167
|
+
if (!options.extend) {
|
|
174
168
|
return;
|
|
175
169
|
}
|
|
176
|
-
let keys =
|
|
170
|
+
let keys = options.extend.extendKey;
|
|
177
171
|
if (typeof keys === "string") {
|
|
178
172
|
keys = [keys];
|
|
179
173
|
}
|
|
@@ -184,15 +178,15 @@ async function extendConfig(config, opts) {
|
|
|
184
178
|
}
|
|
185
179
|
for (const extendSource of extendSources) {
|
|
186
180
|
if (typeof extendSource !== "string") {
|
|
187
|
-
console.warn(`Cannot extend config from \`${JSON.stringify(extendSource)}\` (which should be a string) in ${
|
|
181
|
+
console.warn(`Cannot extend config from \`${JSON.stringify(extendSource)}\` (which should be a string) in ${options.cwd}`);
|
|
188
182
|
continue;
|
|
189
183
|
}
|
|
190
|
-
const _config = await resolveConfig(extendSource,
|
|
184
|
+
const _config = await resolveConfig(extendSource, options);
|
|
191
185
|
if (!_config.config) {
|
|
192
|
-
console.warn(`Cannot extend config from \`${extendSource}\` in ${
|
|
186
|
+
console.warn(`Cannot extend config from \`${extendSource}\` in ${options.cwd}`);
|
|
193
187
|
continue;
|
|
194
188
|
}
|
|
195
|
-
await extendConfig(_config.config, { ...
|
|
189
|
+
await extendConfig(_config.config, { ...options, cwd: _config.cwd });
|
|
196
190
|
config._layers.push(_config);
|
|
197
191
|
if (_config.config._layers) {
|
|
198
192
|
config._layers.push(..._config.config._layers);
|
|
@@ -201,10 +195,10 @@ async function extendConfig(config, opts) {
|
|
|
201
195
|
}
|
|
202
196
|
}
|
|
203
197
|
const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
|
|
204
|
-
const NPM_PACKAGE_RE = /^(@[
|
|
205
|
-
async function resolveConfig(source,
|
|
206
|
-
if (
|
|
207
|
-
const res2 = await
|
|
198
|
+
const NPM_PACKAGE_RE = /^(@[\da-z~-][\d._a-z~-]*\/)?[\da-z~-][\d._a-z~-]*$/;
|
|
199
|
+
async function resolveConfig(source, options) {
|
|
200
|
+
if (options.resolve) {
|
|
201
|
+
const res2 = await options.resolve(source, options);
|
|
208
202
|
if (res2) {
|
|
209
203
|
return res2;
|
|
210
204
|
}
|
|
@@ -213,8 +207,8 @@ async function resolveConfig(source, opts) {
|
|
|
213
207
|
const url = new URL(source);
|
|
214
208
|
const subPath = url.pathname.split("/").slice(2).join("/");
|
|
215
209
|
const gitRepo = url.protocol + url.pathname.split("/").slice(0, 2).join("/");
|
|
216
|
-
const tmpdir = pathe.resolve(
|
|
217
|
-
await
|
|
210
|
+
const tmpdir = pathe.resolve(os.tmpdir(), "c12/", gitRepo.replace(/[#/:@\\]/g, "_"));
|
|
211
|
+
await node_fs.promises.rm(tmpdir, { recursive: true }).catch(() => {
|
|
218
212
|
});
|
|
219
213
|
const gittar = await import('gittar').then((r) => r.default || r);
|
|
220
214
|
const tarFile = await gittar.fetch(gitRepo);
|
|
@@ -223,24 +217,24 @@ async function resolveConfig(source, opts) {
|
|
|
223
217
|
}
|
|
224
218
|
if (NPM_PACKAGE_RE.test(source)) {
|
|
225
219
|
try {
|
|
226
|
-
source =
|
|
227
|
-
} catch
|
|
220
|
+
source = options.jiti.resolve(source, { paths: [options.cwd] });
|
|
221
|
+
} catch {
|
|
228
222
|
}
|
|
229
223
|
}
|
|
230
224
|
const isDir = !pathe.extname(source);
|
|
231
|
-
const cwd = pathe.resolve(
|
|
225
|
+
const cwd = pathe.resolve(options.cwd, isDir ? source : pathe.dirname(source));
|
|
232
226
|
if (isDir) {
|
|
233
|
-
source =
|
|
227
|
+
source = options.configFile;
|
|
234
228
|
}
|
|
235
|
-
const res = { config:
|
|
229
|
+
const res = { config: void 0, cwd };
|
|
236
230
|
try {
|
|
237
|
-
res.configFile =
|
|
238
|
-
} catch
|
|
231
|
+
res.configFile = options.jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
|
|
232
|
+
} catch {
|
|
239
233
|
}
|
|
240
|
-
if (!
|
|
234
|
+
if (!node_fs.existsSync(res.configFile)) {
|
|
241
235
|
return res;
|
|
242
236
|
}
|
|
243
|
-
res.config =
|
|
237
|
+
res.config = options.jiti(res.configFile);
|
|
244
238
|
if (typeof res.config === "function") {
|
|
245
239
|
res.config = await res.config();
|
|
246
240
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ declare type Env = typeof process.env;
|
|
|
35
35
|
*/
|
|
36
36
|
declare function setupDotenv(options: DotenvOptions): Promise<Env>;
|
|
37
37
|
/** Load environment variables into an object. */
|
|
38
|
-
declare function loadDotenv(
|
|
38
|
+
declare function loadDotenv(options: DotenvOptions): Promise<Env>;
|
|
39
39
|
|
|
40
40
|
interface InputConfig extends Record<string, any> {
|
|
41
41
|
}
|
|
@@ -61,13 +61,13 @@ interface LoadConfigOptions<T extends InputConfig = InputConfig> {
|
|
|
61
61
|
defaults?: T;
|
|
62
62
|
defaultConfig?: T;
|
|
63
63
|
overrides?: T;
|
|
64
|
-
resolve?: (id: string,
|
|
64
|
+
resolve?: (id: string, options: LoadConfigOptions) => null | ResolvedConfig | Promise<ResolvedConfig | null>;
|
|
65
65
|
jiti?: JITI;
|
|
66
66
|
jitiOptions?: JITIOptions;
|
|
67
67
|
extend?: false | {
|
|
68
68
|
extendKey?: string | string[];
|
|
69
69
|
};
|
|
70
70
|
}
|
|
71
|
-
declare function loadConfig<T extends InputConfig = InputConfig>(
|
|
71
|
+
declare function loadConfig<T extends InputConfig = InputConfig>(options: LoadConfigOptions<T>): Promise<ResolvedConfig<T>>;
|
|
72
72
|
|
|
73
73
|
export { ConfigLayer, DotenvOptions, Env, InputConfig, LoadConfigOptions, ResolveConfigOptions, ResolvedConfig, loadConfig, loadDotenv, setupDotenv };
|
package/dist/index.mjs
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import { existsSync, promises } from 'fs';
|
|
1
|
+
import { existsSync, promises } from 'node:fs';
|
|
2
2
|
import { resolve, extname, dirname } from 'pathe';
|
|
3
3
|
import * as dotenv from 'dotenv';
|
|
4
|
-
import os from 'os';
|
|
4
|
+
import os from 'node:os';
|
|
5
5
|
import createJiti from 'jiti';
|
|
6
6
|
import * as rc9 from 'rc9';
|
|
7
7
|
import { defu } from 'defu';
|
|
8
8
|
import { findWorkspaceDir } from 'pkg-types';
|
|
9
9
|
|
|
10
10
|
async function setupDotenv(options) {
|
|
11
|
-
const
|
|
12
|
-
const
|
|
11
|
+
const targetEnvironment = options.env ?? process.env;
|
|
12
|
+
const environment = await loadDotenv({
|
|
13
13
|
cwd: options.cwd,
|
|
14
14
|
fileName: options.fileName ?? ".env",
|
|
15
|
-
env:
|
|
15
|
+
env: targetEnvironment,
|
|
16
16
|
interpolate: options.interpolate ?? true
|
|
17
17
|
});
|
|
18
|
-
for (const key in
|
|
19
|
-
if (!key.startsWith("_") &&
|
|
20
|
-
|
|
18
|
+
for (const key in environment) {
|
|
19
|
+
if (!key.startsWith("_") && targetEnvironment[key] === void 0) {
|
|
20
|
+
targetEnvironment[key] = environment[key];
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
return
|
|
23
|
+
return environment;
|
|
24
24
|
}
|
|
25
|
-
async function loadDotenv(
|
|
26
|
-
const
|
|
27
|
-
const dotenvFile = resolve(
|
|
25
|
+
async function loadDotenv(options) {
|
|
26
|
+
const environment = /* @__PURE__ */ Object.create(null);
|
|
27
|
+
const dotenvFile = resolve(options.cwd, options.fileName);
|
|
28
28
|
if (existsSync(dotenvFile)) {
|
|
29
|
-
const parsed = dotenv.parse(await promises.readFile(dotenvFile, "
|
|
30
|
-
Object.assign(
|
|
29
|
+
const parsed = dotenv.parse(await promises.readFile(dotenvFile, "utf8"));
|
|
30
|
+
Object.assign(environment, parsed);
|
|
31
31
|
}
|
|
32
|
-
if (!
|
|
33
|
-
Object.assign(
|
|
34
|
-
|
|
32
|
+
if (!options.env._applied) {
|
|
33
|
+
Object.assign(environment, options.env);
|
|
34
|
+
environment._applied = true;
|
|
35
35
|
}
|
|
36
|
-
if (
|
|
37
|
-
interpolate(
|
|
36
|
+
if (options.interpolate) {
|
|
37
|
+
interpolate(environment);
|
|
38
38
|
}
|
|
39
|
-
return
|
|
39
|
+
return environment;
|
|
40
40
|
}
|
|
41
41
|
function interpolate(target, source = {}, parse = (v) => v) {
|
|
42
42
|
function getValue(key) {
|
|
@@ -46,9 +46,9 @@ function interpolate(target, source = {}, parse = (v) => v) {
|
|
|
46
46
|
if (typeof value !== "string") {
|
|
47
47
|
return value;
|
|
48
48
|
}
|
|
49
|
-
const matches = value.match(/(.?\${?(?:[
|
|
49
|
+
const matches = value.match(/(.?\${?(?:[\w:]+)?}?)/g) || [];
|
|
50
50
|
return parse(matches.reduce((newValue, match) => {
|
|
51
|
-
const parts = /(.?)\${?([
|
|
51
|
+
const parts = /(.?)\${?([\w:]+)?}?/g.exec(match);
|
|
52
52
|
const prefix = parts[1];
|
|
53
53
|
let value2, replacePart;
|
|
54
54
|
if (prefix === "\\") {
|
|
@@ -56,7 +56,7 @@ function interpolate(target, source = {}, parse = (v) => v) {
|
|
|
56
56
|
value2 = replacePart.replace("\\$", "$");
|
|
57
57
|
} else {
|
|
58
58
|
const key = parts[2];
|
|
59
|
-
replacePart = parts[0].
|
|
59
|
+
replacePart = parts[0].slice(prefix.length);
|
|
60
60
|
if (parents.includes(key)) {
|
|
61
61
|
console.warn(`Please avoid recursive environment variables ( loop: ${parents.join(" > ")} > ${key} )`);
|
|
62
62
|
return "";
|
|
@@ -72,58 +72,59 @@ function interpolate(target, source = {}, parse = (v) => v) {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
async function loadConfig(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (
|
|
81
|
-
|
|
75
|
+
async function loadConfig(options) {
|
|
76
|
+
options.cwd = resolve(process.cwd(), options.cwd || ".");
|
|
77
|
+
options.name = options.name || "config";
|
|
78
|
+
options.configFile = options.configFile ?? (options.name !== "config" ? `${options.name}.config` : "config");
|
|
79
|
+
options.rcFile = options.rcFile ?? `.${options.name}rc`;
|
|
80
|
+
if (options.extend !== false) {
|
|
81
|
+
options.extend = {
|
|
82
82
|
extendKey: "extends",
|
|
83
|
-
...
|
|
83
|
+
...options.extend
|
|
84
84
|
};
|
|
85
85
|
}
|
|
86
|
-
|
|
86
|
+
options.jiti = options.jiti || createJiti(void 0, {
|
|
87
87
|
interopDefault: true,
|
|
88
88
|
requireCache: false,
|
|
89
89
|
esmResolve: true,
|
|
90
|
-
...
|
|
90
|
+
...options.jitiOptions
|
|
91
91
|
});
|
|
92
92
|
const r = {
|
|
93
93
|
config: {},
|
|
94
|
-
cwd:
|
|
95
|
-
configFile: resolve(
|
|
94
|
+
cwd: options.cwd,
|
|
95
|
+
configFile: resolve(options.cwd, options.configFile),
|
|
96
96
|
layers: []
|
|
97
97
|
};
|
|
98
|
-
if (
|
|
98
|
+
if (options.dotenv) {
|
|
99
99
|
await setupDotenv({
|
|
100
|
-
cwd:
|
|
101
|
-
...
|
|
100
|
+
cwd: options.cwd,
|
|
101
|
+
...options.dotenv === true ? {} : options.dotenv
|
|
102
102
|
});
|
|
103
103
|
}
|
|
104
|
-
const { config, configFile } = await resolveConfig(".",
|
|
104
|
+
const { config, configFile } = await resolveConfig(".", options);
|
|
105
105
|
if (configFile) {
|
|
106
106
|
r.configFile = configFile;
|
|
107
107
|
}
|
|
108
108
|
const configRC = {};
|
|
109
|
-
if (
|
|
110
|
-
if (
|
|
111
|
-
Object.assign(configRC, rc9.readUser({ name:
|
|
112
|
-
const workspaceDir = await findWorkspaceDir(
|
|
109
|
+
if (options.rcFile) {
|
|
110
|
+
if (options.globalRc) {
|
|
111
|
+
Object.assign(configRC, rc9.readUser({ name: options.rcFile, dir: options.cwd }));
|
|
112
|
+
const workspaceDir = await findWorkspaceDir(options.cwd).catch(() => {
|
|
113
|
+
});
|
|
113
114
|
if (workspaceDir) {
|
|
114
|
-
Object.assign(configRC, rc9.read({ name:
|
|
115
|
+
Object.assign(configRC, rc9.read({ name: options.rcFile, dir: workspaceDir }));
|
|
115
116
|
}
|
|
116
117
|
}
|
|
117
|
-
Object.assign(configRC, rc9.read({ name:
|
|
118
|
+
Object.assign(configRC, rc9.read({ name: options.rcFile, dir: options.cwd }));
|
|
118
119
|
}
|
|
119
120
|
r.config = defu(
|
|
120
|
-
|
|
121
|
+
options.overrides,
|
|
121
122
|
config,
|
|
122
123
|
configRC,
|
|
123
|
-
|
|
124
|
+
options.defaultConfig
|
|
124
125
|
);
|
|
125
|
-
if (
|
|
126
|
-
await extendConfig(r.config,
|
|
126
|
+
if (options.extend) {
|
|
127
|
+
await extendConfig(r.config, options);
|
|
127
128
|
r.layers = r.config._layers;
|
|
128
129
|
delete r.config._layers;
|
|
129
130
|
r.config = defu(
|
|
@@ -132,25 +133,25 @@ async function loadConfig(opts) {
|
|
|
132
133
|
);
|
|
133
134
|
}
|
|
134
135
|
const baseLayers = [
|
|
135
|
-
|
|
136
|
-
{ config, configFile:
|
|
137
|
-
|
|
136
|
+
options.overrides && { config: options.overrides, configFile: void 0, cwd: void 0 },
|
|
137
|
+
{ config, configFile: options.configFile, cwd: options.cwd },
|
|
138
|
+
options.rcFile && { config: configRC, configFile: options.rcFile }
|
|
138
139
|
].filter((l) => l && l.config);
|
|
139
140
|
r.layers = [
|
|
140
141
|
...baseLayers,
|
|
141
142
|
...r.layers
|
|
142
143
|
];
|
|
143
|
-
if (
|
|
144
|
-
r.config = defu(r.config,
|
|
144
|
+
if (options.defaults) {
|
|
145
|
+
r.config = defu(r.config, options.defaults);
|
|
145
146
|
}
|
|
146
147
|
return r;
|
|
147
148
|
}
|
|
148
|
-
async function extendConfig(config,
|
|
149
|
+
async function extendConfig(config, options) {
|
|
149
150
|
config._layers = config._layers || [];
|
|
150
|
-
if (!
|
|
151
|
+
if (!options.extend) {
|
|
151
152
|
return;
|
|
152
153
|
}
|
|
153
|
-
let keys =
|
|
154
|
+
let keys = options.extend.extendKey;
|
|
154
155
|
if (typeof keys === "string") {
|
|
155
156
|
keys = [keys];
|
|
156
157
|
}
|
|
@@ -161,15 +162,15 @@ async function extendConfig(config, opts) {
|
|
|
161
162
|
}
|
|
162
163
|
for (const extendSource of extendSources) {
|
|
163
164
|
if (typeof extendSource !== "string") {
|
|
164
|
-
console.warn(`Cannot extend config from \`${JSON.stringify(extendSource)}\` (which should be a string) in ${
|
|
165
|
+
console.warn(`Cannot extend config from \`${JSON.stringify(extendSource)}\` (which should be a string) in ${options.cwd}`);
|
|
165
166
|
continue;
|
|
166
167
|
}
|
|
167
|
-
const _config = await resolveConfig(extendSource,
|
|
168
|
+
const _config = await resolveConfig(extendSource, options);
|
|
168
169
|
if (!_config.config) {
|
|
169
|
-
console.warn(`Cannot extend config from \`${extendSource}\` in ${
|
|
170
|
+
console.warn(`Cannot extend config from \`${extendSource}\` in ${options.cwd}`);
|
|
170
171
|
continue;
|
|
171
172
|
}
|
|
172
|
-
await extendConfig(_config.config, { ...
|
|
173
|
+
await extendConfig(_config.config, { ...options, cwd: _config.cwd });
|
|
173
174
|
config._layers.push(_config);
|
|
174
175
|
if (_config.config._layers) {
|
|
175
176
|
config._layers.push(..._config.config._layers);
|
|
@@ -178,10 +179,10 @@ async function extendConfig(config, opts) {
|
|
|
178
179
|
}
|
|
179
180
|
}
|
|
180
181
|
const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
|
|
181
|
-
const NPM_PACKAGE_RE = /^(@[
|
|
182
|
-
async function resolveConfig(source,
|
|
183
|
-
if (
|
|
184
|
-
const res2 = await
|
|
182
|
+
const NPM_PACKAGE_RE = /^(@[\da-z~-][\d._a-z~-]*\/)?[\da-z~-][\d._a-z~-]*$/;
|
|
183
|
+
async function resolveConfig(source, options) {
|
|
184
|
+
if (options.resolve) {
|
|
185
|
+
const res2 = await options.resolve(source, options);
|
|
185
186
|
if (res2) {
|
|
186
187
|
return res2;
|
|
187
188
|
}
|
|
@@ -190,7 +191,7 @@ async function resolveConfig(source, opts) {
|
|
|
190
191
|
const url = new URL(source);
|
|
191
192
|
const subPath = url.pathname.split("/").slice(2).join("/");
|
|
192
193
|
const gitRepo = url.protocol + url.pathname.split("/").slice(0, 2).join("/");
|
|
193
|
-
const tmpdir = resolve(os.tmpdir(), "c12/", gitRepo.replace(/[
|
|
194
|
+
const tmpdir = resolve(os.tmpdir(), "c12/", gitRepo.replace(/[#/:@\\]/g, "_"));
|
|
194
195
|
await promises.rm(tmpdir, { recursive: true }).catch(() => {
|
|
195
196
|
});
|
|
196
197
|
const gittar = await import('gittar').then((r) => r.default || r);
|
|
@@ -200,24 +201,24 @@ async function resolveConfig(source, opts) {
|
|
|
200
201
|
}
|
|
201
202
|
if (NPM_PACKAGE_RE.test(source)) {
|
|
202
203
|
try {
|
|
203
|
-
source =
|
|
204
|
-
} catch
|
|
204
|
+
source = options.jiti.resolve(source, { paths: [options.cwd] });
|
|
205
|
+
} catch {
|
|
205
206
|
}
|
|
206
207
|
}
|
|
207
208
|
const isDir = !extname(source);
|
|
208
|
-
const cwd = resolve(
|
|
209
|
+
const cwd = resolve(options.cwd, isDir ? source : dirname(source));
|
|
209
210
|
if (isDir) {
|
|
210
|
-
source =
|
|
211
|
+
source = options.configFile;
|
|
211
212
|
}
|
|
212
|
-
const res = { config:
|
|
213
|
+
const res = { config: void 0, cwd };
|
|
213
214
|
try {
|
|
214
|
-
res.configFile =
|
|
215
|
-
} catch
|
|
215
|
+
res.configFile = options.jiti.resolve(resolve(cwd, source), { paths: [cwd] });
|
|
216
|
+
} catch {
|
|
216
217
|
}
|
|
217
218
|
if (!existsSync(res.configFile)) {
|
|
218
219
|
return res;
|
|
219
220
|
}
|
|
220
|
-
res.config =
|
|
221
|
+
res.config = options.jiti(res.configFile);
|
|
221
222
|
if (typeof res.config === "function") {
|
|
222
223
|
res.config = await res.config();
|
|
223
224
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "c12",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Smart Config Loader",
|
|
5
5
|
"repository": "unjs/c12",
|
|
6
6
|
"license": "MIT",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"import": "./dist/index.mjs",
|
|
12
|
-
"require": "./dist/index.cjs"
|
|
12
|
+
"require": "./dist/index.cjs",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
13
14
|
}
|
|
14
15
|
},
|
|
15
16
|
"main": "./dist/index.cjs",
|
|
@@ -19,25 +20,25 @@
|
|
|
19
20
|
"dist"
|
|
20
21
|
],
|
|
21
22
|
"dependencies": {
|
|
22
|
-
"defu": "^6.1.
|
|
23
|
-
"dotenv": "^16.0.
|
|
23
|
+
"defu": "^6.1.1",
|
|
24
|
+
"dotenv": "^16.0.3",
|
|
24
25
|
"gittar": "^0.1.1",
|
|
25
|
-
"jiti": "^1.
|
|
26
|
-
"mlly": "^0.
|
|
27
|
-
"pathe": "^0.
|
|
28
|
-
"pkg-types": "^0.
|
|
29
|
-
"rc9": "^
|
|
26
|
+
"jiti": "^1.16.0",
|
|
27
|
+
"mlly": "^1.0.0",
|
|
28
|
+
"pathe": "^1.0.0",
|
|
29
|
+
"pkg-types": "^1.0.1",
|
|
30
|
+
"rc9": "^2.0.0"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
|
-
"@
|
|
33
|
-
"
|
|
34
|
-
"eslint": "^
|
|
33
|
+
"@vitest/coverage-c8": "^0.25.2",
|
|
34
|
+
"eslint": "^8.27.0",
|
|
35
|
+
"eslint-config-unjs": "^0.0.2",
|
|
35
36
|
"standard-version": "^9.5.0",
|
|
36
|
-
"typescript": "^4.8.
|
|
37
|
-
"unbuild": "^0.
|
|
38
|
-
"vitest": "^0.
|
|
37
|
+
"typescript": "^4.8.4",
|
|
38
|
+
"unbuild": "^0.9.4",
|
|
39
|
+
"vitest": "^0.25.2"
|
|
39
40
|
},
|
|
40
|
-
"packageManager": "pnpm@7.
|
|
41
|
+
"packageManager": "pnpm@7.16.0",
|
|
41
42
|
"scripts": {
|
|
42
43
|
"build": "unbuild",
|
|
43
44
|
"dev": "vitest dev",
|