@metajoy/config 1.0.2 → 1.0.3
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-asImb96q.d.mts +45 -0
- package/dist/index.d.mts +2670 -106
- package/dist/index.mjs +69 -71
- package/dist/utils/index.d.mts +2 -0
- package/dist/utils/index.mjs +80 -0
- package/package.json +9 -3
package/dist/index.mjs
CHANGED
|
@@ -1,80 +1,78 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { defineConfig, loadEnv as loadEnv$1, watchConfig as watchConfig$1 } from "./utils/index.mjs";
|
|
2
|
+
import { createHooks } from "hookable";
|
|
3
|
+
import { assign, get, isString, merge } from "lodash-es";
|
|
4
|
+
//#region src/config.ts
|
|
5
|
+
let config;
|
|
6
|
+
const hooks = createHooks();
|
|
7
|
+
/**
|
|
8
|
+
* 加载配置
|
|
9
|
+
* @returns 配置对象
|
|
10
|
+
*/
|
|
11
|
+
async function loadConfig(options) {
|
|
12
|
+
return config = config ?? await watchConfig$1(merge({
|
|
13
|
+
async acceptHMR(...args) {
|
|
14
|
+
await hooks.callHook("acceptHMR", ...args);
|
|
15
|
+
},
|
|
16
|
+
async onUpdate(...args) {
|
|
17
|
+
await hooks.callHook("onUpdate", ...args);
|
|
18
|
+
},
|
|
19
|
+
async onWatch(...args) {
|
|
20
|
+
await hooks.callHook("onWatch", ...args);
|
|
21
|
+
}
|
|
22
|
+
}, options));
|
|
18
23
|
}
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
function getConfig(pathOrOptions, mayBeOptions) {
|
|
25
|
+
const path = isString(pathOrOptions) ? pathOrOptions : void 0;
|
|
26
|
+
const options = isString(pathOrOptions) ? mayBeOptions : pathOrOptions;
|
|
27
|
+
if (path) return merge({}, options?.defaults, get(config?.config, path), options?.overrides);
|
|
28
|
+
return merge({}, options?.defaults, config?.config, options?.overrides);
|
|
23
29
|
}
|
|
24
|
-
function
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
function useConfig(pathOrOptions, mayBeOptions) {
|
|
31
|
+
const path = isString(pathOrOptions) ? pathOrOptions : void 0;
|
|
32
|
+
const options = isString(pathOrOptions) ? mayBeOptions : pathOrOptions;
|
|
33
|
+
if (path) {
|
|
34
|
+
const result = getConfig(path, options);
|
|
35
|
+
const resultRef = new WeakRef(result);
|
|
36
|
+
function onUpdate({ newConfig }) {
|
|
37
|
+
const result = resultRef.deref();
|
|
38
|
+
if (result) assign(result, options?.defaults, get(newConfig, path), options?.overrides);
|
|
39
|
+
else hooks.removeHook("onUpdate", onUpdate);
|
|
40
|
+
}
|
|
41
|
+
new FinalizationRegistry((value) => hooks.removeHook("onUpdate", value)).register(result, onUpdate);
|
|
42
|
+
hooks.hook("onUpdate", onUpdate);
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
const result = getConfig(options);
|
|
46
|
+
const resultRef = new WeakRef(result);
|
|
47
|
+
function onUpdate({ newConfig }) {
|
|
48
|
+
const result = resultRef.deref();
|
|
49
|
+
if (result) assign(result, options?.defaults, newConfig, options?.overrides);
|
|
50
|
+
else hooks.removeHook("onUpdate", onUpdate);
|
|
51
|
+
}
|
|
52
|
+
new FinalizationRegistry((value) => hooks.removeHook("onUpdate", value)).register(result, onUpdate);
|
|
53
|
+
hooks.hook("onUpdate", onUpdate);
|
|
54
|
+
return result;
|
|
32
55
|
}
|
|
33
|
-
function
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}));
|
|
44
|
-
if (parsed.NODE_ENV && process.env.VITE_USER_NODE_ENV === void 0) process.env.VITE_USER_NODE_ENV = parsed.NODE_ENV;
|
|
45
|
-
if (parsed.BROWSER && process.env.BROWSER === void 0) process.env.BROWSER = parsed.BROWSER;
|
|
46
|
-
if (parsed.BROWSER_ARGS && process.env.BROWSER_ARGS === void 0) process.env.BROWSER_ARGS = parsed.BROWSER_ARGS;
|
|
47
|
-
expand({
|
|
48
|
-
parsed,
|
|
49
|
-
processEnv: { ...process.env }
|
|
56
|
+
function watchConfig(pathOrCallback, mayBeCallback) {
|
|
57
|
+
const path = typeof pathOrCallback === "string" ? pathOrCallback : void 0;
|
|
58
|
+
const callback = typeof pathOrCallback === "string" ? mayBeCallback : pathOrCallback;
|
|
59
|
+
return hooks.hook("onUpdate", ({ getDiff, newConfig, oldConfig }) => {
|
|
60
|
+
const diffs = getDiff();
|
|
61
|
+
if (path && diffs.some((diff) => diff.key.startsWith(path))) {
|
|
62
|
+
callback?.(get(newConfig, path), get(oldConfig, path));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
callback?.(newConfig, oldConfig);
|
|
50
66
|
});
|
|
51
|
-
for (const [key, value] of Object.entries(parsed)) if (prefixes.some((prefix) => key.startsWith(prefix))) env[key] = value;
|
|
52
|
-
for (const key in process.env) if (prefixes.some((prefix) => key.startsWith(prefix))) env[key] = process.env[key];
|
|
53
|
-
return env;
|
|
54
67
|
}
|
|
55
68
|
//#endregion
|
|
56
|
-
//#region src/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
defaults: { api: {} }
|
|
61
|
-
};
|
|
62
|
-
function defineConfig(config) {
|
|
63
|
-
return config;
|
|
64
|
-
}
|
|
65
|
-
async function loadConfig(options) {
|
|
66
|
-
return await loadConfig$1(merge({}, _options, { dotenv: {
|
|
67
|
-
env: loadEnv(isString(options?.envName) ? options.envName : void 0),
|
|
68
|
-
fileName: getEnvFilesForMode(isString(options?.envName) ? options.envName : void 0),
|
|
69
|
-
expandFileReferences: true
|
|
70
|
-
} }, options));
|
|
69
|
+
//#region src/env.ts
|
|
70
|
+
let env = {};
|
|
71
|
+
function loadEnv(mode, envDir, prefixes) {
|
|
72
|
+
return env = loadEnv$1(mode, envDir, prefixes);
|
|
71
73
|
}
|
|
72
|
-
|
|
73
|
-
return
|
|
74
|
-
env: loadEnv(isString(options?.envName) ? options.envName : void 0),
|
|
75
|
-
fileName: getEnvFilesForMode(isString(options?.envName) ? options.envName : void 0),
|
|
76
|
-
expandFileReferences: true
|
|
77
|
-
} }, options));
|
|
74
|
+
function getEnv(key) {
|
|
75
|
+
return env[key];
|
|
78
76
|
}
|
|
79
77
|
//#endregion
|
|
80
|
-
export { defineConfig,
|
|
78
|
+
export { defineConfig, getConfig, getEnv, loadConfig, loadEnv, useConfig, watchConfig };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as MetajoyConfigDefaults, c as loadConfig, i as MetajoyConfig, l as watchConfig, n as loadEnv, o as MetajoyConfigWithDefaults, r as ApiConfig, s as defineConfig, t as getEnvFilesForMode } from "../index-asImb96q.mjs";
|
|
2
|
+
export { ApiConfig, MetajoyConfig, MetajoyConfigDefaults, MetajoyConfigWithDefaults, defineConfig, getEnvFilesForMode, loadConfig, loadEnv, watchConfig };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { isString, merge } from "lodash-es";
|
|
2
|
+
import { loadConfig as loadConfig$1, watchConfig as watchConfig$1 } from "c12";
|
|
3
|
+
import { expand } from "dotenv-expand";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { parseEnv } from "node:util";
|
|
7
|
+
//#region src/utils/env.ts
|
|
8
|
+
const isWindows = typeof process !== "undefined" && process.platform === "win32";
|
|
9
|
+
const windowsSlashRE = /\\/g;
|
|
10
|
+
function slash(p) {
|
|
11
|
+
return p.replace(windowsSlashRE, "/");
|
|
12
|
+
}
|
|
13
|
+
function normalizePath(id) {
|
|
14
|
+
return path.posix.normalize(isWindows ? slash(id) : id);
|
|
15
|
+
}
|
|
16
|
+
function arraify(target) {
|
|
17
|
+
return Array.isArray(target) ? target : [target];
|
|
18
|
+
}
|
|
19
|
+
function tryStatSync(file) {
|
|
20
|
+
try {
|
|
21
|
+
return fs.statSync(file, { throwIfNoEntry: false });
|
|
22
|
+
} catch {}
|
|
23
|
+
}
|
|
24
|
+
function getEnvFilesForMode(mode = process.env.NODE_ENV || "development", envDir = process.cwd()) {
|
|
25
|
+
if (envDir !== false) return [
|
|
26
|
+
`.env`,
|
|
27
|
+
`.env.local`,
|
|
28
|
+
`.env.${mode}`,
|
|
29
|
+
`.env.${mode}.local`
|
|
30
|
+
].map((file) => normalizePath(path.join(envDir, file)));
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
function loadEnv(mode = process.env.NODE_ENV || "development", envDir = process.cwd(), prefixes = "METAJOY_") {
|
|
34
|
+
if (mode === "local") throw new Error("\"local\" cannot be used as a mode name because it conflicts with the .local postfix for .env files.");
|
|
35
|
+
prefixes = arraify(prefixes);
|
|
36
|
+
const env = {};
|
|
37
|
+
const envFiles = getEnvFilesForMode(mode, envDir);
|
|
38
|
+
const parsed = Object.fromEntries(envFiles.flatMap((filePath) => {
|
|
39
|
+
const stat = tryStatSync(filePath);
|
|
40
|
+
if (!stat || !stat.isFile() && !stat.isFIFO()) return [];
|
|
41
|
+
const parsedEnv = parseEnv(fs.readFileSync(filePath, "utf-8"));
|
|
42
|
+
return Object.entries(parsedEnv);
|
|
43
|
+
}));
|
|
44
|
+
if (parsed.NODE_ENV && process.env.VITE_USER_NODE_ENV === void 0) process.env.VITE_USER_NODE_ENV = parsed.NODE_ENV;
|
|
45
|
+
if (parsed.BROWSER && process.env.BROWSER === void 0) process.env.BROWSER = parsed.BROWSER;
|
|
46
|
+
if (parsed.BROWSER_ARGS && process.env.BROWSER_ARGS === void 0) process.env.BROWSER_ARGS = parsed.BROWSER_ARGS;
|
|
47
|
+
expand({
|
|
48
|
+
parsed,
|
|
49
|
+
processEnv: { ...process.env }
|
|
50
|
+
});
|
|
51
|
+
for (const [key, value] of Object.entries(parsed)) if (prefixes.some((prefix) => key.startsWith(prefix))) env[key] = value;
|
|
52
|
+
for (const key in process.env) if (prefixes.some((prefix) => key.startsWith(prefix))) env[key] = process.env[key];
|
|
53
|
+
return env;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/utils/config.ts
|
|
57
|
+
const _options = {
|
|
58
|
+
name: "metajoy",
|
|
59
|
+
packageJson: true,
|
|
60
|
+
defaults: { api: {} }
|
|
61
|
+
};
|
|
62
|
+
function defineConfig(config) {
|
|
63
|
+
return config;
|
|
64
|
+
}
|
|
65
|
+
async function loadConfig(options) {
|
|
66
|
+
return await loadConfig$1(merge({}, _options, { dotenv: {
|
|
67
|
+
env: loadEnv(isString(options?.envName) ? options.envName : void 0),
|
|
68
|
+
fileName: getEnvFilesForMode(isString(options?.envName) ? options.envName : void 0),
|
|
69
|
+
expandFileReferences: true
|
|
70
|
+
} }, options));
|
|
71
|
+
}
|
|
72
|
+
async function watchConfig(options) {
|
|
73
|
+
return await watchConfig$1(merge({}, _options, { dotenv: {
|
|
74
|
+
env: loadEnv(isString(options?.envName) ? options.envName : void 0),
|
|
75
|
+
fileName: getEnvFilesForMode(isString(options?.envName) ? options.envName : void 0),
|
|
76
|
+
expandFileReferences: true
|
|
77
|
+
} }, options));
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
export { defineConfig, getEnvFilesForMode, loadConfig, loadEnv, watchConfig };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metajoy/config",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Metajoy 项目的配置工具",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"type": "module",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": "./dist/index.mjs",
|
|
11
|
+
"./utils": "./dist/utils/index.mjs",
|
|
11
12
|
"./package.json": "./package.json"
|
|
12
13
|
},
|
|
13
14
|
"publishConfig": {
|
|
@@ -15,15 +16,20 @@
|
|
|
15
16
|
},
|
|
16
17
|
"dependencies": {
|
|
17
18
|
"c12": "4.0.0-beta.5",
|
|
19
|
+
"chokidar": "^5.0.0",
|
|
18
20
|
"dotenv-expand": "^13.0.0",
|
|
21
|
+
"giget": "^3.2.0",
|
|
22
|
+
"hookable": "^6.1.1",
|
|
23
|
+
"jiti": "^2.7.0",
|
|
19
24
|
"lodash-es": "^4.18.1"
|
|
20
25
|
},
|
|
21
26
|
"devDependencies": {
|
|
22
27
|
"@types/lodash-es": "^4.17.12",
|
|
23
|
-
"
|
|
28
|
+
"type-fest": "^5.6.0"
|
|
24
29
|
},
|
|
25
30
|
"inlinedDependencies": {
|
|
26
|
-
"
|
|
31
|
+
"tagged-tag": "1.0.0",
|
|
32
|
+
"type-fest": "5.7.0"
|
|
27
33
|
},
|
|
28
34
|
"scripts": {
|
|
29
35
|
"build": "vp pack",
|