c12 1.4.2 → 1.5.0

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 CHANGED
@@ -8,10 +8,10 @@ const node_os = require('node:os');
8
8
  const createJiti = require('jiti');
9
9
  const rc9 = require('rc9');
10
10
  const defu = require('defu');
11
+ const ohash = require('ohash');
11
12
  const pkgTypes = require('pkg-types');
12
13
  const chokidar = require('chokidar');
13
14
  const perfectDebounce = require('perfect-debounce');
14
- const ohash = require('ohash');
15
15
 
16
16
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
17
17
 
@@ -249,7 +249,7 @@ async function extendConfig(config, options) {
249
249
  }
250
250
  }
251
251
  }
252
- const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
252
+ const GIT_PREFIXES = ["gh:", "github:", "gitlab:", "bitbucket:", "https://"];
253
253
  const NPM_PACKAGE_RE = /^(@[\da-z~-][\d._a-z~-]*\/)?[\da-z~-][\d._a-z~-]*($|\/.*)/;
254
254
  async function resolveConfig(source, options, sourceOptions = {}) {
255
255
  if (options.resolve) {
@@ -260,14 +260,18 @@ async function resolveConfig(source, options, sourceOptions = {}) {
260
260
  }
261
261
  if (GIT_PREFIXES.some((prefix) => source.startsWith(prefix))) {
262
262
  const { downloadTemplate } = await import('giget');
263
- const url = new URL(source);
264
- const gitRepo = url.protocol + url.pathname.split("/").slice(0, 2).join("/");
265
- const name = gitRepo.replace(/[#/:@\\]/g, "_");
266
- const tmpDir = process.env.XDG_CACHE_HOME ? pathe.resolve(process.env.XDG_CACHE_HOME, "c12", name) : pathe.resolve(node_os.homedir(), ".cache/c12", name);
267
- if (node_fs.existsSync(tmpDir)) {
268
- await promises.rm(tmpDir, { recursive: true });
263
+ const cloneName = source.replace(/\W+/g, "_").split("_").splice(0, 3).join("_") + "_" + ohash.hash(source);
264
+ let cloneDir;
265
+ const localNodeModules = pathe.resolve(options.cwd, "node_modules");
266
+ if (node_fs.existsSync(localNodeModules)) {
267
+ cloneDir = pathe.join(localNodeModules, ".c12", cloneName);
268
+ } else {
269
+ cloneDir = process.env.XDG_CACHE_HOME ? pathe.resolve(process.env.XDG_CACHE_HOME, "c12", cloneName) : pathe.resolve(node_os.homedir(), ".cache/c12", cloneName);
270
+ }
271
+ if (node_fs.existsSync(cloneDir)) {
272
+ await promises.rm(cloneDir, { recursive: true });
269
273
  }
270
- const cloned = await downloadTemplate(source, { dir: tmpDir });
274
+ const cloned = await downloadTemplate(source, { dir: cloneDir });
271
275
  source = cloned.dir;
272
276
  }
273
277
  if (NPM_PACKAGE_RE.test(source)) {
@@ -0,0 +1,120 @@
1
+ import { JITI } from 'jiti';
2
+ import { JITIOptions } from 'jiti/dist/types';
3
+ import { WatchOptions } from 'chokidar';
4
+ import { diff } from 'ohash';
5
+
6
+ interface DotenvOptions {
7
+ /**
8
+ * The project root directory (either absolute or relative to the current working directory).
9
+ */
10
+ cwd: string;
11
+ /**
12
+ * What file to look in for environment variables (either absolute or relative
13
+ * to the current working directory). For example, `.env`.
14
+ */
15
+ fileName?: string;
16
+ /**
17
+ * Whether to interpolate variables within .env.
18
+ *
19
+ * @example
20
+ * ```env
21
+ * BASE_DIR="/test"
22
+ * # resolves to "/test/further"
23
+ * ANOTHER_DIR="${BASE_DIR}/further"
24
+ * ```
25
+ */
26
+ interpolate?: boolean;
27
+ /**
28
+ * An object describing environment variables (key, value pairs).
29
+ */
30
+ env?: NodeJS.ProcessEnv;
31
+ }
32
+ type Env = typeof process.env;
33
+ /**
34
+ * Load and interpolate environment variables into `process.env`.
35
+ * If you need more control (or access to the values), consider using `loadDotenv` instead
36
+ *
37
+ */
38
+ declare function setupDotenv(options: DotenvOptions): Promise<Env>;
39
+ /** Load environment variables into an object. */
40
+ declare function loadDotenv(options: DotenvOptions): Promise<Env>;
41
+
42
+ interface ConfigLayerMeta {
43
+ name?: string;
44
+ [key: string]: any;
45
+ }
46
+ type UserInputConfig = Record<string, any>;
47
+ interface C12InputConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> {
48
+ $test?: T;
49
+ $development?: T;
50
+ $production?: T;
51
+ $env?: Record<string, T>;
52
+ $meta?: MT;
53
+ }
54
+ type InputConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> = C12InputConfig<T, MT> & T;
55
+ interface SourceOptions<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> {
56
+ meta?: MT;
57
+ overrides?: T;
58
+ [key: string]: any;
59
+ }
60
+ interface ConfigLayer<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> {
61
+ config: T | null;
62
+ source?: string;
63
+ sourceOptions?: SourceOptions<T, MT>;
64
+ meta?: MT;
65
+ cwd?: string;
66
+ configFile?: string;
67
+ }
68
+ interface ResolvedConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> extends ConfigLayer<T, MT> {
69
+ layers?: ConfigLayer<T, MT>[];
70
+ cwd?: string;
71
+ }
72
+ interface LoadConfigOptions<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> {
73
+ name?: string;
74
+ cwd?: string;
75
+ configFile?: string;
76
+ rcFile?: false | string;
77
+ globalRc?: boolean;
78
+ dotenv?: boolean | DotenvOptions;
79
+ envName?: string | false;
80
+ packageJson?: boolean | string | string[];
81
+ defaults?: T;
82
+ defaultConfig?: T;
83
+ overrides?: T;
84
+ resolve?: (id: string, options: LoadConfigOptions<T, MT>) => null | undefined | ResolvedConfig<T, MT> | Promise<ResolvedConfig<T, MT> | undefined | null>;
85
+ jiti?: JITI;
86
+ jitiOptions?: JITIOptions;
87
+ extend?: false | {
88
+ extendKey?: string | string[];
89
+ };
90
+ }
91
+ type DefineConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> = (input: InputConfig<T, MT>) => InputConfig<T, MT>;
92
+ declare function createDefineConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta>(): DefineConfig<T, MT>;
93
+
94
+ declare function loadConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta>(options: LoadConfigOptions<T, MT>): Promise<ResolvedConfig<T, MT>>;
95
+
96
+ type ConfigWatcher<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> = ResolvedConfig<T, MT> & {
97
+ watchingFiles: string[];
98
+ unwatch: () => Promise<void>;
99
+ };
100
+ interface WatchConfigOptions<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> extends LoadConfigOptions<T, MT> {
101
+ chokidarOptions?: WatchOptions;
102
+ debounce?: false | number;
103
+ onWatch?: (event: {
104
+ type: "created" | "updated" | "removed";
105
+ path: string;
106
+ }) => void | Promise<void>;
107
+ acceptHMR?: (context: {
108
+ getDiff: () => ReturnType<typeof diff>;
109
+ newConfig: ResolvedConfig<T, MT>;
110
+ oldConfig: ResolvedConfig<T, MT>;
111
+ }) => void | boolean | Promise<void | boolean>;
112
+ onUpdate?: (context: {
113
+ getDiff: () => ReturnType<typeof diff>;
114
+ newConfig: ResolvedConfig<T, MT>;
115
+ oldConfig: ResolvedConfig<T, MT>;
116
+ }) => void | Promise<void>;
117
+ }
118
+ declare function watchConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta>(options: WatchConfigOptions<T, MT>): Promise<ConfigWatcher<T, MT>>;
119
+
120
+ export { type C12InputConfig, type ConfigLayer, type ConfigLayerMeta, type ConfigWatcher, type DefineConfig, type DotenvOptions, type Env, type InputConfig, type LoadConfigOptions, type ResolvedConfig, type SourceOptions, type UserInputConfig, type WatchConfigOptions, createDefineConfig, loadConfig, loadDotenv, setupDotenv, watchConfig };
@@ -0,0 +1,120 @@
1
+ import { JITI } from 'jiti';
2
+ import { JITIOptions } from 'jiti/dist/types';
3
+ import { WatchOptions } from 'chokidar';
4
+ import { diff } from 'ohash';
5
+
6
+ interface DotenvOptions {
7
+ /**
8
+ * The project root directory (either absolute or relative to the current working directory).
9
+ */
10
+ cwd: string;
11
+ /**
12
+ * What file to look in for environment variables (either absolute or relative
13
+ * to the current working directory). For example, `.env`.
14
+ */
15
+ fileName?: string;
16
+ /**
17
+ * Whether to interpolate variables within .env.
18
+ *
19
+ * @example
20
+ * ```env
21
+ * BASE_DIR="/test"
22
+ * # resolves to "/test/further"
23
+ * ANOTHER_DIR="${BASE_DIR}/further"
24
+ * ```
25
+ */
26
+ interpolate?: boolean;
27
+ /**
28
+ * An object describing environment variables (key, value pairs).
29
+ */
30
+ env?: NodeJS.ProcessEnv;
31
+ }
32
+ type Env = typeof process.env;
33
+ /**
34
+ * Load and interpolate environment variables into `process.env`.
35
+ * If you need more control (or access to the values), consider using `loadDotenv` instead
36
+ *
37
+ */
38
+ declare function setupDotenv(options: DotenvOptions): Promise<Env>;
39
+ /** Load environment variables into an object. */
40
+ declare function loadDotenv(options: DotenvOptions): Promise<Env>;
41
+
42
+ interface ConfigLayerMeta {
43
+ name?: string;
44
+ [key: string]: any;
45
+ }
46
+ type UserInputConfig = Record<string, any>;
47
+ interface C12InputConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> {
48
+ $test?: T;
49
+ $development?: T;
50
+ $production?: T;
51
+ $env?: Record<string, T>;
52
+ $meta?: MT;
53
+ }
54
+ type InputConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> = C12InputConfig<T, MT> & T;
55
+ interface SourceOptions<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> {
56
+ meta?: MT;
57
+ overrides?: T;
58
+ [key: string]: any;
59
+ }
60
+ interface ConfigLayer<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> {
61
+ config: T | null;
62
+ source?: string;
63
+ sourceOptions?: SourceOptions<T, MT>;
64
+ meta?: MT;
65
+ cwd?: string;
66
+ configFile?: string;
67
+ }
68
+ interface ResolvedConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> extends ConfigLayer<T, MT> {
69
+ layers?: ConfigLayer<T, MT>[];
70
+ cwd?: string;
71
+ }
72
+ interface LoadConfigOptions<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> {
73
+ name?: string;
74
+ cwd?: string;
75
+ configFile?: string;
76
+ rcFile?: false | string;
77
+ globalRc?: boolean;
78
+ dotenv?: boolean | DotenvOptions;
79
+ envName?: string | false;
80
+ packageJson?: boolean | string | string[];
81
+ defaults?: T;
82
+ defaultConfig?: T;
83
+ overrides?: T;
84
+ resolve?: (id: string, options: LoadConfigOptions<T, MT>) => null | undefined | ResolvedConfig<T, MT> | Promise<ResolvedConfig<T, MT> | undefined | null>;
85
+ jiti?: JITI;
86
+ jitiOptions?: JITIOptions;
87
+ extend?: false | {
88
+ extendKey?: string | string[];
89
+ };
90
+ }
91
+ type DefineConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> = (input: InputConfig<T, MT>) => InputConfig<T, MT>;
92
+ declare function createDefineConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta>(): DefineConfig<T, MT>;
93
+
94
+ declare function loadConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta>(options: LoadConfigOptions<T, MT>): Promise<ResolvedConfig<T, MT>>;
95
+
96
+ type ConfigWatcher<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> = ResolvedConfig<T, MT> & {
97
+ watchingFiles: string[];
98
+ unwatch: () => Promise<void>;
99
+ };
100
+ interface WatchConfigOptions<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> extends LoadConfigOptions<T, MT> {
101
+ chokidarOptions?: WatchOptions;
102
+ debounce?: false | number;
103
+ onWatch?: (event: {
104
+ type: "created" | "updated" | "removed";
105
+ path: string;
106
+ }) => void | Promise<void>;
107
+ acceptHMR?: (context: {
108
+ getDiff: () => ReturnType<typeof diff>;
109
+ newConfig: ResolvedConfig<T, MT>;
110
+ oldConfig: ResolvedConfig<T, MT>;
111
+ }) => void | boolean | Promise<void | boolean>;
112
+ onUpdate?: (context: {
113
+ getDiff: () => ReturnType<typeof diff>;
114
+ newConfig: ResolvedConfig<T, MT>;
115
+ oldConfig: ResolvedConfig<T, MT>;
116
+ }) => void | Promise<void>;
117
+ }
118
+ declare function watchConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta>(options: WatchConfigOptions<T, MT>): Promise<ConfigWatcher<T, MT>>;
119
+
120
+ export { type C12InputConfig, type ConfigLayer, type ConfigLayerMeta, type ConfigWatcher, type DefineConfig, type DotenvOptions, type Env, type InputConfig, type LoadConfigOptions, type ResolvedConfig, type SourceOptions, type UserInputConfig, type WatchConfigOptions, createDefineConfig, loadConfig, loadDotenv, setupDotenv, watchConfig };
package/dist/index.d.ts CHANGED
@@ -117,4 +117,4 @@ interface WatchConfigOptions<T extends UserInputConfig = UserInputConfig, MT ext
117
117
  }
118
118
  declare function watchConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta>(options: WatchConfigOptions<T, MT>): Promise<ConfigWatcher<T, MT>>;
119
119
 
120
- export { C12InputConfig, ConfigLayer, ConfigLayerMeta, ConfigWatcher, DefineConfig, DotenvOptions, Env, InputConfig, LoadConfigOptions, ResolvedConfig, SourceOptions, UserInputConfig, WatchConfigOptions, createDefineConfig, loadConfig, loadDotenv, setupDotenv, watchConfig };
120
+ export { type C12InputConfig, type ConfigLayer, type ConfigLayerMeta, type ConfigWatcher, type DefineConfig, type DotenvOptions, type Env, type InputConfig, type LoadConfigOptions, type ResolvedConfig, type SourceOptions, type UserInputConfig, type WatchConfigOptions, createDefineConfig, loadConfig, loadDotenv, setupDotenv, watchConfig };
package/dist/index.mjs CHANGED
@@ -1,15 +1,15 @@
1
1
  import { existsSync, promises } from 'node:fs';
2
- import { resolve, extname, basename, dirname } from 'pathe';
2
+ import { resolve, join, extname, basename, dirname } from 'pathe';
3
3
  import * as dotenv from 'dotenv';
4
4
  import { rm } from 'node:fs/promises';
5
5
  import { homedir } from 'node:os';
6
6
  import createJiti from 'jiti';
7
7
  import * as rc9 from 'rc9';
8
8
  import { defu } from 'defu';
9
+ import { hash, diff } from 'ohash';
9
10
  import { findWorkspaceDir, readPackageJSON } from 'pkg-types';
10
11
  import { watch } from 'chokidar';
11
12
  import { debounce } from 'perfect-debounce';
12
- import { diff } from 'ohash';
13
13
 
14
14
  async function setupDotenv(options) {
15
15
  const targetEnvironment = options.env ?? process.env;
@@ -229,7 +229,7 @@ async function extendConfig(config, options) {
229
229
  }
230
230
  }
231
231
  }
232
- const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
232
+ const GIT_PREFIXES = ["gh:", "github:", "gitlab:", "bitbucket:", "https://"];
233
233
  const NPM_PACKAGE_RE = /^(@[\da-z~-][\d._a-z~-]*\/)?[\da-z~-][\d._a-z~-]*($|\/.*)/;
234
234
  async function resolveConfig(source, options, sourceOptions = {}) {
235
235
  if (options.resolve) {
@@ -240,14 +240,18 @@ async function resolveConfig(source, options, sourceOptions = {}) {
240
240
  }
241
241
  if (GIT_PREFIXES.some((prefix) => source.startsWith(prefix))) {
242
242
  const { downloadTemplate } = await import('giget');
243
- const url = new URL(source);
244
- const gitRepo = url.protocol + url.pathname.split("/").slice(0, 2).join("/");
245
- const name = gitRepo.replace(/[#/:@\\]/g, "_");
246
- const tmpDir = process.env.XDG_CACHE_HOME ? resolve(process.env.XDG_CACHE_HOME, "c12", name) : resolve(homedir(), ".cache/c12", name);
247
- if (existsSync(tmpDir)) {
248
- await rm(tmpDir, { recursive: true });
243
+ const cloneName = source.replace(/\W+/g, "_").split("_").splice(0, 3).join("_") + "_" + hash(source);
244
+ let cloneDir;
245
+ const localNodeModules = resolve(options.cwd, "node_modules");
246
+ if (existsSync(localNodeModules)) {
247
+ cloneDir = join(localNodeModules, ".c12", cloneName);
248
+ } else {
249
+ cloneDir = process.env.XDG_CACHE_HOME ? resolve(process.env.XDG_CACHE_HOME, "c12", cloneName) : resolve(homedir(), ".cache/c12", cloneName);
249
250
  }
250
- const cloned = await downloadTemplate(source, { dir: tmpDir });
251
+ if (existsSync(cloneDir)) {
252
+ await rm(cloneDir, { recursive: true });
253
+ }
254
+ const cloned = await downloadTemplate(source, { dir: cloneDir });
251
255
  source = cloned.dir;
252
256
  }
253
257
  if (NPM_PACKAGE_RE.test(source)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c12",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
4
4
  "description": "Smart Config Loader",
5
5
  "repository": "unjs/c12",
6
6
  "license": "MIT",
@@ -33,25 +33,25 @@
33
33
  "chokidar": "^3.5.3",
34
34
  "defu": "^6.1.2",
35
35
  "dotenv": "^16.3.1",
36
- "giget": "^1.1.2",
37
- "jiti": "^1.18.2",
38
- "mlly": "^1.4.0",
39
- "ohash": "^1.1.2",
36
+ "giget": "^1.1.3",
37
+ "jiti": "^1.20.0",
38
+ "mlly": "^1.4.2",
39
+ "ohash": "^1.1.3",
40
40
  "pathe": "^1.1.1",
41
41
  "perfect-debounce": "^1.0.0",
42
42
  "pkg-types": "^1.0.3",
43
43
  "rc9": "^2.1.1"
44
44
  },
45
45
  "devDependencies": {
46
- "@vitest/coverage-v8": "^0.32.2",
47
- "changelogen": "^0.5.3",
48
- "eslint": "^8.43.0",
46
+ "@vitest/coverage-v8": "^0.34.6",
47
+ "changelogen": "^0.5.5",
48
+ "eslint": "^8.51.0",
49
49
  "eslint-config-unjs": "^0.2.1",
50
- "expect-type": "^0.16.0",
51
- "prettier": "^2.8.8",
52
- "typescript": "^5.1.3",
53
- "unbuild": "^1.2.1",
54
- "vitest": "^0.32.2"
50
+ "expect-type": "^0.17.3",
51
+ "prettier": "^3.0.3",
52
+ "typescript": "^5.2.2",
53
+ "unbuild": "^2.0.0",
54
+ "vitest": "^0.34.6"
55
55
  },
56
- "packageManager": "pnpm@8.6.3"
56
+ "packageManager": "pnpm@8.8.0"
57
57
  }