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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 - UnJS
3
+ Copyright (c) Pooya Parsa <pooya@pi0.io>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.cjs CHANGED
@@ -1,65 +1,58 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
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 _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
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["default"] = e;
19
+ n.default = e;
25
20
  return n;
26
21
  }
27
22
 
28
- const dotenv__namespace = /*#__PURE__*/_interopNamespace(dotenv);
29
- const os__default = /*#__PURE__*/_interopDefaultLegacy(os);
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 targetEnv = options.env ?? process.env;
35
- const env = await loadDotenv({
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: targetEnv,
31
+ env: targetEnvironment,
39
32
  interpolate: options.interpolate ?? true
40
33
  });
41
- for (const key in env) {
42
- if (!key.startsWith("_") && targetEnv[key] === void 0) {
43
- targetEnv[key] = env[key];
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 env;
39
+ return environment;
47
40
  }
48
- async function loadDotenv(opts) {
49
- const env = /* @__PURE__ */ Object.create(null);
50
- const dotenvFile = pathe.resolve(opts.cwd, opts.fileName);
51
- if (fs.existsSync(dotenvFile)) {
52
- const parsed = dotenv__namespace.parse(await fs.promises.readFile(dotenvFile, "utf-8"));
53
- Object.assign(env, parsed);
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 (!opts.env._applied) {
56
- Object.assign(env, opts.env);
57
- env._applied = true;
48
+ if (!options.env._applied) {
49
+ Object.assign(environment, options.env);
50
+ environment._applied = true;
58
51
  }
59
- if (opts.interpolate) {
60
- interpolate(env);
52
+ if (options.interpolate) {
53
+ interpolate(environment);
61
54
  }
62
- return env;
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(/(.?\${?(?:[a-zA-Z0-9_:]+)?}?)/g) || [];
65
+ const matches = value.match(/(.?\${?(?:[\w:]+)?}?)/g) || [];
73
66
  return parse(matches.reduce((newValue, match) => {
74
- const parts = /(.?)\${?([a-zA-Z0-9_:]+)?}?/g.exec(match);
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].substring(prefix.length);
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(opts) {
99
- opts.cwd = pathe.resolve(process.cwd(), opts.cwd || ".");
100
- opts.name = opts.name || "config";
101
- opts.configFile = opts.configFile ?? (opts.name !== "config" ? `${opts.name}.config` : "config");
102
- opts.rcFile = opts.rcFile ?? `.${opts.name}rc`;
103
- if (opts.extend !== false) {
104
- opts.extend = {
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
- ...opts.extend
99
+ ...options.extend
107
100
  };
108
101
  }
109
- opts.jiti = opts.jiti || createJiti__default(null, {
102
+ options.jiti = options.jiti || createJiti(void 0, {
110
103
  interopDefault: true,
111
104
  requireCache: false,
112
105
  esmResolve: true,
113
- ...opts.jitiOptions
106
+ ...options.jitiOptions
114
107
  });
115
108
  const r = {
116
109
  config: {},
117
- cwd: opts.cwd,
118
- configFile: pathe.resolve(opts.cwd, opts.configFile),
110
+ cwd: options.cwd,
111
+ configFile: pathe.resolve(options.cwd, options.configFile),
119
112
  layers: []
120
113
  };
121
- if (opts.dotenv) {
114
+ if (options.dotenv) {
122
115
  await setupDotenv({
123
- cwd: opts.cwd,
124
- ...opts.dotenv === true ? {} : opts.dotenv
116
+ cwd: options.cwd,
117
+ ...options.dotenv === true ? {} : options.dotenv
125
118
  });
126
119
  }
127
- const { config, configFile } = await resolveConfig(".", opts);
120
+ const { config, configFile } = await resolveConfig(".", options);
128
121
  if (configFile) {
129
122
  r.configFile = configFile;
130
123
  }
131
124
  const configRC = {};
132
- if (opts.rcFile) {
133
- if (opts.globalRc) {
134
- Object.assign(configRC, rc9__namespace.readUser({ name: opts.rcFile, dir: opts.cwd }));
135
- const workspaceDir = await pkgTypes.findWorkspaceDir(opts.cwd).catch(() => null);
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: opts.rcFile, dir: workspaceDir }));
131
+ Object.assign(configRC, rc9__namespace.read({ name: options.rcFile, dir: workspaceDir }));
138
132
  }
139
133
  }
140
- Object.assign(configRC, rc9__namespace.read({ name: opts.rcFile, dir: opts.cwd }));
134
+ Object.assign(configRC, rc9__namespace.read({ name: options.rcFile, dir: options.cwd }));
141
135
  }
142
136
  r.config = defu.defu(
143
- opts.overrides,
137
+ options.overrides,
144
138
  config,
145
139
  configRC,
146
- opts.defaultConfig
140
+ options.defaultConfig
147
141
  );
148
- if (opts.extend) {
149
- await extendConfig(r.config, opts);
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
- opts.overrides && { config: opts.overrides, configFile: void 0, cwd: void 0 },
159
- { config, configFile: opts.configFile, cwd: opts.cwd },
160
- opts.rcFile && { config: configRC, configFile: opts.rcFile }
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 (opts.defaults) {
167
- r.config = defu.defu(r.config, opts.defaults);
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, opts) {
165
+ async function extendConfig(config, options) {
172
166
  config._layers = config._layers || [];
173
- if (!opts.extend) {
167
+ if (!options.extend) {
174
168
  return;
175
169
  }
176
- let keys = opts.extend.extendKey;
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 ${opts.cwd}`);
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, opts);
184
+ const _config = await resolveConfig(extendSource, options);
191
185
  if (!_config.config) {
192
- console.warn(`Cannot extend config from \`${extendSource}\` in ${opts.cwd}`);
186
+ console.warn(`Cannot extend config from \`${extendSource}\` in ${options.cwd}`);
193
187
  continue;
194
188
  }
195
- await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
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 = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
205
- async function resolveConfig(source, opts) {
206
- if (opts.resolve) {
207
- const res2 = await opts.resolve(source, opts);
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(os__default.tmpdir(), "c12/", gitRepo.replace(/[#:@/\\]/g, "_"));
217
- await fs.promises.rm(tmpdir, { recursive: true }).catch(() => {
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 = opts.jiti.resolve(source, { paths: [opts.cwd] });
227
- } catch (_err) {
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(opts.cwd, isDir ? source : pathe.dirname(source));
225
+ const cwd = pathe.resolve(options.cwd, isDir ? source : pathe.dirname(source));
232
226
  if (isDir) {
233
- source = opts.configFile;
227
+ source = options.configFile;
234
228
  }
235
- const res = { config: null, cwd };
229
+ const res = { config: void 0, cwd };
236
230
  try {
237
- res.configFile = opts.jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
238
- } catch (_err) {
231
+ res.configFile = options.jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
232
+ } catch {
239
233
  }
240
- if (!fs.existsSync(res.configFile)) {
234
+ if (!node_fs.existsSync(res.configFile)) {
241
235
  return res;
242
236
  }
243
- res.config = opts.jiti(res.configFile);
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(opts: DotenvOptions): Promise<Env>;
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, opts: LoadConfigOptions) => null | ResolvedConfig | Promise<ResolvedConfig | null>;
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>(opts: LoadConfigOptions<T>): Promise<ResolvedConfig<T>>;
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 targetEnv = options.env ?? process.env;
12
- const env = await loadDotenv({
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: targetEnv,
15
+ env: targetEnvironment,
16
16
  interpolate: options.interpolate ?? true
17
17
  });
18
- for (const key in env) {
19
- if (!key.startsWith("_") && targetEnv[key] === void 0) {
20
- targetEnv[key] = env[key];
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 env;
23
+ return environment;
24
24
  }
25
- async function loadDotenv(opts) {
26
- const env = /* @__PURE__ */ Object.create(null);
27
- const dotenvFile = resolve(opts.cwd, opts.fileName);
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, "utf-8"));
30
- Object.assign(env, parsed);
29
+ const parsed = dotenv.parse(await promises.readFile(dotenvFile, "utf8"));
30
+ Object.assign(environment, parsed);
31
31
  }
32
- if (!opts.env._applied) {
33
- Object.assign(env, opts.env);
34
- env._applied = true;
32
+ if (!options.env._applied) {
33
+ Object.assign(environment, options.env);
34
+ environment._applied = true;
35
35
  }
36
- if (opts.interpolate) {
37
- interpolate(env);
36
+ if (options.interpolate) {
37
+ interpolate(environment);
38
38
  }
39
- return env;
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(/(.?\${?(?:[a-zA-Z0-9_:]+)?}?)/g) || [];
49
+ const matches = value.match(/(.?\${?(?:[\w:]+)?}?)/g) || [];
50
50
  return parse(matches.reduce((newValue, match) => {
51
- const parts = /(.?)\${?([a-zA-Z0-9_:]+)?}?/g.exec(match);
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].substring(prefix.length);
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(opts) {
76
- opts.cwd = resolve(process.cwd(), opts.cwd || ".");
77
- opts.name = opts.name || "config";
78
- opts.configFile = opts.configFile ?? (opts.name !== "config" ? `${opts.name}.config` : "config");
79
- opts.rcFile = opts.rcFile ?? `.${opts.name}rc`;
80
- if (opts.extend !== false) {
81
- opts.extend = {
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
- ...opts.extend
83
+ ...options.extend
84
84
  };
85
85
  }
86
- opts.jiti = opts.jiti || createJiti(null, {
86
+ options.jiti = options.jiti || createJiti(void 0, {
87
87
  interopDefault: true,
88
88
  requireCache: false,
89
89
  esmResolve: true,
90
- ...opts.jitiOptions
90
+ ...options.jitiOptions
91
91
  });
92
92
  const r = {
93
93
  config: {},
94
- cwd: opts.cwd,
95
- configFile: resolve(opts.cwd, opts.configFile),
94
+ cwd: options.cwd,
95
+ configFile: resolve(options.cwd, options.configFile),
96
96
  layers: []
97
97
  };
98
- if (opts.dotenv) {
98
+ if (options.dotenv) {
99
99
  await setupDotenv({
100
- cwd: opts.cwd,
101
- ...opts.dotenv === true ? {} : opts.dotenv
100
+ cwd: options.cwd,
101
+ ...options.dotenv === true ? {} : options.dotenv
102
102
  });
103
103
  }
104
- const { config, configFile } = await resolveConfig(".", opts);
104
+ const { config, configFile } = await resolveConfig(".", options);
105
105
  if (configFile) {
106
106
  r.configFile = configFile;
107
107
  }
108
108
  const configRC = {};
109
- if (opts.rcFile) {
110
- if (opts.globalRc) {
111
- Object.assign(configRC, rc9.readUser({ name: opts.rcFile, dir: opts.cwd }));
112
- const workspaceDir = await findWorkspaceDir(opts.cwd).catch(() => null);
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: opts.rcFile, dir: workspaceDir }));
115
+ Object.assign(configRC, rc9.read({ name: options.rcFile, dir: workspaceDir }));
115
116
  }
116
117
  }
117
- Object.assign(configRC, rc9.read({ name: opts.rcFile, dir: opts.cwd }));
118
+ Object.assign(configRC, rc9.read({ name: options.rcFile, dir: options.cwd }));
118
119
  }
119
120
  r.config = defu(
120
- opts.overrides,
121
+ options.overrides,
121
122
  config,
122
123
  configRC,
123
- opts.defaultConfig
124
+ options.defaultConfig
124
125
  );
125
- if (opts.extend) {
126
- await extendConfig(r.config, opts);
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
- opts.overrides && { config: opts.overrides, configFile: void 0, cwd: void 0 },
136
- { config, configFile: opts.configFile, cwd: opts.cwd },
137
- opts.rcFile && { config: configRC, configFile: opts.rcFile }
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 (opts.defaults) {
144
- r.config = defu(r.config, opts.defaults);
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, opts) {
149
+ async function extendConfig(config, options) {
149
150
  config._layers = config._layers || [];
150
- if (!opts.extend) {
151
+ if (!options.extend) {
151
152
  return;
152
153
  }
153
- let keys = opts.extend.extendKey;
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 ${opts.cwd}`);
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, opts);
168
+ const _config = await resolveConfig(extendSource, options);
168
169
  if (!_config.config) {
169
- console.warn(`Cannot extend config from \`${extendSource}\` in ${opts.cwd}`);
170
+ console.warn(`Cannot extend config from \`${extendSource}\` in ${options.cwd}`);
170
171
  continue;
171
172
  }
172
- await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
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 = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
182
- async function resolveConfig(source, opts) {
183
- if (opts.resolve) {
184
- const res2 = await opts.resolve(source, opts);
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(/[#:@/\\]/g, "_"));
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 = opts.jiti.resolve(source, { paths: [opts.cwd] });
204
- } catch (_err) {
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(opts.cwd, isDir ? source : dirname(source));
209
+ const cwd = resolve(options.cwd, isDir ? source : dirname(source));
209
210
  if (isDir) {
210
- source = opts.configFile;
211
+ source = options.configFile;
211
212
  }
212
- const res = { config: null, cwd };
213
+ const res = { config: void 0, cwd };
213
214
  try {
214
- res.configFile = opts.jiti.resolve(resolve(cwd, source), { paths: [cwd] });
215
- } catch (_err) {
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 = opts.jiti(res.configFile);
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.2.13",
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.0",
23
- "dotenv": "^16.0.2",
23
+ "defu": "^6.1.1",
24
+ "dotenv": "^16.0.3",
24
25
  "gittar": "^0.1.1",
25
- "jiti": "^1.15.0",
26
- "mlly": "^0.5.14",
27
- "pathe": "^0.3.8",
28
- "pkg-types": "^0.3.5",
29
- "rc9": "^1.2.2"
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
- "@nuxtjs/eslint-config-typescript": "^11.0.0",
33
- "@vitest/coverage-c8": "^0.23.4",
34
- "eslint": "^8.23.1",
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.3",
37
- "unbuild": "^0.8.11",
38
- "vitest": "^0.23.4"
37
+ "typescript": "^4.8.4",
38
+ "unbuild": "^0.9.4",
39
+ "vitest": "^0.25.2"
39
40
  },
40
- "packageManager": "pnpm@7.12.0",
41
+ "packageManager": "pnpm@7.16.0",
41
42
  "scripts": {
42
43
  "build": "unbuild",
43
44
  "dev": "vitest dev",