-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
40 lines (35 loc) · 1.3 KB
/
app.js
File metadata and controls
40 lines (35 loc) · 1.3 KB
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
const fs = require('fs');
const {entry, output, excludeFiles, excludePaths} = require('./config');
const blankLineReg = /\n\s*\r/g;
let fileCount = 0;
let readData = '';
async function read(path) {
const dir = await fs.promises.opendir(path);
for await (const dirent of dir) {
if (dirent.isFile()) {
if (!excludeFiles.includes(dirent.name)) {
fileCount++;
const filePath = path + '/' + dirent.name;
const readerStream = fs.createReadStream(filePath, 'utf8');
readerStream.on('data', (chunk) => {
const relativeFilePath = filePath.replace(entry, '');
readData += `\n--------------------- ${relativeFilePath} ---------------------\n`;
readData += chunk.replace(blankLineReg, '');
});
}
} else {
if (!excludePaths.includes(dirent.name)) {
const childPath = path + '/' + dirent.name;
await read(childPath);
}
}
}
}
async function extract() {
await read(entry);
const writerStream = fs.createWriteStream(output, 'utf8');
writerStream.write(readData, 'utf8', () => {
console.log(`执行完毕,提取文件总数:${fileCount}`);
});
}
extract();