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 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<string>;
9
- export declare function exec(command: string, options?: SpawnOptions): Promise<string>;
10
- export declare function exec(command: string, args?: string[], options?: SpawnOptions): Promise<string>;
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
- 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];
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
- 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);
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
- // 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;
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.14",
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.7.9",
85
+ "@types/node": "^22.8.2",
86
86
  "typescript": "^5.6.3"
87
87
  },
88
88
  "files": [