ph-utils 0.13.2 → 0.14.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.
@@ -0,0 +1,33 @@
1
+ /** 配置信息 */
2
+ export declare let config: Record<string, any>;
3
+ /**
4
+ * 解析环境变量;
5
+ * 同时读取多个环境变量文件: .env, .env.local, .env.[development|test|production];
6
+ * 根据运行环境变量 `NODE_ENV` 读取不同的环境变量文件;
7
+ * 同时支持手动通过运行命令指定 `NODE_ENV` 值, 不指定默认为: production
8
+ *
9
+ * ```bash
10
+ * node test.js --NODE_ENV development
11
+ * // or
12
+ * node test.js -n development
13
+ * ```
14
+ *
15
+ * ```js
16
+ * // test.js
17
+ * parseEnvs();
18
+ * ```
19
+ *
20
+ * @returns
21
+ */
22
+ export declare function parseEnv(envFiles?: string[]): Record<string, string>;
23
+ /**
24
+ * 解析配置文件并合并内容。
25
+ *
26
+ * @param files - 要解析的配置文件列表,默认为 ["config.json", "config.local.json"]。
27
+ * @param runParseEnv - 是否运行环境变量解析,默认为 true。
28
+ * @returns 合并后的配置对象。
29
+ *
30
+ * 该函数会根据当前环境加载相应的配置文件,并将其内容合并到最终的配置对象中。
31
+ * 如果指定的文件列表中不包含环境特定的配置文件,则会自动添加。
32
+ */
33
+ export declare function parseConfig(files?: string[], runParseEnv?: boolean): Record<string, any>;
package/lib/config.js ADDED
@@ -0,0 +1,99 @@
1
+ import { parseEnv as readEnv, parseArgs } from "node:util";
2
+ import { readFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ /** 配置信息 */
5
+ export let config = {};
6
+ /**
7
+ * 解析环境变量;
8
+ * 同时读取多个环境变量文件: .env, .env.local, .env.[development|test|production];
9
+ * 根据运行环境变量 `NODE_ENV` 读取不同的环境变量文件;
10
+ * 同时支持手动通过运行命令指定 `NODE_ENV` 值, 不指定默认为: production
11
+ *
12
+ * ```bash
13
+ * node test.js --NODE_ENV development
14
+ * // or
15
+ * node test.js -n development
16
+ * ```
17
+ *
18
+ * ```js
19
+ * // test.js
20
+ * parseEnvs();
21
+ * ```
22
+ *
23
+ * @returns
24
+ */
25
+ export function parseEnv(envFiles = [".env", ".env.local"]) {
26
+ // development, test, production
27
+ const files = [...envFiles];
28
+ let nodeEnv = process.env.NODE_ENV;
29
+ const { values } = parseArgs({
30
+ options: {
31
+ NODE_ENV: {
32
+ type: "string",
33
+ short: "n",
34
+ },
35
+ },
36
+ strict: false,
37
+ });
38
+ if (values.NODE_ENV != null && typeof values.NODE_ENV === "string") {
39
+ nodeEnv = values.NODE_ENV;
40
+ }
41
+ if (nodeEnv == null) {
42
+ nodeEnv = "production";
43
+ }
44
+ process.env.NODE_ENV = nodeEnv;
45
+ const envFile = `.env.${nodeEnv}`;
46
+ if (!files.includes(envFile)) {
47
+ files.push(envFile);
48
+ }
49
+ let envParsed = {};
50
+ for (let i = 0, len = files.length; i < len; i++) {
51
+ const file = join(process.cwd(), files[i]);
52
+ try {
53
+ const envContent = readFileSync(file, {
54
+ encoding: "utf-8",
55
+ });
56
+ const envValue = readEnv(envContent);
57
+ for (const key in envValue) {
58
+ process.env[key] = envValue[key];
59
+ envParsed[key] = envValue[key];
60
+ }
61
+ // eslint-disable-next-line
62
+ }
63
+ catch (err) { }
64
+ }
65
+ return envParsed;
66
+ }
67
+ /**
68
+ * 解析配置文件并合并内容。
69
+ *
70
+ * @param files - 要解析的配置文件列表,默认为 ["config.json", "config.local.json"]。
71
+ * @param runParseEnv - 是否运行环境变量解析,默认为 true。
72
+ * @returns 合并后的配置对象。
73
+ *
74
+ * 该函数会根据当前环境加载相应的配置文件,并将其内容合并到最终的配置对象中。
75
+ * 如果指定的文件列表中不包含环境特定的配置文件,则会自动添加。
76
+ */
77
+ export function parseConfig(files, runParseEnv = true) {
78
+ let d = [...(files || ["config.json", "config.local.json"])];
79
+ if (runParseEnv) {
80
+ parseEnv();
81
+ }
82
+ const envConfigPath = `config.${process.env.NODE_ENV}.json`;
83
+ if (!d.includes(envConfigPath)) {
84
+ d.push(envConfigPath);
85
+ }
86
+ for (let i = 0, len = d.length; i < len; i++) {
87
+ const filePath = join(process.cwd(), d[i]);
88
+ try {
89
+ const content = readFileSync(filePath, {
90
+ encoding: "utf-8",
91
+ });
92
+ let contentJson = JSON.parse(content);
93
+ config = { ...config, ...contentJson };
94
+ // eslint-disable-next-line
95
+ }
96
+ catch (err) { }
97
+ }
98
+ return config;
99
+ }
package/lib/server.d.ts CHANGED
@@ -16,23 +16,3 @@ export declare function exec(command: string, args?: string[], options?: SpawnOp
16
16
  stdout: string;
17
17
  stderr: string;
18
18
  }>;
19
- /**
20
- * 解析环境变量;
21
- * 同时读取多个环境变量文件: .env, .env.local, .env.[development|test|production];
22
- * 根据运行环境变量 `NODE_ENV` 读取不同的环境变量文件;
23
- * 同时支持手动通过运行命令指定 `NODE_ENV` 值, 不指定默认为: production
24
- *
25
- * ```bash
26
- * node test.js --NODE_ENV development
27
- * // or
28
- * node test.js -n development
29
- * ```
30
- *
31
- * ```js
32
- * // test.js
33
- * parseEnvs();
34
- * ```
35
- *
36
- * @returns
37
- */
38
- export declare function parseEnvs(envFiles?: string[]): Record<string, string>;
package/lib/server.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { execFile } from "node:child_process";
2
- import { parseEnv, parseArgs, promisify } from "node:util";
3
- import { readFileSync } from "node:fs";
2
+ import { promisify } from "node:util";
4
3
  const execFilePromise = promisify(execFile);
5
4
  /**
6
5
  * 执行命令
@@ -28,62 +27,3 @@ export function exec(command, ...params) {
28
27
  }
29
28
  return execFilePromise(cmd, argvs, opts);
30
29
  }
31
- /**
32
- * 解析环境变量;
33
- * 同时读取多个环境变量文件: .env, .env.local, .env.[development|test|production];
34
- * 根据运行环境变量 `NODE_ENV` 读取不同的环境变量文件;
35
- * 同时支持手动通过运行命令指定 `NODE_ENV` 值, 不指定默认为: production
36
- *
37
- * ```bash
38
- * node test.js --NODE_ENV development
39
- * // or
40
- * node test.js -n development
41
- * ```
42
- *
43
- * ```js
44
- * // test.js
45
- * parseEnvs();
46
- * ```
47
- *
48
- * @returns
49
- */
50
- export function parseEnvs(envFiles = [".env", ".env.local"]) {
51
- // development, test, production
52
- const files = [...envFiles];
53
- let nodeEnv = process.env.NODE_ENV;
54
- const { values } = parseArgs({
55
- options: {
56
- NODE_ENV: {
57
- type: "string",
58
- short: "n",
59
- },
60
- },
61
- strict: false,
62
- });
63
- if (values.NODE_ENV != null && typeof values.NODE_ENV === "string") {
64
- nodeEnv = values.NODE_ENV;
65
- }
66
- if (nodeEnv == null) {
67
- nodeEnv = "production";
68
- }
69
- process.env.NODE_ENV = nodeEnv;
70
- const envFile = `.env.${nodeEnv}`;
71
- if (!files.includes(envFile)) {
72
- files.push(envFile);
73
- }
74
- let envParsed = {};
75
- for (let i = 0, len = files.length; i < len; i++) {
76
- const file = files[i];
77
- try {
78
- const envContent = readFileSync(file, { encoding: "utf-8" });
79
- const envValue = parseEnv(envContent);
80
- for (const key in envValue) {
81
- process.env[key] = envValue[key];
82
- envParsed[key] = envValue[key];
83
- }
84
- // eslint-disable-next-line
85
- }
86
- catch (err) { }
87
- }
88
- return envParsed;
89
- }
package/package.json CHANGED
@@ -68,7 +68,7 @@
68
68
  },
69
69
  "./*": "./lib/*"
70
70
  },
71
- "version": "0.13.2",
71
+ "version": "0.14.1",
72
72
  "type": "module",
73
73
  "repository": {
74
74
  "type": "git",