-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,49 @@ | ||
import { readdir, stat } from "fs:promises"; | ||
import { join, extname, normalize, basename } from "path"; | ||
import { spawn } from 'child_process'; | ||
import { readdir, stat } from 'fs:promises'; | ||
import { join, extname, normalize, basename } from 'node:path'; | ||
import { spawn } from 'node:child_process'; | ||
|
||
// 递归一个文件夹 | ||
async function recursiveDir( | ||
folderPath, | ||
callback, | ||
options = { | ||
filter: (file) => { | ||
return false; // 默认不跳过任何文件 | ||
}, | ||
} | ||
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); | ||
} | ||
} | ||
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); | ||
|
||
export function spawnAsync (...args) { | ||
return new Promise((res, reject) => { | ||
const child = spawn(...args); | ||
let result = ''; | ||
child.stdout.on('data', (data) => { | ||
result += data; | ||
}); | ||
|
||
let result = ''; | ||
child.stdout.on('data', (data) => { | ||
result += data; | ||
}); | ||
child.on('close', () => { | ||
res(result); | ||
}); | ||
|
||
child.on('close', () => { | ||
res(result); | ||
}); | ||
|
||
child.on('error', (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
}; | ||
child.on('error', (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} |