c12 0.2.8 → 0.2.11
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/README.md +9 -1
- package/dist/index.cjs +38 -12
- package/dist/index.d.ts +6 -1
- package/dist/index.mjs +38 -11
- package/package.json +15 -14
package/README.md
CHANGED
|
@@ -85,7 +85,7 @@ Set to `false` to disable loading RC config.
|
|
|
85
85
|
|
|
86
86
|
### `globalRC`
|
|
87
87
|
|
|
88
|
-
Load RC config from the user's home directory. Only enabled when `rcFile` is provided. Set to `false` to disable this functionality.
|
|
88
|
+
Load RC config from the workspace directory and user's home directory. Only enabled when `rcFile` is provided. Set to `false` to disable this functionality.
|
|
89
89
|
|
|
90
90
|
### `dotenv`
|
|
91
91
|
|
|
@@ -99,6 +99,14 @@ Specify default configuration. It has the **lowest** priority.
|
|
|
99
99
|
|
|
100
100
|
Specify override configuration. It has the **highest** priority.
|
|
101
101
|
|
|
102
|
+
### `jiti`
|
|
103
|
+
|
|
104
|
+
Custom [unjs/jiti](https://github.com/unjs/jiti) instance used to import configuration files.
|
|
105
|
+
|
|
106
|
+
### `jitiOptions`
|
|
107
|
+
|
|
108
|
+
Custom [unjs/jiti](https://github.com/unjs/jiti) options to import configuration files.
|
|
109
|
+
|
|
102
110
|
## Extending configuration
|
|
103
111
|
|
|
104
112
|
If resolved config contains a `extends` key, it will be used to extend configuration.
|
package/dist/index.cjs
CHANGED
|
@@ -9,6 +9,7 @@ const os = require('os');
|
|
|
9
9
|
const createJiti = require('jiti');
|
|
10
10
|
const rc9 = require('rc9');
|
|
11
11
|
const defu = require('defu');
|
|
12
|
+
const pkgTypes = require('pkg-types');
|
|
12
13
|
|
|
13
14
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
14
15
|
|
|
@@ -28,7 +29,6 @@ const dotenv__namespace = /*#__PURE__*/_interopNamespace(dotenv);
|
|
|
28
29
|
const os__default = /*#__PURE__*/_interopDefaultLegacy(os);
|
|
29
30
|
const createJiti__default = /*#__PURE__*/_interopDefaultLegacy(createJiti);
|
|
30
31
|
const rc9__namespace = /*#__PURE__*/_interopNamespace(rc9);
|
|
31
|
-
const defu__default = /*#__PURE__*/_interopDefaultLegacy(defu);
|
|
32
32
|
|
|
33
33
|
async function setupDotenv(options) {
|
|
34
34
|
const targetEnv = options.env ?? process.env;
|
|
@@ -106,6 +106,12 @@ async function loadConfig(opts) {
|
|
|
106
106
|
...opts.extend
|
|
107
107
|
};
|
|
108
108
|
}
|
|
109
|
+
opts.jiti = opts.jiti || createJiti__default(null, {
|
|
110
|
+
interopDefault: true,
|
|
111
|
+
requireCache: false,
|
|
112
|
+
esmResolve: true,
|
|
113
|
+
...opts.jitiOptions
|
|
114
|
+
});
|
|
109
115
|
const r = {
|
|
110
116
|
config: {},
|
|
111
117
|
cwd: opts.cwd,
|
|
@@ -126,15 +132,26 @@ async function loadConfig(opts) {
|
|
|
126
132
|
if (opts.rcFile) {
|
|
127
133
|
if (opts.globalRc) {
|
|
128
134
|
Object.assign(configRC, rc9__namespace.readUser({ name: opts.rcFile, dir: opts.cwd }));
|
|
135
|
+
const workspaceDir = await pkgTypes.findWorkspaceDir(opts.cwd).catch(() => null);
|
|
136
|
+
if (workspaceDir) {
|
|
137
|
+
Object.assign(configRC, rc9__namespace.read({ name: opts.rcFile, dir: workspaceDir }));
|
|
138
|
+
}
|
|
129
139
|
}
|
|
130
140
|
Object.assign(configRC, rc9__namespace.read({ name: opts.rcFile, dir: opts.cwd }));
|
|
131
141
|
}
|
|
132
|
-
r.config =
|
|
142
|
+
r.config = defu.defu(
|
|
143
|
+
opts.overrides,
|
|
144
|
+
config,
|
|
145
|
+
configRC
|
|
146
|
+
);
|
|
133
147
|
if (opts.extend) {
|
|
134
148
|
await extendConfig(r.config, opts);
|
|
135
149
|
r.layers = r.config._layers;
|
|
136
150
|
delete r.config._layers;
|
|
137
|
-
r.config =
|
|
151
|
+
r.config = defu.defu(
|
|
152
|
+
r.config,
|
|
153
|
+
...r.layers.map((e) => e.config)
|
|
154
|
+
);
|
|
138
155
|
}
|
|
139
156
|
const baseLayers = [
|
|
140
157
|
opts.overrides && { config: opts.overrides, configFile: void 0, cwd: void 0 },
|
|
@@ -146,7 +163,7 @@ async function loadConfig(opts) {
|
|
|
146
163
|
...r.layers
|
|
147
164
|
];
|
|
148
165
|
if (opts.defaults) {
|
|
149
|
-
r.config =
|
|
166
|
+
r.config = defu.defu(r.config, opts.defaults);
|
|
150
167
|
}
|
|
151
168
|
return r;
|
|
152
169
|
}
|
|
@@ -155,13 +172,23 @@ async function extendConfig(config, opts) {
|
|
|
155
172
|
if (!opts.extend) {
|
|
156
173
|
return;
|
|
157
174
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
175
|
+
let keys = opts.extend.extendKey;
|
|
176
|
+
if (typeof keys === "string") {
|
|
177
|
+
keys = [keys];
|
|
178
|
+
}
|
|
179
|
+
const extendSources = [];
|
|
180
|
+
for (const key of keys) {
|
|
181
|
+
extendSources.push(...(Array.isArray(config[key]) ? config[key] : [config[key]]).filter(Boolean));
|
|
182
|
+
delete config[key];
|
|
183
|
+
}
|
|
161
184
|
for (const extendSource of extendSources) {
|
|
185
|
+
if (typeof extendSource !== "string") {
|
|
186
|
+
console.warn(`Cannot extend config from \`${JSON.stringify(extendSource)}\` (which should be a string) in ${opts.cwd}`);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
162
189
|
const _config = await resolveConfig(extendSource, opts);
|
|
163
190
|
if (!_config.config) {
|
|
164
|
-
console.warn(`Cannot extend config from
|
|
191
|
+
console.warn(`Cannot extend config from \`${extendSource}\` in ${opts.cwd}`);
|
|
165
192
|
continue;
|
|
166
193
|
}
|
|
167
194
|
await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
|
|
@@ -174,7 +201,6 @@ async function extendConfig(config, opts) {
|
|
|
174
201
|
}
|
|
175
202
|
const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
|
|
176
203
|
const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
|
|
177
|
-
const jiti = createJiti__default(null, { cache: false, interopDefault: true, requireCache: false });
|
|
178
204
|
async function resolveConfig(source, opts) {
|
|
179
205
|
if (opts.resolve) {
|
|
180
206
|
const res2 = await opts.resolve(source, opts);
|
|
@@ -196,7 +222,7 @@ async function resolveConfig(source, opts) {
|
|
|
196
222
|
}
|
|
197
223
|
if (NPM_PACKAGE_RE.test(source)) {
|
|
198
224
|
try {
|
|
199
|
-
source = jiti.resolve(source, { paths: [opts.cwd] });
|
|
225
|
+
source = opts.jiti.resolve(source, { paths: [opts.cwd] });
|
|
200
226
|
} catch (_err) {
|
|
201
227
|
}
|
|
202
228
|
}
|
|
@@ -207,13 +233,13 @@ async function resolveConfig(source, opts) {
|
|
|
207
233
|
}
|
|
208
234
|
const res = { config: null, cwd };
|
|
209
235
|
try {
|
|
210
|
-
res.configFile = jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
|
|
236
|
+
res.configFile = opts.jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
|
|
211
237
|
} catch (_err) {
|
|
212
238
|
}
|
|
213
239
|
if (!fs.existsSync(res.configFile)) {
|
|
214
240
|
return res;
|
|
215
241
|
}
|
|
216
|
-
res.config = jiti(res.configFile);
|
|
242
|
+
res.config = opts.jiti(res.configFile);
|
|
217
243
|
if (typeof res.config === "function") {
|
|
218
244
|
res.config = await res.config();
|
|
219
245
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { JITI } from 'jiti';
|
|
2
|
+
import { JITIOptions } from 'jiti/dist/types';
|
|
3
|
+
|
|
1
4
|
interface DotenvOptions {
|
|
2
5
|
/**
|
|
3
6
|
* The project root directory (either absolute or relative to the current working directory).
|
|
@@ -58,8 +61,10 @@ interface LoadConfigOptions<T extends InputConfig = InputConfig> {
|
|
|
58
61
|
defaults?: T;
|
|
59
62
|
overrides?: T;
|
|
60
63
|
resolve?: (id: string, opts: LoadConfigOptions) => null | ResolvedConfig | Promise<ResolvedConfig | null>;
|
|
64
|
+
jiti?: JITI;
|
|
65
|
+
jitiOptions?: JITIOptions;
|
|
61
66
|
extend?: false | {
|
|
62
|
-
extendKey?: string;
|
|
67
|
+
extendKey?: string | string[];
|
|
63
68
|
};
|
|
64
69
|
}
|
|
65
70
|
declare function loadConfig<T extends InputConfig = InputConfig>(opts: LoadConfigOptions<T>): Promise<ResolvedConfig<T>>;
|
package/dist/index.mjs
CHANGED
|
@@ -4,7 +4,8 @@ import * as dotenv from 'dotenv';
|
|
|
4
4
|
import os from 'os';
|
|
5
5
|
import createJiti from 'jiti';
|
|
6
6
|
import * as rc9 from 'rc9';
|
|
7
|
-
import defu from 'defu';
|
|
7
|
+
import { defu } from 'defu';
|
|
8
|
+
import { findWorkspaceDir } from 'pkg-types';
|
|
8
9
|
|
|
9
10
|
async function setupDotenv(options) {
|
|
10
11
|
const targetEnv = options.env ?? process.env;
|
|
@@ -82,6 +83,12 @@ async function loadConfig(opts) {
|
|
|
82
83
|
...opts.extend
|
|
83
84
|
};
|
|
84
85
|
}
|
|
86
|
+
opts.jiti = opts.jiti || createJiti(null, {
|
|
87
|
+
interopDefault: true,
|
|
88
|
+
requireCache: false,
|
|
89
|
+
esmResolve: true,
|
|
90
|
+
...opts.jitiOptions
|
|
91
|
+
});
|
|
85
92
|
const r = {
|
|
86
93
|
config: {},
|
|
87
94
|
cwd: opts.cwd,
|
|
@@ -102,15 +109,26 @@ async function loadConfig(opts) {
|
|
|
102
109
|
if (opts.rcFile) {
|
|
103
110
|
if (opts.globalRc) {
|
|
104
111
|
Object.assign(configRC, rc9.readUser({ name: opts.rcFile, dir: opts.cwd }));
|
|
112
|
+
const workspaceDir = await findWorkspaceDir(opts.cwd).catch(() => null);
|
|
113
|
+
if (workspaceDir) {
|
|
114
|
+
Object.assign(configRC, rc9.read({ name: opts.rcFile, dir: workspaceDir }));
|
|
115
|
+
}
|
|
105
116
|
}
|
|
106
117
|
Object.assign(configRC, rc9.read({ name: opts.rcFile, dir: opts.cwd }));
|
|
107
118
|
}
|
|
108
|
-
r.config = defu(
|
|
119
|
+
r.config = defu(
|
|
120
|
+
opts.overrides,
|
|
121
|
+
config,
|
|
122
|
+
configRC
|
|
123
|
+
);
|
|
109
124
|
if (opts.extend) {
|
|
110
125
|
await extendConfig(r.config, opts);
|
|
111
126
|
r.layers = r.config._layers;
|
|
112
127
|
delete r.config._layers;
|
|
113
|
-
r.config = defu(
|
|
128
|
+
r.config = defu(
|
|
129
|
+
r.config,
|
|
130
|
+
...r.layers.map((e) => e.config)
|
|
131
|
+
);
|
|
114
132
|
}
|
|
115
133
|
const baseLayers = [
|
|
116
134
|
opts.overrides && { config: opts.overrides, configFile: void 0, cwd: void 0 },
|
|
@@ -131,13 +149,23 @@ async function extendConfig(config, opts) {
|
|
|
131
149
|
if (!opts.extend) {
|
|
132
150
|
return;
|
|
133
151
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
152
|
+
let keys = opts.extend.extendKey;
|
|
153
|
+
if (typeof keys === "string") {
|
|
154
|
+
keys = [keys];
|
|
155
|
+
}
|
|
156
|
+
const extendSources = [];
|
|
157
|
+
for (const key of keys) {
|
|
158
|
+
extendSources.push(...(Array.isArray(config[key]) ? config[key] : [config[key]]).filter(Boolean));
|
|
159
|
+
delete config[key];
|
|
160
|
+
}
|
|
137
161
|
for (const extendSource of extendSources) {
|
|
162
|
+
if (typeof extendSource !== "string") {
|
|
163
|
+
console.warn(`Cannot extend config from \`${JSON.stringify(extendSource)}\` (which should be a string) in ${opts.cwd}`);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
138
166
|
const _config = await resolveConfig(extendSource, opts);
|
|
139
167
|
if (!_config.config) {
|
|
140
|
-
console.warn(`Cannot extend config from
|
|
168
|
+
console.warn(`Cannot extend config from \`${extendSource}\` in ${opts.cwd}`);
|
|
141
169
|
continue;
|
|
142
170
|
}
|
|
143
171
|
await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
|
|
@@ -150,7 +178,6 @@ async function extendConfig(config, opts) {
|
|
|
150
178
|
}
|
|
151
179
|
const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
|
|
152
180
|
const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
|
|
153
|
-
const jiti = createJiti(null, { cache: false, interopDefault: true, requireCache: false });
|
|
154
181
|
async function resolveConfig(source, opts) {
|
|
155
182
|
if (opts.resolve) {
|
|
156
183
|
const res2 = await opts.resolve(source, opts);
|
|
@@ -172,7 +199,7 @@ async function resolveConfig(source, opts) {
|
|
|
172
199
|
}
|
|
173
200
|
if (NPM_PACKAGE_RE.test(source)) {
|
|
174
201
|
try {
|
|
175
|
-
source = jiti.resolve(source, { paths: [opts.cwd] });
|
|
202
|
+
source = opts.jiti.resolve(source, { paths: [opts.cwd] });
|
|
176
203
|
} catch (_err) {
|
|
177
204
|
}
|
|
178
205
|
}
|
|
@@ -183,13 +210,13 @@ async function resolveConfig(source, opts) {
|
|
|
183
210
|
}
|
|
184
211
|
const res = { config: null, cwd };
|
|
185
212
|
try {
|
|
186
|
-
res.configFile = jiti.resolve(resolve(cwd, source), { paths: [cwd] });
|
|
213
|
+
res.configFile = opts.jiti.resolve(resolve(cwd, source), { paths: [cwd] });
|
|
187
214
|
} catch (_err) {
|
|
188
215
|
}
|
|
189
216
|
if (!existsSync(res.configFile)) {
|
|
190
217
|
return res;
|
|
191
218
|
}
|
|
192
|
-
res.config = jiti(res.configFile);
|
|
219
|
+
res.config = opts.jiti(res.configFile);
|
|
193
220
|
if (typeof res.config === "function") {
|
|
194
221
|
res.config = await res.config();
|
|
195
222
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "c12",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.11",
|
|
4
4
|
"description": "Smart Config Loader",
|
|
5
5
|
"repository": "unjs/c12",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,24 +19,25 @@
|
|
|
19
19
|
"dist"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"defu": "^6.
|
|
23
|
-
"dotenv": "^16.0.
|
|
22
|
+
"defu": "^6.1.0",
|
|
23
|
+
"dotenv": "^16.0.2",
|
|
24
24
|
"gittar": "^0.1.1",
|
|
25
|
-
"jiti": "^1.
|
|
26
|
-
"mlly": "^0.5.
|
|
27
|
-
"pathe": "^0.3.
|
|
25
|
+
"jiti": "^1.15.0",
|
|
26
|
+
"mlly": "^0.5.14",
|
|
27
|
+
"pathe": "^0.3.7",
|
|
28
|
+
"pkg-types": "^0.3.5",
|
|
28
29
|
"rc9": "^1.2.2"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
|
-
"@nuxtjs/eslint-config-typescript": "
|
|
32
|
-
"c8": "
|
|
33
|
-
"eslint": "
|
|
34
|
-
"standard-version": "
|
|
35
|
-
"typescript": "
|
|
36
|
-
"unbuild": "
|
|
37
|
-
"vitest": "
|
|
32
|
+
"@nuxtjs/eslint-config-typescript": "^11.0.0",
|
|
33
|
+
"@vitest/coverage-c8": "^0.23.1",
|
|
34
|
+
"eslint": "^8.23.0",
|
|
35
|
+
"standard-version": "^9.5.0",
|
|
36
|
+
"typescript": "^4.8.2",
|
|
37
|
+
"unbuild": "^0.8.10",
|
|
38
|
+
"vitest": "^0.23.1"
|
|
38
39
|
},
|
|
39
|
-
"packageManager": "pnpm@7.
|
|
40
|
+
"packageManager": "pnpm@7.11.0",
|
|
40
41
|
"scripts": {
|
|
41
42
|
"build": "unbuild",
|
|
42
43
|
"dev": "vitest dev",
|