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.
Files changed (2) hide show
  1. package/lib/server.js +66 -65
  2. 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
- let argvs = [];
11
- const commandItems = command.split(" ");
12
- const cmd = commandItems.shift();
13
- if (commandItems.length > 0) {
14
- argvs = commandItems;
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
- 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
- }
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
- else {
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
- // development, test, production
66
- const files = [".env", ".env.local"];
67
- let nodeEnv = process.env.NODE_ENV;
68
- const { values } = parseArgs({
69
- options: {
70
- NODE_ENV: {
71
- type: "string",
72
- short: "n",
73
- },
74
- },
75
- strict: false,
76
- });
77
- if (values.NODE_ENV != null && typeof values.NODE_ENV === "string") {
78
- nodeEnv = values.NODE_ENV;
79
- }
80
- if (nodeEnv == null) {
81
- nodeEnv = "production";
82
- }
83
- process.env.NODE_ENV = nodeEnv;
84
- files.push(`.env.${nodeEnv}`);
85
- let envParsed = {};
86
- for (let i = 0, len = files.length; i < len; i++) {
87
- const file = files[i];
88
- try {
89
- const envContent = readFileSync(file, { encoding: "utf-8" });
90
- const envValue = parseEnv(envContent);
91
- envParsed = { ...envParsed, ...envValue };
92
- }
93
- catch (err) { }
94
- }
95
- for (const key in envParsed) {
96
- process.env[key] = envParsed[key];
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
  }
package/package.json CHANGED
@@ -68,7 +68,7 @@
68
68
  },
69
69
  "./*": "./lib/*"
70
70
  },
71
- "version": "0.9.12",
71
+ "version": "0.9.14",
72
72
  "type": "module",
73
73
  "repository": {
74
74
  "type": "git",