@metajoy/config 1.0.1 → 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.mjs CHANGED
@@ -1,19 +1,78 @@
1
- import { loadConfig as loadConfig$1, watchConfig as watchConfig$1 } from "c12";
2
- import { merge } from "lodash-es";
3
- //#region src/index.ts
4
- function defineConfig(config) {
5
- return config;
6
- }
7
- const _options = {
8
- name: "metajoy",
9
- packageJson: true,
10
- defaults: { api: {} }
11
- };
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
+ */
12
11
  async function loadConfig(options) {
13
- return await loadConfig$1(merge({}, _options, 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));
23
+ }
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);
29
+ }
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;
55
+ }
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);
66
+ });
67
+ }
68
+ //#endregion
69
+ //#region src/env.ts
70
+ let env = {};
71
+ function loadEnv(mode, envDir, prefixes) {
72
+ return env = loadEnv$1(mode, envDir, prefixes);
14
73
  }
15
- async function watchConfig(options) {
16
- return await watchConfig$1(merge({}, _options, options));
74
+ function getEnv(key) {
75
+ return env[key];
17
76
  }
18
77
  //#endregion
19
- export { defineConfig, loadConfig, watchConfig };
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.1",
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,14 +16,20 @@
15
16
  },
16
17
  "dependencies": {
17
18
  "c12": "4.0.0-beta.5",
19
+ "chokidar": "^5.0.0",
20
+ "dotenv-expand": "^13.0.0",
21
+ "giget": "^3.2.0",
22
+ "hookable": "^6.1.1",
23
+ "jiti": "^2.7.0",
18
24
  "lodash-es": "^4.18.1"
19
25
  },
20
26
  "devDependencies": {
21
27
  "@types/lodash-es": "^4.17.12",
22
- "giget": "^3.2.0"
28
+ "type-fest": "^5.6.0"
23
29
  },
24
30
  "inlinedDependencies": {
25
- "giget": "3.2.0"
31
+ "tagged-tag": "1.0.0",
32
+ "type-fest": "5.7.0"
26
33
  },
27
34
  "scripts": {
28
35
  "build": "vp pack",