-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
278 lines (241 loc) · 7.1 KB
/
cli.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env node
(async function () {
"use script";
process.on("uncaughtException", (err) => {
console.error("got an error: %s", err);
process.exitCode = 1;
});
const updateNotifier = require("update-notifier");
const whatTime = require("what-time");
const minimatch = require("minimatch");
const async = require("async");
const fs = require("fs");
const path = require("path");
const listMd = require("./src/util/readmd.js");
const meow = require("meow");
const mergeConfig = require("./src/config/mergeConfig.js");
const {
g,
y,
yow,
m,
b,
r,
relaPath,
insert_flg,
} = require("./src/util/util.js");
// Cli cmd
const cli = meow(`
Usage
$ translateMds [folder/file name] [options]
Example
$ translateMds md/
$ translateMds -T 'inlineCode,linkReference,link,heading' readme.md
${b("[options]")}
${g("-a API")} : default < baidu > ${y("{google|baidu|youdao}")}
${g("-f from ")} : default < auto detect >
${g("-t to ")} : default < zh >
${g("-N num ")} : default < 1 > ${y("{async number}")}
${g("-R rewrite")} : default < false > ${y(
"{yes/no rewrite translate file}"
)}
🌟${m("[high user options]")}❤️
${g("-D debug")}
${g("-C cache")} : default: false ${y("cache in disk")}
${g("-G google.com")} : default: false ${y(
"{ cn => com with Google api }"
)}
${g("-F force")} : default: false ${y(
"{ If, translate result is no 100%, force wirte md file }"
)}
${g("-M match")} : default [ ". ", "! "//...] ${y(
"{match this str, merge translate result }"
)}
${g("-S skips")} : default ["... ", "etc. ", "i.e. "] ${y(
"{match this str will, skip merge translate result }"
)}
${g("-T types")} : default ["html", "code"] ${y(
"{pass the md AST type}"
)}
${g("--timewait ")} : default < 80 > ${y(
"{each fetch api wait time}"
)}
${g("--values [path]")} : default: false ${y(
"{write the original of wait for translate file}"
)} ${r("[single file]")}
${g("--translate [path]")} : default: false ${y(
"{use this file translate}"
)} ${r("[single file]")}
${g("--text-glob [pattern]")} : default: false ${y(
"{text must be match, then be transalte}"
)}
${g("--no-disk")} : default: false ${y(
"{do not use cached Result}"
)}
${g("--cache-name [filename]")}: default: "translateMds" ${y(
"named the cache file"
)}
${g("--glob [pattern]")} : default: false ${y(
"{file must be match, then be transalte}"
)}
${g("--ignore [relative file/folder]")} : default: false ${y(
"{ignore files/folders string, split with `,` }"
)}
`);
updateNotifier({ pkg: cli.pkg }).notify();
// Fix write file Path is absoulte
const dir = cli.input[0];
if (!dir) {
console.error(g("--> v" + cli.pkg.version), cli.help);
process.exit(1);
}
// Merge config
const {
debug,
tranFr,
tranTo,
api,
rewrite,
asyncNum,
Force,
ignores,
glob,
Cache,
} = mergeConfig(cli);
const translateMds = require("./src/translateMds.js");
const {
loggerStart,
loggerText,
loggerStop,
oneOra,
} = require("./src/config/loggerConfig.js"); // Winston config
// after workOptions ready
const { writeDataToFile } = require("./src/util/writeDataToFile.js");
console.log(b(`> ${yow(`Cache:${Cache}`)} Starting 翻译`) + r(dir));
// Get floder markdown files Array
const getList = await listMd(path.resolve(process.cwd(), dir), {
deep: "all",
});
console.log(b(`总文件数 ${getList.length}, 有些文件会跳过`));
let Done = 0;
const noDone = [];
let showAsyncnum = 0;
loggerStart("translate running ... >> ");
async.mapLimit(getList, asyncNum, runTranslate, (err, IsTranslateS) => {
loggerStop();
if (noDone.length > 0) {
process.exitCode = 1;
}
if (err) {
throw err;
}
Done++;
if (IsTranslateS.every((x) => Boolean(x))) {
oneOra("All Done");
} else {
if (debug !== "debug") {
oneOra(
`Some No Done , ${yow("use")} cli-option${r(" { -D } ")} find the Err`
);
}
if (!Force) {
oneOra(
`Or ${yow("use")} cli-option${r(
" { -F } "
)} Force put the translate Result`
);
}
if (debug === "debug" || Force) {
oneOra(
`[${g("DEBUG")}:${debug === "debug"}|${g("Force")}:${Force}] mode`
);
}
}
oneOra(`time:${whatTime(process.uptime())}`);
});
/**
* @description async Translate filename value , Return true or false
* @param {String} value
* @returns {Boolean}
*/
async function runTranslate(value) {
const rePath = relaPath(value);
loggerText(`++++ <😊 > ${rePath}`);
let State = true;
Done++;
const localDone = Done;
// Filter same file
if (value.endsWith(`.${tranTo}.md`) || !value.endsWith(".md")) {
loggerText(b(`- 翻译的 - 或者 不是 md 文件的 ${g(rePath)}`));
return State;
}
if (value.match(/\.[a-zA-Z]{2}\.md+/)) {
// TOGO country short name
loggerText(b(`- 有后缀为 *.国家简写.md ${g(rePath)}`));
return State;
}
if (!rewrite && fs.existsSync(insert_flg(value, `.${tranTo}`, 3))) {
loggerText(b(`已翻译, 不覆盖 ${g(rePath)}`));
return State;
}
if (glob && glob.some((g) => !minimatch(value, g, { matchBase: true }))) {
loggerText(b(`glob, no match ${g(rePath)}`));
return State;
}
if (
ignores &&
ignores.some((ignore) => value.includes(path.resolve(ignore)))
) {
loggerText(b(`ignore, ${g(rePath)}`));
return State;
}
loggerText(`1. do 第${localDone}文件 ${rePath}`);
// Open async num
showAsyncnum++;
const startTime = new Date().getTime();
const _translateMds = await translateMds(
[value, api, tranFr, tranTo],
debug,
true
);
// Succeed / force wirte data
if (_translateMds.every((x) => !x.error && x.text) || Force) {
// Translate no ok
const _tranData = _translateMds.map((x) => x.text); // Single file translate data
await writeDataToFile(_tranData, value).then((text) => loggerText(text));
}
let Err;
for (const _t of _translateMds) {
if (_t.error) {
Err = _t.error;
break;
}
}
const endtime = new Date().getTime() - startTime;
const humanTime = whatTime(endtime / 1000);
if (State && !Err) {
oneOra(
`已搞定 第 ${localDone} 文件 - 并发${b(showAsyncnum)} -- ${b(
humanTime
)} - ${rePath}`
);
} else {
State = false; // Translate no ok
if (!State) {
// Write data no ok | translate no ok
noDone.push(value); // If process exit code
oneOra(
`没完成 第 ${localDone} 文件 - 并发${b(showAsyncnum)} -- ${b(
humanTime
)} - ${rePath} \n ${Err}`,
"fail"
);
}
}
showAsyncnum--;
return State;
}
process.on("exit", (_) => {
loggerStop();
});
})();