ph-utils 0.9.14 → 0.9.15
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/server.d.ts +12 -3
- package/lib/server.js +54 -68
- package/package.json +2 -2
package/lib/server.d.ts
CHANGED
@@ -5,9 +5,18 @@ import type { SpawnOptions } from "node:child_process";
|
|
5
5
|
* @param command 待执行的命令
|
6
6
|
* @param args 命令参数
|
7
7
|
*/
|
8
|
-
export declare function exec(command: string, args?: string[]): Promise<
|
9
|
-
|
10
|
-
|
8
|
+
export declare function exec(command: string, args?: string[]): Promise<{
|
9
|
+
stdout: string;
|
10
|
+
stderr: string;
|
11
|
+
}>;
|
12
|
+
export declare function exec(command: string, options?: SpawnOptions): Promise<{
|
13
|
+
stdout: string;
|
14
|
+
stderr: string;
|
15
|
+
}>;
|
16
|
+
export declare function exec(command: string, args?: string[], options?: SpawnOptions): Promise<{
|
17
|
+
stdout: string;
|
18
|
+
stderr: string;
|
19
|
+
}>;
|
11
20
|
/**
|
12
21
|
* 解析环境变量;
|
13
22
|
* 同时读取多个环境变量文件: .env, .env.local, .env.[development|test|production];
|
package/lib/server.js
CHANGED
@@ -1,48 +1,32 @@
|
|
1
1
|
import { execFile } from "node:child_process";
|
2
|
-
import { parseEnv, parseArgs } from "node:util";
|
2
|
+
import { parseEnv, parseArgs, promisify } from "node:util";
|
3
3
|
import { readFileSync } from "node:fs";
|
4
|
+
const execFilePromise = promisify(execFile);
|
4
5
|
/**
|
5
6
|
* 执行命令
|
6
7
|
* @param cmd 执行的命令
|
7
8
|
* @returns
|
8
9
|
*/
|
9
10
|
export function exec(command, ...params) {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
}
|
16
|
-
let opts = {};
|
17
|
-
if (params[0] != null) {
|
18
|
-
if (params[0] instanceof Array) {
|
19
|
-
argvs.push(...params[0]);
|
20
|
-
if (params[1] != null) {
|
21
|
-
opts = params[1];
|
22
|
-
}
|
23
|
-
} else {
|
24
|
-
opts = params[0];
|
11
|
+
let argvs = [];
|
12
|
+
const commandItems = command.split(" ");
|
13
|
+
const cmd = commandItems.shift();
|
14
|
+
if (commandItems.length > 0) {
|
15
|
+
argvs = commandItems;
|
25
16
|
}
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
if (
|
34
|
-
stderr != null &&
|
35
|
-
(stderr.includes("error") || stderr.includes("Error"))
|
36
|
-
) {
|
37
|
-
const error = new Error(stderr);
|
38
|
-
error.type = "StdErr";
|
39
|
-
reject(error);
|
40
|
-
} else {
|
41
|
-
resolve(msg);
|
17
|
+
let opts = { shell: true };
|
18
|
+
if (params[0] != null) {
|
19
|
+
if (params[0] instanceof Array) {
|
20
|
+
argvs.push(...params[0]);
|
21
|
+
if (params[1] != null) {
|
22
|
+
opts = params[1];
|
23
|
+
}
|
42
24
|
}
|
43
|
-
|
44
|
-
|
45
|
-
|
25
|
+
else {
|
26
|
+
opts = params[0];
|
27
|
+
}
|
28
|
+
}
|
29
|
+
return execFilePromise(cmd, argvs, opts);
|
46
30
|
}
|
47
31
|
/**
|
48
32
|
* 解析环境变量;
|
@@ -64,37 +48,39 @@ export function exec(command, ...params) {
|
|
64
48
|
* @returns
|
65
49
|
*/
|
66
50
|
export function parseEnvs() {
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
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;
|
100
86
|
}
|
package/package.json
CHANGED
@@ -68,7 +68,7 @@
|
|
68
68
|
},
|
69
69
|
"./*": "./lib/*"
|
70
70
|
},
|
71
|
-
"version": "0.9.
|
71
|
+
"version": "0.9.15",
|
72
72
|
"type": "module",
|
73
73
|
"repository": {
|
74
74
|
"type": "git",
|
@@ -82,7 +82,7 @@
|
|
82
82
|
},
|
83
83
|
"homepage": "https://gitee.com/towardly/ph/tree/master/packages/utils",
|
84
84
|
"devDependencies": {
|
85
|
-
"@types/node": "^22.
|
85
|
+
"@types/node": "^22.8.2",
|
86
86
|
"typescript": "^5.6.3"
|
87
87
|
},
|
88
88
|
"files": [
|