-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodejs.js
77 lines (76 loc) · 2.1 KB
/
nodejs.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
let fs = require("fs");
let path = require("path");
// 第一个参数是路径
var filePath = path.resolve("./");
// 统计结果
let num = 0;
line(filePath);
// 获取行数
function line(filePath) {
fs.readdir(filePath, function (err, files) {
if (err) {
console.warn(err);
} else {
//遍历读取到的文件列表
files.forEach(function (filename) {
//获取当前文件的绝对路径
var filedir = path.join(filePath, filename);
//根据文件路径获取文件信息,返回一个fs.Stats对象
fs.stat(filedir, function (eror, stats) {
if (eror) {
console.warn("获取文件stats失败");
} else {
var isFile = stats.isFile(); //是文件
var isDir = stats.isDirectory(); //是文件夹
if (isFile && filedir.slice(-3) === ".md") {
// 是文件并且是md文件
fs.readFile(
filedir,
{
encoding: "utf-8",
},
function (err, data) {
if (err) {
console.error(err);
} else {
let lines = data.split(" ").join("");
console.log(filedir + " " + lines.length);
num += lines.length;
}
}
);
}
if (isDir) {
line(filedir); //递归,如果是文件夹,就继续遍历该文件夹下面的文件
}
}
});
});
}
});
console.log(num);
}
function processData(data, filedir) {
// 关键字
var key = data.match(/@\{\w*\}@/gm);
let context = data;
if (key) {
key.forEach((item) => {
if (file[item.slice(2, item.length - 2)]) {
context = context.replace(
`${item}`,
file[item.slice(2, item.length - 2)]
);
}
});
const filePath = filedir.split("\\").slice(-2);
const fileRelative =
filePath[0] === "classroom_docs"
? `./modified/modified-${filePath[1]}`
: `./modified/${filePath[0]}/modified-${filePath[1]}`;
writedir = process.env.NODE_ENV == "production" ? filedir : fileRelative;
fs.writeFileSync(writedir, context, (err, res) => {
console.error("writeFileSync", err);
});
}
}