-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.mjs
49 lines (42 loc) · 1.14 KB
/
utils.mjs
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
import { readdir, stat } from 'node:fs/promises';
import { join, extname, normalize, basename } from 'node:path';
import { spawn } from 'node:child_process';
// 递归一个文件夹
export async function recursiveDir(
folderPath,
callback,
options = {
filter: (file) => {
return false; // 默认不跳过任何文件
},
}
) {
const files = await readdir(folderPath);
for (const file of files) {
const filePath = join(folderPath, file);
const stats = await stat(filePath);
if (options.filter?.(file)) {
continue;
}
if (stats.isDirectory()) {
await recursiveDir(filePath, callback, options);
} else {
await callback(filePath);
}
}
}
export function spawnAsync(...args) {
return new Promise((res, reject) => {
const child = spawn(...args);
let result = '';
child.stdout.on('data', (data) => {
result += data;
});
child.on('close', () => {
res(result);
});
child.on('error', (err) => {
reject(err);
});
});
}