ph-utils 0.13.1 → 0.14.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/lib/config.d.ts +33 -0
- package/lib/config.js +99 -0
- package/lib/dom.d.ts +1 -1
- package/lib/dom.js +1 -1
- package/lib/server.d.ts +0 -20
- package/lib/server.js +1 -58
- package/package.json +1 -1
package/lib/config.d.ts
ADDED
@@ -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 = ["config.json", "config.local.json"], runParseEnv = true) {
|
78
|
+
let d = [...files];
|
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/dom.d.ts
CHANGED
@@ -21,7 +21,7 @@ export declare function $(selector: string | HTMLElement, dom?: DocumentContext)
|
|
21
21
|
/**
|
22
22
|
* 创建一个 HTML 元素,支持通过标签名或 HTML 字符串创建。
|
23
23
|
* @param tag - 元素标签名或 HTML 字符串。
|
24
|
-
* @param option -
|
24
|
+
* @param option - 元素的属性、样式、文本内容等配置。所有不是指定的属性,都会通过 setAttribute 设置
|
25
25
|
* @param ctx - 元素的父级文档上下文。
|
26
26
|
* @returns 创建的 HTML 元素。
|
27
27
|
*/
|
package/lib/dom.js
CHANGED
@@ -18,7 +18,7 @@ export function $(selector, dom) {
|
|
18
18
|
/**
|
19
19
|
* 创建一个 HTML 元素,支持通过标签名或 HTML 字符串创建。
|
20
20
|
* @param tag - 元素标签名或 HTML 字符串。
|
21
|
-
* @param option -
|
21
|
+
* @param option - 元素的属性、样式、文本内容等配置。所有不是指定的属性,都会通过 setAttribute 设置
|
22
22
|
* @param ctx - 元素的父级文档上下文。
|
23
23
|
* @returns 创建的 HTML 元素。
|
24
24
|
*/
|
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(): Record<string, string>;
|
package/lib/server.js
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
import { execFile } from "node:child_process";
|
2
|
-
import {
|
3
|
-
import { readFileSync } from "node:fs";
|
2
|
+
import { promisify } from "node:util";
|
4
3
|
const execFilePromise = promisify(execFile);
|
5
4
|
/**
|
6
5
|
* 执行命令
|
@@ -28,59 +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() {
|
51
|
-
// development, test, production
|
52
|
-
const files = [".env", ".env.local"];
|
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
|
-
files.push(`.env.${nodeEnv}`);
|
71
|
-
let envParsed = {};
|
72
|
-
for (let i = 0, len = files.length; i < len; i++) {
|
73
|
-
const file = files[i];
|
74
|
-
try {
|
75
|
-
const envContent = readFileSync(file, { encoding: "utf-8" });
|
76
|
-
const envValue = parseEnv(envContent);
|
77
|
-
envParsed = { ...envParsed, ...envValue };
|
78
|
-
// eslint-disable-next-line
|
79
|
-
}
|
80
|
-
catch (err) { }
|
81
|
-
}
|
82
|
-
for (const key in envParsed) {
|
83
|
-
process.env[key] = envParsed[key];
|
84
|
-
}
|
85
|
-
return envParsed;
|
86
|
-
}
|