ph-utils 0.9.12 → 0.9.14
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/server.js +66 -65
- package/package.json +1 -1
package/lib/server.js
CHANGED
@@ -7,40 +7,42 @@ import { readFileSync } from "node:fs";
|
|
7
7
|
* @returns
|
8
8
|
*/
|
9
9
|
export function exec(command, ...params) {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
let argvs = [];
|
11
|
+
const commandItems = command.split(" ");
|
12
|
+
const cmd = commandItems.shift();
|
13
|
+
if (commandItems.length > 0) {
|
14
|
+
argvs = commandItems;
|
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];
|
15
25
|
}
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
26
|
+
}
|
27
|
+
return new Promise((resolve, reject) => {
|
28
|
+
execFile(cmd, argvs, opts || {}, (err, stdout, stderr) => {
|
29
|
+
if (err) {
|
30
|
+
reject(err);
|
31
|
+
} else {
|
32
|
+
let msg = stdout || stderr;
|
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);
|
23
42
|
}
|
24
|
-
|
25
|
-
opts = params[0];
|
26
|
-
}
|
27
|
-
}
|
28
|
-
return new Promise((resolve, reject) => {
|
29
|
-
execFile(cmd, argvs, (opts || {}), (err, stdout, stderr) => {
|
30
|
-
if (err) {
|
31
|
-
reject(err);
|
32
|
-
}
|
33
|
-
else {
|
34
|
-
let msg = stdout || stderr;
|
35
|
-
if (msg.includes("error") || msg.includes("Error")) {
|
36
|
-
const error = new Error(msg);
|
37
|
-
error.type = "StdErr";
|
38
|
-
reject(error);
|
39
|
-
}
|
40
|
-
resolve(msg);
|
41
|
-
}
|
42
|
-
});
|
43
|
+
}
|
43
44
|
});
|
45
|
+
});
|
44
46
|
}
|
45
47
|
/**
|
46
48
|
* 解析环境变量;
|
@@ -62,38 +64,37 @@ export function exec(command, ...params) {
|
|
62
64
|
* @returns
|
63
65
|
*/
|
64
66
|
export function parseEnvs() {
|
65
|
-
|
66
|
-
|
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
|
-
return envParsed;
|
67
|
+
// development, test, production
|
68
|
+
const files = [".env", ".env.local"];
|
69
|
+
let nodeEnv = process.env.NODE_ENV;
|
70
|
+
const { values } = parseArgs({
|
71
|
+
options: {
|
72
|
+
NODE_ENV: {
|
73
|
+
type: "string",
|
74
|
+
short: "n",
|
75
|
+
},
|
76
|
+
},
|
77
|
+
strict: false,
|
78
|
+
});
|
79
|
+
if (values.NODE_ENV != null && typeof values.NODE_ENV === "string") {
|
80
|
+
nodeEnv = values.NODE_ENV;
|
81
|
+
}
|
82
|
+
if (nodeEnv == null) {
|
83
|
+
nodeEnv = "production";
|
84
|
+
}
|
85
|
+
process.env.NODE_ENV = nodeEnv;
|
86
|
+
files.push(`.env.${nodeEnv}`);
|
87
|
+
let envParsed = {};
|
88
|
+
for (let i = 0, len = files.length; i < len; i++) {
|
89
|
+
const file = files[i];
|
90
|
+
try {
|
91
|
+
const envContent = readFileSync(file, { encoding: "utf-8" });
|
92
|
+
const envValue = parseEnv(envContent);
|
93
|
+
envParsed = { ...envParsed, ...envValue };
|
94
|
+
} catch (err) {}
|
95
|
+
}
|
96
|
+
for (const key in envParsed) {
|
97
|
+
process.env[key] = envParsed[key];
|
98
|
+
}
|
99
|
+
return envParsed;
|
99
100
|
}
|