-
Notifications
You must be signed in to change notification settings - Fork 38
/
scan_general.js
120 lines (105 loc) · 2.9 KB
/
scan_general.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const fs = require("fs");
const path = require("path");
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});
const singleREP = /'([^']*[^\x00-\xff]{0,200}[^']*)'/g;
const doubleREP = /"([^"]*[^\x00-\xff]{0,200}[^"]*)"/g;
const fileList = [];
const hashStr = (text) => {
"use strict";
let hash = 5381,
index = text.length;
while (index) {
hash = (hash * 33) ^ text.charCodeAt(--index);
}
return hash >>> 0;
};
const hashKey = (value, onError) => {
const key =
"k_" + ("0000" + hashStr(value.replace(/\s+/g, "")).toString(36)).slice(-7);
return key;
};
const output = (md5ChineseMap) => {
readline.question(`JSON文件输出路径(含文件名):`, (path) => {
fs.writeFile(path, JSON.stringify(md5ChineseMap), (err) => {
if (err) {
console.error(err);
return;
} else {
console.log("输出成功");
return;
}
});
});
};
const flatArray = (arr) => {
if (Object.prototype.toString.call(arr) != "[object Array]") {
return false;
}
let res = [];
arr.map((item) => {
if (item instanceof Array) {
res.push(...item);
} else {
res.push(item);
}
});
return Array.from(new Set(res));
};
const scanFile = (path, resolve, reject) => {
fs.readFile(path, "utf8", (err, fileData) => {
if (err) {
console.error(err);
return;
}
let chineseList = [];
const resultSingle = fileData.match(singleREP);
if (Array.isArray(resultSingle) && resultSingle.length > 0) {
chineseList.push(...resultSingle.map((item) => item.split("'")[1]));
}
const resultDouble = fileData.match(doubleREP);
if (Array.isArray(resultDouble) && resultDouble.length > 0) {
chineseList.push(...resultDouble.map((item) => item.split('"')[1]));
}
chineseList = Array.from(new Set(chineseList));
chineseList = chineseList.filter((item) => /[\u4e00-\u9fa5]/.test(item));
resolve(chineseList);
});
};
const readPath = (currentDirPath, isMaybeFile) => {
if (isMaybeFile) {
let stat = fs.statSync(currentDirPath);
if (stat.isFile()) {
fileList.push(currentDirPath);
return;
}
}
fs.readdirSync(currentDirPath).forEach(function (name) {
let filePath = path.join(currentDirPath, name);
let stat = fs.statSync(filePath);
if (stat.isFile()) {
fileList.push(filePath);
} else if (stat.isDirectory()) {
readPath(filePath, false);
}
});
};
readline.question(`原始文件/文件夹路径:`, (path) => {
readPath(path, true);
Promise.all(
fileList.map((item) => {
return new Promise((resolve, reject) => {
return scanFile(item, resolve, reject);
});
})
).then((arr) => {
const md5ChineseMap = {};
const finalData = flatArray(arr);
finalData.forEach((item) => {
md5ChineseMap[hashKey(item)] = item;
});
output(md5ChineseMap);
});
});