ph-utils 0.5.0 → 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/file.d.ts +3 -3
- package/lib/file.js +5 -5
- package/lib/server.d.ts +7 -29
- package/lib/server.js +37 -62
- package/package.json +1 -1
package/lib/file.d.ts
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
/**
|
2
2
|
* 读取文件内容为JSON格式
|
3
3
|
* @param filepath 读取的文件路径
|
4
|
-
* @param
|
4
|
+
* @param defaultValue 读取失败时,提供默认值, 如果不提供传参数则[抛出异常]
|
5
5
|
*
|
6
|
-
* @example <caption>1. 文件不存在时,
|
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,
|
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
|
7
|
+
* @param defaultValue 读取失败时,提供默认值, 如果不提供传参数则[抛出异常]
|
8
8
|
*
|
9
|
-
* @example <caption>1. 文件不存在时,
|
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,
|
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 (
|
19
|
-
resolve(
|
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
|
4
|
-
* @
|
5
|
+
* @param command 待执行的命令
|
6
|
+
* @param args 命令参数
|
5
7
|
*/
|
6
|
-
export declare function exec(
|
7
|
-
|
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 {
|
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(
|
7
|
+
export function exec(command, ...params) {
|
9
8
|
return new Promise((resolve, reject) => {
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
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
|
}
|