-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzh-converter.js
36 lines (33 loc) · 1020 Bytes
/
zh-converter.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
const fs = require('fs');
const path = require('path');
const chineseConv = require('chinese-conv');
exports.run = (process, toTraditional = true, filterExtname = '') => {
fs.readdir(process.cwd(), (err, data) => {
data.filter((file) => {
return filterExtname === '' || filterExtname === '*'
? true
: path.extname(file) === filterExtname;
}).map(file => {
this.convertFile(file, toTraditional);
});
});
}
exports.convertFile = async (file, toTraditional, callback) => {
fs.readFile(file, 'utf-8', (err, data) => {
try {
doConvert(file, toTraditional, data);
} catch (error) {
console.log(`%s ${file} be converted fail !`, "\x1b[31m");
if (callback)
callback(`${file} be converted fail !`);
}
});
}
function doConvert(file, toTraditional, data) {
fs.writeFile(
file,
toTraditional ? chineseConv.tify(data) : chineseConv.sify(data),
() => {
console.log(`%s ${file} be converted! success.`, "\x1b[37m");
});
}