递归处理文件
const fs = require('fs')
const path = require('path')
function recurse(src, dst, filter, processor) {
if (filter(src, dst, filter)) {
const stats = fs.statSync(src)
if (stats.isDirectory()) {
fs.mkdirSync(dst, { recursive: true })
fs.readdirSync(src).forEach(sub => recursive(path.join(src, sub), path.join(dst, sub)), filter, processor)
} else if (stats.isFile()) {
processor(src, dst)
}
}
}
function copyTxt(src, dst) {
recurse(
src,
dst,
(src, dst) => path.extname(src) === '.txt',
(src, dst) => fs.createReadStream(src).pipe(fs.createWriteStream(dst))
)
}
copyText('~/a', '~/b')