-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
65 lines (59 loc) · 1.71 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import fs from "fs-extra";
import path from "path";
import rimraf from "rimraf";
/** 包装promise函数返回值
* @param {Promise} promise
*/
export const to = (promise: Promise<any>): Promise<any> => {
return promise
.then((res: any) => [null, res])
.catch((err: any) => [err, null]);
};
/** 判断一个路径是目录还是文件
* 1.假如存在用fs.stat 判断路径类型,不存在用path.extname判断
* @param {String} pathStr
* @returns {boolean}
*/
export const pathIsFile = async (pathStr: string): Promise<Boolean> => {
if (fs.existsSync(pathStr)) {
const stats = await fs.promises.stat(pathStr);
return stats.isFile();
} else {
return path.extname(pathStr) !== ""; // 这里对于不存在的路径直接判断是否有后缀
}
};
/** 拷贝文件,对于目录会进行递归拷贝
* @param {String}source
* @param {String}target
*/
export const copyFiles = async (
source: string,
target: string
): Promise<void> => {
try {
// const isFile = await pathIsFile(target);
// const isExists = fs.existsSync(target);
// if (isExists && isFile) await rimrafPromify(target);
await rimrafPromify(target);
await fs.copy(source, target); // 拷贝文件夹会直接把文件内容递归拷贝
} catch (err) {
console.error(err);
}
};
/** rimraf 函数的promise 改造
* @param {String} pathStr
*/
export const rimrafPromify = (pathStr: string): Promise<any> => {
return new Promise((resolve, reject) => {
rimraf(pathStr, (error) => {
if (error) return reject(error);
resolve();
});
});
};
export const log = console.log.bind(console);
// 进程退出
export const exit = (code: number, msg: string) => {
log(msg);
process.exit(code);
};