-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
48 lines (44 loc) · 1.84 KB
/
index.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
const opencc = require('node-opencc');
const path = require('path');
const fs = require('fs');
const { isBinaryFileSync } = require("isbinaryfile");
const { type } = require('./config');
const ig = require('ignore').default().add(fs.readFileSync('./.c2cignore', 'utf-8').toString());
const outputFolderPath = path.join(__dirname, './output');
if (!fs.existsSync(outputFolderPath)) fs.mkdirSync(outputFolderPath);
const s2t = e => opencc[type](e);
const getRelativePath = path => {
return path.replace(__dirname, '').replace('\\input\\', '');
}
function doReadDir(dir = path.join(__dirname, './input')) {
const files = fs.readdirSync(dir).reverse();
for (let index = 0; index < files.length; index++) {
const file = files[index];
const modulePath = path.join(dir, file);
const isDir = fs.lstatSync(modulePath).isDirectory();
if (isDir) {
doReadDir(modulePath);
} else {
try {
const outputPath = path.join(__dirname, './output', getRelativePath(modulePath));
const outputFolder = path.join(__dirname, './output', getRelativePath(dir));
if (ig.ignores(getRelativePath(modulePath))) continue;
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder, {
recursive: true
});
}
if (isBinaryFileSync(modulePath)) {
fs.copyFileSync(modulePath, outputPath);
} else {
console.log(`正在转换:${outputPath}`);
const translateData = fs.readFileSync(modulePath, 'utf8');
fs.writeFileSync(outputPath, s2t(translateData));
}
} catch (error) {
console.error(error);
}
}
}
}
doReadDir();