@aztt-cli/utils 0.0.1 → 0.0.3
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/build/index.js +60 -3
- package/lib/index.ts +119 -3
- package/package.json +4 -2
package/build/index.js
CHANGED
@@ -1,4 +1,61 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
'use strict';
|
2
|
+
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
3
|
+
import { Spinner } from 'cli-spinner';
|
4
|
+
import { spawn } from 'child_process';
|
5
|
+
function isObject(o) {
|
6
|
+
return Object.prototype.toString.call(o) === '[object Object]';
|
3
7
|
}
|
4
|
-
|
8
|
+
function spinnerStart(msg, spinnerString = '|/-\\') {
|
9
|
+
const spinner = new Spinner(`${msg} %s`);
|
10
|
+
spinner.setSpinnerString(spinnerString);
|
11
|
+
spinner.start();
|
12
|
+
return spinner;
|
13
|
+
}
|
14
|
+
function sleep(timeout = 1000) {
|
15
|
+
return new Promise(resolve => setTimeout(resolve, timeout));
|
16
|
+
}
|
17
|
+
function exec(command, args, options = {}) {
|
18
|
+
const win32 = process.platform === 'win32';
|
19
|
+
const cmd = win32 ? 'cmd' : command;
|
20
|
+
const cmdArgs = win32 ? ['/c'].concat(command, args) : args;
|
21
|
+
return spawn(cmd, cmdArgs, options);
|
22
|
+
}
|
23
|
+
function execAsync(command, args, options = {}) {
|
24
|
+
return new Promise((resolve, reject) => {
|
25
|
+
const p = exec(command, args, options);
|
26
|
+
p.on('error', e => {
|
27
|
+
reject(e);
|
28
|
+
});
|
29
|
+
p.on('exit', c => {
|
30
|
+
resolve(c);
|
31
|
+
});
|
32
|
+
});
|
33
|
+
}
|
34
|
+
function readFile(path, options = {}) {
|
35
|
+
if (existsSync(path)) {
|
36
|
+
const buffer = readFileSync(path);
|
37
|
+
if (buffer) {
|
38
|
+
if (options.toJson) {
|
39
|
+
return buffer.toJSON();
|
40
|
+
}
|
41
|
+
else {
|
42
|
+
return buffer.toString();
|
43
|
+
}
|
44
|
+
}
|
45
|
+
}
|
46
|
+
return null;
|
47
|
+
}
|
48
|
+
function writeFile(path, data, { rewrite = true } = {}) {
|
49
|
+
if (existsSync(path)) {
|
50
|
+
if (rewrite) {
|
51
|
+
writeFileSync(path, data);
|
52
|
+
return true;
|
53
|
+
}
|
54
|
+
return false;
|
55
|
+
}
|
56
|
+
else {
|
57
|
+
writeFileSync(path, data);
|
58
|
+
return true;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
export { isObject, spinnerStart, sleep, exec, execAsync, readFile, writeFile, };
|
package/lib/index.ts
CHANGED
@@ -1,5 +1,121 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
4
|
+
import { Spinner } from 'cli-spinner';
|
5
|
+
import { spawn, ChildProcess } from 'child_process';
|
6
|
+
|
7
|
+
/**
|
8
|
+
* 判断一个值是否为对象
|
9
|
+
* @param o 任意值
|
10
|
+
* @returns 如果是对象则返回 true,否则返回 false
|
11
|
+
*/
|
12
|
+
function isObject(o: any): boolean {
|
13
|
+
return Object.prototype.toString.call(o) === '[object Object]';
|
14
|
+
}
|
15
|
+
|
16
|
+
/**
|
17
|
+
* 启动一个命令行 spinner
|
18
|
+
* @param msg spinner 显示的消息
|
19
|
+
* @param spinnerString spinner 的旋转字符序列
|
20
|
+
* @returns 返回 Spinner 实例
|
21
|
+
*/
|
22
|
+
function spinnerStart(msg: string, spinnerString: string = '|/-\\'): Spinner {
|
23
|
+
const spinner = new Spinner(`${msg} %s`);
|
24
|
+
spinner.setSpinnerString(spinnerString);
|
25
|
+
spinner.start();
|
26
|
+
return spinner;
|
27
|
+
}
|
28
|
+
|
29
|
+
/**
|
30
|
+
* 延迟指定的时间
|
31
|
+
* @param timeout 延迟的时间,默认为 1000 毫秒
|
32
|
+
* @returns 返回一个 Promise,在指定时间后 resolve
|
33
|
+
*/
|
34
|
+
function sleep(timeout: number = 1000): Promise<void> {
|
35
|
+
return new Promise(resolve => setTimeout(resolve, timeout));
|
36
|
+
}
|
37
|
+
|
38
|
+
/**
|
39
|
+
* 执行一个命令
|
40
|
+
* @param command 要执行的命令
|
41
|
+
* @param args 命令的参数
|
42
|
+
* @param options 子进程的选项
|
43
|
+
* @returns 返回子进程实例
|
44
|
+
*/
|
45
|
+
function exec(command: string, args: string[], options: any = {}): ChildProcess {
|
46
|
+
const win32 = process.platform === 'win32';
|
47
|
+
|
48
|
+
const cmd = win32 ? 'cmd' : command;
|
49
|
+
const cmdArgs = win32 ? ['/c'].concat(command, args) : args;
|
50
|
+
|
51
|
+
return spawn(cmd, cmdArgs, options);
|
52
|
+
}
|
53
|
+
|
54
|
+
/**
|
55
|
+
* 异步执行一个命令
|
56
|
+
* @param command 要执行的命令
|
57
|
+
* @param args 命令的参数
|
58
|
+
* @param options 子进程的选项
|
59
|
+
* @returns 返回一个 Promise,resolve 时返回退出码,reject 时返回错误
|
60
|
+
*/
|
61
|
+
function execAsync(command: string, args: string[], options: any = {}): Promise<number> {
|
62
|
+
return new Promise((resolve, reject) => {
|
63
|
+
const p = exec(command, args, options);
|
64
|
+
p.on('error', e => {
|
65
|
+
reject(e);
|
66
|
+
});
|
67
|
+
p.on('exit', c => {
|
68
|
+
resolve(c);
|
69
|
+
});
|
70
|
+
});
|
71
|
+
}
|
72
|
+
|
73
|
+
/**
|
74
|
+
* 读取文件内容
|
75
|
+
* @param path 文件路径
|
76
|
+
* @param options 选项,包含 toJson 属性,决定是否将内容转换为 JSON
|
77
|
+
* @returns 返回文件内容或 JSON 对象,如果文件不存在则返回 null
|
78
|
+
*/
|
79
|
+
function readFile(path: string, options: { toJson?: boolean } = {}): string | object | null {
|
80
|
+
if (existsSync(path)) {
|
81
|
+
const buffer = readFileSync(path);
|
82
|
+
if (buffer) {
|
83
|
+
if (options.toJson) {
|
84
|
+
return buffer.toJSON();
|
85
|
+
} else {
|
86
|
+
return buffer.toString();
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
return null;
|
91
|
+
}
|
92
|
+
|
93
|
+
/**
|
94
|
+
* 写入文件内容
|
95
|
+
* @param path 文件路径
|
96
|
+
* @param data 要写入的数据
|
97
|
+
* @param options 选项,包含 rewrite 属性,决定是否覆盖已存在的文件
|
98
|
+
* @returns 如果写入成功返回 true,否则返回 false
|
99
|
+
*/
|
100
|
+
function writeFile(path: string, data: string, { rewrite = true } = {}): boolean {
|
101
|
+
if (existsSync(path)) {
|
102
|
+
if (rewrite) {
|
103
|
+
writeFileSync(path, data);
|
104
|
+
return true;
|
105
|
+
}
|
106
|
+
return false;
|
107
|
+
} else {
|
108
|
+
writeFileSync(path, data);
|
109
|
+
return true;
|
110
|
+
}
|
3
111
|
}
|
4
112
|
|
5
|
-
export {
|
113
|
+
export {
|
114
|
+
isObject,
|
115
|
+
spinnerStart,
|
116
|
+
sleep,
|
117
|
+
exec,
|
118
|
+
execAsync,
|
119
|
+
readFile,
|
120
|
+
writeFile,
|
121
|
+
};
|
package/package.json
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
{
|
2
2
|
"name": "@aztt-cli/utils",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.3",
|
4
4
|
"type": "module",
|
5
5
|
"description": "> TODO: description",
|
6
6
|
"author": "za990326 <956267437@qq.com>",
|
7
7
|
"homepage": "",
|
8
8
|
"license": "ISC",
|
9
9
|
"main": "build/index.js",
|
10
|
+
"types": "build/index.d.ts",
|
10
11
|
"directories": {
|
11
12
|
"lib": "lib",
|
12
13
|
"test": "__tests__"
|
@@ -23,9 +24,10 @@
|
|
23
24
|
"build": "tsc --watch"
|
24
25
|
},
|
25
26
|
"dependencies": {
|
26
|
-
"
|
27
|
+
"cli-spinner": "^0.2.10"
|
27
28
|
},
|
28
29
|
"devDependencies": {
|
30
|
+
"@types/cli-spinner": "^0.2.3",
|
29
31
|
"typescript": "^5.7.3"
|
30
32
|
}
|
31
33
|
}
|