c12 0.2.10 → 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 +22 -9
- package/dist/index.d.ts +5 -0
- package/dist/index.mjs +20 -6
- 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,10 +132,14 @@ 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(
|
|
133
143
|
opts.overrides,
|
|
134
144
|
config,
|
|
135
145
|
configRC
|
|
@@ -138,7 +148,7 @@ async function loadConfig(opts) {
|
|
|
138
148
|
await extendConfig(r.config, opts);
|
|
139
149
|
r.layers = r.config._layers;
|
|
140
150
|
delete r.config._layers;
|
|
141
|
-
r.config =
|
|
151
|
+
r.config = defu.defu(
|
|
142
152
|
r.config,
|
|
143
153
|
...r.layers.map((e) => e.config)
|
|
144
154
|
);
|
|
@@ -153,7 +163,7 @@ async function loadConfig(opts) {
|
|
|
153
163
|
...r.layers
|
|
154
164
|
];
|
|
155
165
|
if (opts.defaults) {
|
|
156
|
-
r.config =
|
|
166
|
+
r.config = defu.defu(r.config, opts.defaults);
|
|
157
167
|
}
|
|
158
168
|
return r;
|
|
159
169
|
}
|
|
@@ -172,9 +182,13 @@ async function extendConfig(config, opts) {
|
|
|
172
182
|
delete config[key];
|
|
173
183
|
}
|
|
174
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
|
+
}
|
|
175
189
|
const _config = await resolveConfig(extendSource, opts);
|
|
176
190
|
if (!_config.config) {
|
|
177
|
-
console.warn(`Cannot extend config from
|
|
191
|
+
console.warn(`Cannot extend config from \`${extendSource}\` in ${opts.cwd}`);
|
|
178
192
|
continue;
|
|
179
193
|
}
|
|
180
194
|
await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
|
|
@@ -187,7 +201,6 @@ async function extendConfig(config, opts) {
|
|
|
187
201
|
}
|
|
188
202
|
const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
|
|
189
203
|
const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
|
|
190
|
-
const jiti = createJiti__default(null, { cache: false, interopDefault: true, requireCache: false, esmResolve: true });
|
|
191
204
|
async function resolveConfig(source, opts) {
|
|
192
205
|
if (opts.resolve) {
|
|
193
206
|
const res2 = await opts.resolve(source, opts);
|
|
@@ -209,7 +222,7 @@ async function resolveConfig(source, opts) {
|
|
|
209
222
|
}
|
|
210
223
|
if (NPM_PACKAGE_RE.test(source)) {
|
|
211
224
|
try {
|
|
212
|
-
source = jiti.resolve(source, { paths: [opts.cwd] });
|
|
225
|
+
source = opts.jiti.resolve(source, { paths: [opts.cwd] });
|
|
213
226
|
} catch (_err) {
|
|
214
227
|
}
|
|
215
228
|
}
|
|
@@ -220,13 +233,13 @@ async function resolveConfig(source, opts) {
|
|
|
220
233
|
}
|
|
221
234
|
const res = { config: null, cwd };
|
|
222
235
|
try {
|
|
223
|
-
res.configFile = jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
|
|
236
|
+
res.configFile = opts.jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
|
|
224
237
|
} catch (_err) {
|
|
225
238
|
}
|
|
226
239
|
if (!fs.existsSync(res.configFile)) {
|
|
227
240
|
return res;
|
|
228
241
|
}
|
|
229
|
-
res.config = jiti(res.configFile);
|
|
242
|
+
res.config = opts.jiti(res.configFile);
|
|
230
243
|
if (typeof res.config === "function") {
|
|
231
244
|
res.config = await res.config();
|
|
232
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,6 +61,8 @@ 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
67
|
extendKey?: string | string[];
|
|
63
68
|
};
|
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,6 +109,10 @@ 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
|
}
|
|
@@ -148,9 +159,13 @@ async function extendConfig(config, opts) {
|
|
|
148
159
|
delete config[key];
|
|
149
160
|
}
|
|
150
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
|
+
}
|
|
151
166
|
const _config = await resolveConfig(extendSource, opts);
|
|
152
167
|
if (!_config.config) {
|
|
153
|
-
console.warn(`Cannot extend config from
|
|
168
|
+
console.warn(`Cannot extend config from \`${extendSource}\` in ${opts.cwd}`);
|
|
154
169
|
continue;
|
|
155
170
|
}
|
|
156
171
|
await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
|
|
@@ -163,7 +178,6 @@ async function extendConfig(config, opts) {
|
|
|
163
178
|
}
|
|
164
179
|
const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
|
|
165
180
|
const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
|
|
166
|
-
const jiti = createJiti(null, { cache: false, interopDefault: true, requireCache: false, esmResolve: true });
|
|
167
181
|
async function resolveConfig(source, opts) {
|
|
168
182
|
if (opts.resolve) {
|
|
169
183
|
const res2 = await opts.resolve(source, opts);
|
|
@@ -185,7 +199,7 @@ async function resolveConfig(source, opts) {
|
|
|
185
199
|
}
|
|
186
200
|
if (NPM_PACKAGE_RE.test(source)) {
|
|
187
201
|
try {
|
|
188
|
-
source = jiti.resolve(source, { paths: [opts.cwd] });
|
|
202
|
+
source = opts.jiti.resolve(source, { paths: [opts.cwd] });
|
|
189
203
|
} catch (_err) {
|
|
190
204
|
}
|
|
191
205
|
}
|
|
@@ -196,13 +210,13 @@ async function resolveConfig(source, opts) {
|
|
|
196
210
|
}
|
|
197
211
|
const res = { config: null, cwd };
|
|
198
212
|
try {
|
|
199
|
-
res.configFile = jiti.resolve(resolve(cwd, source), { paths: [cwd] });
|
|
213
|
+
res.configFile = opts.jiti.resolve(resolve(cwd, source), { paths: [cwd] });
|
|
200
214
|
} catch (_err) {
|
|
201
215
|
}
|
|
202
216
|
if (!existsSync(res.configFile)) {
|
|
203
217
|
return res;
|
|
204
218
|
}
|
|
205
|
-
res.config = jiti(res.configFile);
|
|
219
|
+
res.config = opts.jiti(res.configFile);
|
|
206
220
|
if (typeof res.config === "function") {
|
|
207
221
|
res.config = await res.config();
|
|
208
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",
|