ph-utils 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
package/lib/file.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  /**
2
2
  * 读取文件内容为JSON格式
3
3
  * @param filepath 读取的文件路径
4
- * @param errIsNull 读取失败时,是否返回 null,默认为 false[抛出异常]
4
+ * @param defaultValue 读取失败时,提供默认值, 如果不提供传参数则[抛出异常]
5
5
  *
6
- * @example <caption>1. 文件不存在时, 返回null,而不是 catch</caption>
6
+ * @example <caption>1. 文件不存在时, 默认为: null</caption>
7
7
  * await readJSON("./not-exists.json", true);
8
8
  *
9
9
  * @returns Promise<unknown>
10
10
  */
11
- export declare function readJSON<T>(filepath: string, errIsNull?: boolean): Promise<T>;
11
+ export declare function readJSON<T>(filepath: string, defaultValue?: T): Promise<T>;
12
12
  /**
13
13
  * 写入 JSON 格式的数据到文件
14
14
  * @param file 待写入的文件
package/lib/file.js CHANGED
@@ -4,19 +4,19 @@ import fs from "node:fs";
4
4
  /**
5
5
  * 读取文件内容为JSON格式
6
6
  * @param filepath 读取的文件路径
7
- * @param errIsNull 读取失败时,是否返回 null,默认为 false[抛出异常]
7
+ * @param defaultValue 读取失败时,提供默认值, 如果不提供传参数则[抛出异常]
8
8
  *
9
- * @example <caption>1. 文件不存在时, 返回null,而不是 catch</caption>
9
+ * @example <caption>1. 文件不存在时, 默认为: null</caption>
10
10
  * await readJSON("./not-exists.json", true);
11
11
  *
12
12
  * @returns Promise<unknown>
13
13
  */
14
- export function readJSON(filepath, errIsNull = false) {
14
+ export function readJSON(filepath, defaultValue) {
15
15
  return new Promise((resolve, reject) => {
16
16
  fs.readFile(path.resolve(filepath), "utf-8", (err, data) => {
17
17
  if (err) {
18
- if (errIsNull) {
19
- resolve(null);
18
+ if (defaultValue !== undefined) {
19
+ resolve(defaultValue);
20
20
  }
21
21
  else {
22
22
  reject(err);
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.0",
55
+ "version": "0.6.0",
56
56
  "type": "module",
57
57
  "repository": {
58
58
  "type": "git",