-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource-coder.js
More file actions
46 lines (41 loc) · 1.06 KB
/
source-coder.js
File metadata and controls
46 lines (41 loc) · 1.06 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
40
41
42
43
44
45
46
const path = require("path");
const fs = require("fs");
const [fileDir, filename] = process.argv.slice(2);
const filePaths = readDir(fileDir);
let buffer = new Buffer.from("");
filePaths.forEach((filePath) => {
const content = fs.readFileSync(filePath);
buffer = Buffer.concat([buffer, new Buffer.from("\n"), content]);
});
fs.writeFileSync(
`${__dirname}/out/${filename || new Date().getTime()}-${
buffer.toString().length
}.txt`,
buffer
);
/**
* 读取文件目录,返回所有文件的路径
* @param {*} filePath
* @returns filePaths[]
*/
function readDir(filePath) {
const readdirResult = fs.readdirSync(filePath);
const allFilePaths = [];
readdirResult.forEach((name) => {
const file_path = `${filePath}/${name}`;
const stat = fs.lstatSync(file_path);
if (stat.isDirectory()) {
allFilePaths.push(...readDir(file_path));
} else {
if (
file_path.indexOf(".json") !== -1 ||
file_path.indexOf(".png") !== -1 ||
file_path.indexOf(".svg") !== -1 ||
) {
return;
}
allFilePaths.push(file_path);
}
});
return allFilePaths;
}