@metajoy/config 1.0.1 → 1.0.2

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.d.mts CHANGED
@@ -97,18 +97,22 @@ interface MetajoyConfig {
97
97
  api?: ApiConfig;
98
98
  }
99
99
  //#endregion
100
+ //#region src/env.d.ts
101
+ declare function getEnvFilesForMode(mode?: string, envDir?: string | false): string[];
102
+ declare function loadEnv(mode?: string, envDir?: string | false, prefixes?: string | string[]): Record<string, string>;
103
+ //#endregion
100
104
  //#region src/index.d.ts
101
- declare function defineConfig(config: InputConfig<MetajoyConfig> & {
102
- extends?: string | (string | [string, DownloadTemplateOptions])[];
103
- }): _$c12.C12InputConfig<MetajoyConfig, _$c12.ConfigLayerMeta> & MetajoyConfig & {
104
- extends?: string | (string | [string, DownloadTemplateOptions])[];
105
- };
106
105
  declare const _defaults: {
107
106
  api: {};
108
107
  };
109
108
  type MetajoyConfigDefaults = typeof _defaults;
110
109
  type MetajoyConfigWithDefaults = MetajoyConfig & MetajoyConfigDefaults;
110
+ declare function defineConfig(config: InputConfig<MetajoyConfig> & {
111
+ extends?: string | (string | [string, DownloadTemplateOptions])[];
112
+ }): _$c12.C12InputConfig<MetajoyConfig, _$c12.ConfigLayerMeta> & MetajoyConfig & {
113
+ extends?: string | (string | [string, DownloadTemplateOptions])[];
114
+ };
111
115
  declare function loadConfig(options?: LoadConfigOptions<MetajoyConfig>): Promise<ResolvedConfig<MetajoyConfigWithDefaults, _$c12.ConfigLayerMeta>>;
112
116
  declare function watchConfig(options?: WatchConfigOptions<MetajoyConfig>): Promise<ConfigWatcher<MetajoyConfigWithDefaults>>;
113
117
  //#endregion
114
- export { type MetajoyConfig, MetajoyConfigDefaults, MetajoyConfigWithDefaults, defineConfig, loadConfig, watchConfig };
118
+ export { type MetajoyConfig, MetajoyConfigDefaults, MetajoyConfigWithDefaults, defineConfig, getEnvFilesForMode, loadConfig, loadEnv, watchConfig };
package/dist/index.mjs CHANGED
@@ -1,19 +1,80 @@
1
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;
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { parseEnv } from "node:util";
5
+ import { isString, merge } from "lodash-es";
6
+ import { expand } from "dotenv-expand";
7
+ //#region src/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 [];
6
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/index.ts
7
57
  const _options = {
8
58
  name: "metajoy",
9
59
  packageJson: true,
10
60
  defaults: { api: {} }
11
61
  };
62
+ function defineConfig(config) {
63
+ return config;
64
+ }
12
65
  async function loadConfig(options) {
13
- return await loadConfig$1(merge({}, _options, 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));
14
71
  }
15
72
  async function watchConfig(options) {
16
- return await watchConfig$1(merge({}, _options, 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));
17
78
  }
18
79
  //#endregion
19
- export { defineConfig, loadConfig, watchConfig };
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.2",
4
4
  "description": "Metajoy 项目的配置工具",
5
5
  "files": [
6
6
  "dist"
@@ -15,6 +15,7 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "c12": "4.0.0-beta.5",
18
+ "dotenv-expand": "^13.0.0",
18
19
  "lodash-es": "^4.18.1"
19
20
  },
20
21
  "devDependencies": {