-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindWords.js
More file actions
73 lines (68 loc) · 2.2 KB
/
FindWords.js
File metadata and controls
73 lines (68 loc) · 2.2 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
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
import * as path from 'path';
import fs from 'node:fs';
// 给一个路径,读取路径下的文件夹或者文件夹
const readDirectory = async (cusPath, result = {}) => {
try {
const files = await fs.promises.readdir(cusPath);
for (const file of files) {
const filePath = path.join(cusPath, file);
const isDir = await isDirectory(filePath);
if (isDir) {
await readDirectory(filePath, result);
} else {
const data = await readFileData(filePath);
const words = getWords(data);
if (!result[cusPath]) {
result[cusPath] = {};
}
result[cusPath][file] = { words };
}
}
} catch (err) {
console.log('Unable to scan directory: ' + err);
}
return result;
};
// 判断一个路径是不是文件夹
const isDirectory = path => {
return new Promise((resolve, reject) => {
fs.stat(path, (err, stats) => {
if (err) {
reject(err);
}
if (stats.isFile()) {
resolve(false);
} else if (stats.isDirectory()) {
resolve(true);
}
});
});
};
// 读取文件中的数据
const readFileData = filePath => {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};
// 获取指定的字符串,通过正则表达式
//$t('number of days in status|Number of Days in Status')
//.translateRaw('others|Others')
//.translate('others|Others')
//$t("number of days in status|Number of Days in Status")
//.translateRaw("others|Others")
//.translate("others|Others")
//const regex = /\$t\((['"])(.*?)\1\)|\.translateRaw\((['"])(.*?)\3\)|\.translate\((['"])(.*?)\5\)/g;
const getWords = (data, reg = /\$t\((['"])(.*?)\1\)|\.translateRaw\((['"])(.*?)\3\)|\.translate\((['"])(.*?)\5\)/g) => {
return data.match(reg);
};
/*---------------------------------------------------------------------------------------------------------*/
// 读取目录下的文件夹
let directoryName = '/apps/www/components/pages/ars';
const directoryPath = path.join(__dirname, directoryName);
readDirectory(directoryPath).then(result => console.log(JSON.stringify(result, null, 2)));