c12 1.1.0 → 1.1.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/dist/index.cjs +60 -34
- package/dist/index.d.ts +4 -4
- package/dist/index.mjs +60 -34
- package/package.json +25 -22
package/dist/index.cjs
CHANGED
|
@@ -64,25 +64,32 @@ function interpolate(target, source = {}, parse = (v) => v) {
|
|
|
64
64
|
return value;
|
|
65
65
|
}
|
|
66
66
|
const matches = value.match(/(.?\${?(?:[\w:]+)?}?)/g) || [];
|
|
67
|
-
return parse(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
67
|
+
return parse(
|
|
68
|
+
// eslint-disable-next-line unicorn/no-array-reduce
|
|
69
|
+
matches.reduce((newValue, match) => {
|
|
70
|
+
const parts = /(.?)\${?([\w:]+)?}?/g.exec(match);
|
|
71
|
+
const prefix = parts[1];
|
|
72
|
+
let value2, replacePart;
|
|
73
|
+
if (prefix === "\\") {
|
|
74
|
+
replacePart = parts[0];
|
|
75
|
+
value2 = replacePart.replace("\\$", "$");
|
|
76
|
+
} else {
|
|
77
|
+
const key = parts[2];
|
|
78
|
+
replacePart = parts[0].slice(prefix.length);
|
|
79
|
+
if (parents.includes(key)) {
|
|
80
|
+
console.warn(
|
|
81
|
+
`Please avoid recursive environment variables ( loop: ${parents.join(
|
|
82
|
+
" > "
|
|
83
|
+
)} > ${key} )`
|
|
84
|
+
);
|
|
85
|
+
return "";
|
|
86
|
+
}
|
|
87
|
+
value2 = getValue(key);
|
|
88
|
+
value2 = interpolate2(value2, [...parents, key]);
|
|
80
89
|
}
|
|
81
|
-
value2
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return value2 !== void 0 ? newValue.replace(replacePart, value2) : newValue;
|
|
85
|
-
}, value));
|
|
90
|
+
return value2 !== void 0 ? newValue.replace(replacePart, value2) : newValue;
|
|
91
|
+
}, value)
|
|
92
|
+
);
|
|
86
93
|
}
|
|
87
94
|
for (const key in target) {
|
|
88
95
|
target[key] = interpolate2(getValue(key));
|
|
@@ -125,14 +132,23 @@ async function loadConfig(options) {
|
|
|
125
132
|
const configRC = {};
|
|
126
133
|
if (options.rcFile) {
|
|
127
134
|
if (options.globalRc) {
|
|
128
|
-
Object.assign(
|
|
135
|
+
Object.assign(
|
|
136
|
+
configRC,
|
|
137
|
+
rc9__namespace.readUser({ name: options.rcFile, dir: options.cwd })
|
|
138
|
+
);
|
|
129
139
|
const workspaceDir = await pkgTypes.findWorkspaceDir(options.cwd).catch(() => {
|
|
130
140
|
});
|
|
131
141
|
if (workspaceDir) {
|
|
132
|
-
Object.assign(
|
|
142
|
+
Object.assign(
|
|
143
|
+
configRC,
|
|
144
|
+
rc9__namespace.read({ name: options.rcFile, dir: workspaceDir })
|
|
145
|
+
);
|
|
133
146
|
}
|
|
134
147
|
}
|
|
135
|
-
Object.assign(
|
|
148
|
+
Object.assign(
|
|
149
|
+
configRC,
|
|
150
|
+
rc9__namespace.read({ name: options.rcFile, dir: options.cwd })
|
|
151
|
+
);
|
|
136
152
|
}
|
|
137
153
|
r.config = defu.defu(
|
|
138
154
|
options.overrides,
|
|
@@ -144,20 +160,18 @@ async function loadConfig(options) {
|
|
|
144
160
|
await extendConfig(r.config, options);
|
|
145
161
|
r.layers = r.config._layers;
|
|
146
162
|
delete r.config._layers;
|
|
147
|
-
r.config = defu.defu(
|
|
148
|
-
r.config,
|
|
149
|
-
...r.layers.map((e) => e.config)
|
|
150
|
-
);
|
|
163
|
+
r.config = defu.defu(r.config, ...r.layers.map((e) => e.config));
|
|
151
164
|
}
|
|
152
165
|
const baseLayers = [
|
|
153
|
-
options.overrides && {
|
|
166
|
+
options.overrides && {
|
|
167
|
+
config: options.overrides,
|
|
168
|
+
configFile: void 0,
|
|
169
|
+
cwd: void 0
|
|
170
|
+
},
|
|
154
171
|
{ config, configFile: options.configFile, cwd: options.cwd },
|
|
155
172
|
options.rcFile && { config: configRC, configFile: options.rcFile }
|
|
156
173
|
].filter((l) => l && l.config);
|
|
157
|
-
r.layers = [
|
|
158
|
-
...baseLayers,
|
|
159
|
-
...r.layers
|
|
160
|
-
];
|
|
174
|
+
r.layers = [...baseLayers, ...r.layers];
|
|
161
175
|
if (options.defaults) {
|
|
162
176
|
r.config = defu.defu(r.config, options.defaults);
|
|
163
177
|
}
|
|
@@ -174,17 +188,27 @@ async function extendConfig(config, options) {
|
|
|
174
188
|
}
|
|
175
189
|
const extendSources = [];
|
|
176
190
|
for (const key of keys) {
|
|
177
|
-
extendSources.push(
|
|
191
|
+
extendSources.push(
|
|
192
|
+
...(Array.isArray(config[key]) ? config[key] : [config[key]]).filter(
|
|
193
|
+
Boolean
|
|
194
|
+
)
|
|
195
|
+
);
|
|
178
196
|
delete config[key];
|
|
179
197
|
}
|
|
180
198
|
for (const extendSource of extendSources) {
|
|
181
199
|
if (typeof extendSource !== "string") {
|
|
182
|
-
console.warn(
|
|
200
|
+
console.warn(
|
|
201
|
+
`Cannot extend config from \`${JSON.stringify(
|
|
202
|
+
extendSource
|
|
203
|
+
)}\` (which should be a string) in ${options.cwd}`
|
|
204
|
+
);
|
|
183
205
|
continue;
|
|
184
206
|
}
|
|
185
207
|
const _config = await resolveConfig(extendSource, options);
|
|
186
208
|
if (!_config.config) {
|
|
187
|
-
console.warn(
|
|
209
|
+
console.warn(
|
|
210
|
+
`Cannot extend config from \`${extendSource}\` in ${options.cwd}`
|
|
211
|
+
);
|
|
188
212
|
continue;
|
|
189
213
|
}
|
|
190
214
|
await extendConfig(_config.config, { ...options, cwd: _config.cwd });
|
|
@@ -229,7 +253,9 @@ async function resolveConfig(source, options) {
|
|
|
229
253
|
}
|
|
230
254
|
const res = { config: void 0, cwd };
|
|
231
255
|
try {
|
|
232
|
-
res.configFile = options.jiti.resolve(pathe.resolve(cwd, source), {
|
|
256
|
+
res.configFile = options.jiti.resolve(pathe.resolve(cwd, source), {
|
|
257
|
+
paths: [cwd]
|
|
258
|
+
});
|
|
233
259
|
} catch {
|
|
234
260
|
}
|
|
235
261
|
if (!node_fs.existsSync(res.configFile)) {
|
package/dist/index.d.ts
CHANGED
|
@@ -4,12 +4,12 @@ import { JITIOptions } from 'jiti/dist/types';
|
|
|
4
4
|
interface DotenvOptions {
|
|
5
5
|
/**
|
|
6
6
|
* The project root directory (either absolute or relative to the current working directory).
|
|
7
|
-
|
|
7
|
+
*/
|
|
8
8
|
cwd: string;
|
|
9
9
|
/**
|
|
10
10
|
* What file to look in for environment variables (either absolute or relative
|
|
11
11
|
* to the current working directory). For example, `.env`.
|
|
12
|
-
|
|
12
|
+
*/
|
|
13
13
|
fileName?: string;
|
|
14
14
|
/**
|
|
15
15
|
* Whether to interpolate variables within .env.
|
|
@@ -24,10 +24,10 @@ interface DotenvOptions {
|
|
|
24
24
|
interpolate?: boolean;
|
|
25
25
|
/**
|
|
26
26
|
* An object describing environment variables (key, value pairs).
|
|
27
|
-
|
|
27
|
+
*/
|
|
28
28
|
env?: NodeJS.ProcessEnv;
|
|
29
29
|
}
|
|
30
|
-
|
|
30
|
+
type Env = typeof process.env;
|
|
31
31
|
/**
|
|
32
32
|
* Load and interpolate environment variables into `process.env`.
|
|
33
33
|
* If you need more control (or access to the values), consider using `loadDotenv` instead
|
package/dist/index.mjs
CHANGED
|
@@ -48,25 +48,32 @@ function interpolate(target, source = {}, parse = (v) => v) {
|
|
|
48
48
|
return value;
|
|
49
49
|
}
|
|
50
50
|
const matches = value.match(/(.?\${?(?:[\w:]+)?}?)/g) || [];
|
|
51
|
-
return parse(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
51
|
+
return parse(
|
|
52
|
+
// eslint-disable-next-line unicorn/no-array-reduce
|
|
53
|
+
matches.reduce((newValue, match) => {
|
|
54
|
+
const parts = /(.?)\${?([\w:]+)?}?/g.exec(match);
|
|
55
|
+
const prefix = parts[1];
|
|
56
|
+
let value2, replacePart;
|
|
57
|
+
if (prefix === "\\") {
|
|
58
|
+
replacePart = parts[0];
|
|
59
|
+
value2 = replacePart.replace("\\$", "$");
|
|
60
|
+
} else {
|
|
61
|
+
const key = parts[2];
|
|
62
|
+
replacePart = parts[0].slice(prefix.length);
|
|
63
|
+
if (parents.includes(key)) {
|
|
64
|
+
console.warn(
|
|
65
|
+
`Please avoid recursive environment variables ( loop: ${parents.join(
|
|
66
|
+
" > "
|
|
67
|
+
)} > ${key} )`
|
|
68
|
+
);
|
|
69
|
+
return "";
|
|
70
|
+
}
|
|
71
|
+
value2 = getValue(key);
|
|
72
|
+
value2 = interpolate2(value2, [...parents, key]);
|
|
64
73
|
}
|
|
65
|
-
value2
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return value2 !== void 0 ? newValue.replace(replacePart, value2) : newValue;
|
|
69
|
-
}, value));
|
|
74
|
+
return value2 !== void 0 ? newValue.replace(replacePart, value2) : newValue;
|
|
75
|
+
}, value)
|
|
76
|
+
);
|
|
70
77
|
}
|
|
71
78
|
for (const key in target) {
|
|
72
79
|
target[key] = interpolate2(getValue(key));
|
|
@@ -109,14 +116,23 @@ async function loadConfig(options) {
|
|
|
109
116
|
const configRC = {};
|
|
110
117
|
if (options.rcFile) {
|
|
111
118
|
if (options.globalRc) {
|
|
112
|
-
Object.assign(
|
|
119
|
+
Object.assign(
|
|
120
|
+
configRC,
|
|
121
|
+
rc9.readUser({ name: options.rcFile, dir: options.cwd })
|
|
122
|
+
);
|
|
113
123
|
const workspaceDir = await findWorkspaceDir(options.cwd).catch(() => {
|
|
114
124
|
});
|
|
115
125
|
if (workspaceDir) {
|
|
116
|
-
Object.assign(
|
|
126
|
+
Object.assign(
|
|
127
|
+
configRC,
|
|
128
|
+
rc9.read({ name: options.rcFile, dir: workspaceDir })
|
|
129
|
+
);
|
|
117
130
|
}
|
|
118
131
|
}
|
|
119
|
-
Object.assign(
|
|
132
|
+
Object.assign(
|
|
133
|
+
configRC,
|
|
134
|
+
rc9.read({ name: options.rcFile, dir: options.cwd })
|
|
135
|
+
);
|
|
120
136
|
}
|
|
121
137
|
r.config = defu(
|
|
122
138
|
options.overrides,
|
|
@@ -128,20 +144,18 @@ async function loadConfig(options) {
|
|
|
128
144
|
await extendConfig(r.config, options);
|
|
129
145
|
r.layers = r.config._layers;
|
|
130
146
|
delete r.config._layers;
|
|
131
|
-
r.config = defu(
|
|
132
|
-
r.config,
|
|
133
|
-
...r.layers.map((e) => e.config)
|
|
134
|
-
);
|
|
147
|
+
r.config = defu(r.config, ...r.layers.map((e) => e.config));
|
|
135
148
|
}
|
|
136
149
|
const baseLayers = [
|
|
137
|
-
options.overrides && {
|
|
150
|
+
options.overrides && {
|
|
151
|
+
config: options.overrides,
|
|
152
|
+
configFile: void 0,
|
|
153
|
+
cwd: void 0
|
|
154
|
+
},
|
|
138
155
|
{ config, configFile: options.configFile, cwd: options.cwd },
|
|
139
156
|
options.rcFile && { config: configRC, configFile: options.rcFile }
|
|
140
157
|
].filter((l) => l && l.config);
|
|
141
|
-
r.layers = [
|
|
142
|
-
...baseLayers,
|
|
143
|
-
...r.layers
|
|
144
|
-
];
|
|
158
|
+
r.layers = [...baseLayers, ...r.layers];
|
|
145
159
|
if (options.defaults) {
|
|
146
160
|
r.config = defu(r.config, options.defaults);
|
|
147
161
|
}
|
|
@@ -158,17 +172,27 @@ async function extendConfig(config, options) {
|
|
|
158
172
|
}
|
|
159
173
|
const extendSources = [];
|
|
160
174
|
for (const key of keys) {
|
|
161
|
-
extendSources.push(
|
|
175
|
+
extendSources.push(
|
|
176
|
+
...(Array.isArray(config[key]) ? config[key] : [config[key]]).filter(
|
|
177
|
+
Boolean
|
|
178
|
+
)
|
|
179
|
+
);
|
|
162
180
|
delete config[key];
|
|
163
181
|
}
|
|
164
182
|
for (const extendSource of extendSources) {
|
|
165
183
|
if (typeof extendSource !== "string") {
|
|
166
|
-
console.warn(
|
|
184
|
+
console.warn(
|
|
185
|
+
`Cannot extend config from \`${JSON.stringify(
|
|
186
|
+
extendSource
|
|
187
|
+
)}\` (which should be a string) in ${options.cwd}`
|
|
188
|
+
);
|
|
167
189
|
continue;
|
|
168
190
|
}
|
|
169
191
|
const _config = await resolveConfig(extendSource, options);
|
|
170
192
|
if (!_config.config) {
|
|
171
|
-
console.warn(
|
|
193
|
+
console.warn(
|
|
194
|
+
`Cannot extend config from \`${extendSource}\` in ${options.cwd}`
|
|
195
|
+
);
|
|
172
196
|
continue;
|
|
173
197
|
}
|
|
174
198
|
await extendConfig(_config.config, { ...options, cwd: _config.cwd });
|
|
@@ -213,7 +237,9 @@ async function resolveConfig(source, options) {
|
|
|
213
237
|
}
|
|
214
238
|
const res = { config: void 0, cwd };
|
|
215
239
|
try {
|
|
216
|
-
res.configFile = options.jiti.resolve(resolve(cwd, source), {
|
|
240
|
+
res.configFile = options.jiti.resolve(resolve(cwd, source), {
|
|
241
|
+
paths: [cwd]
|
|
242
|
+
});
|
|
217
243
|
} catch {
|
|
218
244
|
}
|
|
219
245
|
if (!existsSync(res.configFile)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "c12",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Smart Config Loader",
|
|
5
5
|
"repository": "unjs/c12",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,31 +19,34 @@
|
|
|
19
19
|
"files": [
|
|
20
20
|
"dist"
|
|
21
21
|
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "unbuild",
|
|
24
|
+
"dev": "vitest dev",
|
|
25
|
+
"lint": "eslint --ext .ts,.js,.mjs,.cjs . && prettier -c src test",
|
|
26
|
+
"lint:fix": "eslint --ext .ts,.js,.mjs,.cjs . --fix && prettier -w src test",
|
|
27
|
+
"prepack": "unbuild",
|
|
28
|
+
"release": "pnpm test && changelogen --release && npm publish && git push --follow-tags",
|
|
29
|
+
"test": "vitest run --coverage"
|
|
30
|
+
},
|
|
22
31
|
"dependencies": {
|
|
23
|
-
"defu": "^6.1.
|
|
32
|
+
"defu": "^6.1.2",
|
|
24
33
|
"dotenv": "^16.0.3",
|
|
25
34
|
"giget": "^1.0.0",
|
|
26
|
-
"jiti": "^1.
|
|
27
|
-
"mlly": "^1.
|
|
28
|
-
"pathe": "^1.
|
|
35
|
+
"jiti": "^1.17.0",
|
|
36
|
+
"mlly": "^1.1.1",
|
|
37
|
+
"pathe": "^1.1.0",
|
|
29
38
|
"pkg-types": "^1.0.1",
|
|
30
|
-
"rc9": "^2.0.
|
|
39
|
+
"rc9": "^2.0.1"
|
|
31
40
|
},
|
|
32
41
|
"devDependencies": {
|
|
33
|
-
"@vitest/coverage-c8": "^0.
|
|
34
|
-
"
|
|
35
|
-
"eslint
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
42
|
+
"@vitest/coverage-c8": "^0.28.5",
|
|
43
|
+
"changelogen": "^0.4.1",
|
|
44
|
+
"eslint": "^8.34.0",
|
|
45
|
+
"eslint-config-unjs": "^0.1.0",
|
|
46
|
+
"prettier": "^2.8.4",
|
|
47
|
+
"typescript": "^4.9.5",
|
|
48
|
+
"unbuild": "^1.1.1",
|
|
49
|
+
"vitest": "^0.28.5"
|
|
40
50
|
},
|
|
41
|
-
"packageManager": "pnpm@7.
|
|
42
|
-
|
|
43
|
-
"build": "unbuild",
|
|
44
|
-
"dev": "vitest dev",
|
|
45
|
-
"lint": "eslint --ext .ts,.js,.mjs,.cjs .",
|
|
46
|
-
"release": "pnpm test && standard-version && git push --follow-tags && pnpm publish",
|
|
47
|
-
"test": "vitest run --coverage"
|
|
48
|
-
}
|
|
49
|
-
}
|
|
51
|
+
"packageManager": "pnpm@7.27.0"
|
|
52
|
+
}
|