ph-utils 0.5.1 → 0.6.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/server.d.ts CHANGED
@@ -1,32 +1,10 @@
1
+ /// <reference types="node" />
2
+ import type { SpawnOptions } from "node:child_process";
1
3
  /**
2
4
  * 执行命令
3
- * @param cmd 执行的命令
4
- * @returns
5
+ * @param command 待执行的命令
6
+ * @param args 命令参数
5
7
  */
6
- export declare function exec(cmd: string): Promise<string>;
7
- interface SpawnCmdOptions {
8
- /** 命令运行目录 */
9
- cwd?: string;
10
- /** 每一行的输出 */
11
- data?: (lineText?: string) => void;
12
- /** 错误输出 */
13
- error?: (err: Error) => void;
14
- /** 最终结果 */
15
- finally?: (err?: Error) => void;
16
- }
17
- /**
18
- * 执行 spawn 命令
19
- * @param command 执行的命令: 例如: git
20
- * @param args 命令参数: ['clone', 'https://xxxx.git']
21
- * @param options 参数
22
- */
23
- export declare function spawnCmd(command: string, args?: string[], options?: SpawnCmdOptions): void;
24
- /**
25
- * 执行 spawn 命令
26
- * @param command 执行的命令: 例如: git
27
- * @param args 命令参数: ['clone', 'https://xxxx.git']
28
- * @param options 参数
29
- * @returns
30
- */
31
- export declare function spawnPromise(command: string, args?: string[], options?: SpawnCmdOptions): Promise<unknown>;
32
- export {};
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>;
package/lib/server.js CHANGED
@@ -1,77 +1,52 @@
1
- import { exec as execCmd, spawn } from 'child_process';
2
- import { isBlank } from './index.js';
1
+ import { spawn } from "node:child_process";
3
2
  /**
4
3
  * 执行命令
5
4
  * @param cmd 执行的命令
6
5
  * @returns
7
6
  */
8
- export function exec(cmd) {
7
+ export function exec(command, ...params) {
9
8
  return new Promise((resolve, reject) => {
10
- execCmd(cmd, (err, stdout, stderr) => {
11
- if (err) {
12
- reject(err);
9
+ let argvs = [];
10
+ const commandItems = command.split(" ");
11
+ const cmd = commandItems.shift();
12
+ if (commandItems.length > 0) {
13
+ argvs = commandItems;
14
+ }
15
+ let opts = undefined;
16
+ if (params[0] != null) {
17
+ if (params[0] instanceof Array) {
18
+ argvs.push(...params[0]);
19
+ if (params[1] != null) {
20
+ opts = params[1];
21
+ }
13
22
  }
14
23
  else {
15
- if (!isBlank(stderr) && stderr.indexOf('error') !== -1) {
16
- reject(new Error(stderr));
17
- }
18
- else {
19
- resolve((isBlank(stdout) ? stderr : stdout).trim());
20
- }
24
+ opts = params[0];
21
25
  }
22
- });
23
- });
24
- }
25
- /**
26
- * 执行 spawn 命令
27
- * @param command 执行的命令: 例如: git
28
- * @param args 命令参数: ['clone', 'https://xxxx.git']
29
- * @param options 参数
30
- */
31
- export function spawnCmd(command, args, options) {
32
- const spawnClient = spawn(command, args, { cwd: options?.cwd });
33
- let err;
34
- spawnClient.stdout.on('data', (chunk) => {
35
- if (options?.data != null && typeof options.data === 'function') {
36
- options.data(chunk);
37
- }
38
- });
39
- spawnClient.stderr.on('error', (error) => {
40
- err = error;
41
- if (options?.error && typeof options.error === 'function') {
42
- options.error(error);
43
- }
44
- });
45
- spawnClient.on('error', (error) => {
46
- err = error;
47
- if (options?.error && typeof options.error === 'function') {
48
- options.error(error);
49
26
  }
50
- });
51
- spawnClient.on('close', () => {
52
- if (options?.finally && typeof options.finally === 'function') {
53
- options.finally(err);
54
- }
55
- });
56
- }
57
- /**
58
- * 执行 spawn 命令
59
- * @param command 执行的命令: 例如: git
60
- * @param args 命令参数: ['clone', 'https://xxxx.git']
61
- * @param options 参数
62
- * @returns
63
- */
64
- export function spawnPromise(command, args, options) {
65
- return new Promise((resolve, reject) => {
66
- options = options || {};
67
- options.finally = (err) => {
68
- if (err == null) {
69
- resolve(1);
27
+ const prs = spawn(cmd, argvs, opts);
28
+ let msg = [];
29
+ let error;
30
+ prs.stderr.on("data", (chunk) => {
31
+ msg.push(chunk.toString("utf-8"));
32
+ });
33
+ prs.stdout.on("data", (chunk) => {
34
+ msg.push(chunk.toString("utf-8"));
35
+ });
36
+ prs.on("error", (err) => {
37
+ error = err;
38
+ });
39
+ prs.on("close", (code, signal) => {
40
+ if (code === 0) {
41
+ resolve(msg.join("\n"));
70
42
  }
71
43
  else {
72
- reject(err);
44
+ if (error == null) {
45
+ error = new Error(msg[msg.length - 1]);
46
+ }
47
+ error.errno = code;
48
+ reject(error);
73
49
  }
74
- };
75
- spawnCmd(command, args, options);
50
+ });
76
51
  });
77
52
  }
package/package.json CHANGED
@@ -52,7 +52,7 @@
52
52
  },
53
53
  "./*": "./lib/*"
54
54
  },
55
- "version": "0.5.1",
55
+ "version": "0.6.0",
56
56
  "type": "module",
57
57
  "repository": {
58
58
  "type": "git",