b28-cli 1.6.7 → 1.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,420 +14,69 @@ var _inherits2 = require("babel-runtime/helpers/inherits");
14
14
 
15
15
  var _inherits3 = _interopRequireDefault(_inherits2);
16
16
 
17
- var _babylon = require("babylon");
18
-
19
- var _babelGenerator = require("babel-generator");
20
-
21
- var _babelGenerator2 = _interopRequireDefault(_babelGenerator);
22
-
23
17
  var _extract = require("./extract");
24
18
 
25
19
  var _extract2 = _interopRequireDefault(_extract);
26
20
 
27
- var _config = require("../util/config");
28
-
29
21
  var _index = require("../util/index");
30
22
 
31
23
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32
24
 
33
- var htmlparser = require("htmlparser2");
34
- var HANDLE_ATTRIBUTE = ["alt", "placeholder", "title"];
35
-
36
25
  /**
37
26
  * JS文件解析类
38
27
  */
39
-
40
28
  var ExtractJs = function (_Extract) {
41
29
  (0, _inherits3.default)(ExtractJs, _Extract);
42
30
 
43
- function ExtractJs(option) {
31
+ function ExtractJs(option, words) {
44
32
  (0, _classCallCheck3.default)(this, ExtractJs);
45
-
46
- var _this = (0, _possibleConstructorReturn3.default)(this, _Extract.call(this, option));
47
-
48
- _this.isAST = true;
49
- _this.tempNodes = [];
50
- _this.oldCode = "";
51
- _this.newCode = "";
52
- _this.offSet = 0;
53
- return _this;
33
+ return (0, _possibleConstructorReturn3.default)(this, _Extract.call(this, option, words));
54
34
  }
55
35
 
56
- ExtractJs.prototype.transNode = function transNode(jsDoc, fromHtml) {
57
- var _this2 = this;
58
-
59
- this.fromHtml = !!fromHtml;
60
- this.newCode = jsDoc;
61
- // 修正原厂代码中的语法错误项,默认语法错误项是不需要进行提取的项
62
- this.oldCode = jsDoc = this.correctCode(jsDoc);
63
-
64
- if (this.oldCode.length !== this.newCode.length) {
65
- return Promise.resolve(this.newCode);
66
- }
67
-
68
- return new Promise(function (resolve, reject) {
69
- try {
70
- var AST = (0, _babylon.parse)(jsDoc, {
71
- sourceType: "script"
72
- });
73
- resolve(AST);
74
- } catch (err) {
75
- var _AST = (0, _babylon.parse)(jsDoc, {
76
- sourceType: "module"
77
- });
78
-
79
- resolve(_AST);
80
- }
81
- }).catch(function (err) {
82
- _this2.fromHtml ? (0, _index.log)("\u5185\u8054JS\u5904\u7406\u51FA\u9519- " + err, _index.LOG_TYPE.ERROR) : (0, _index.log)("js代码包含语法错误,故无法添加翻译函数!" + err.message, _index.LOG_TYPE.ERROR);
83
- });
36
+ ExtractJs.prototype.transNode = function transNode(jsDoc) {
37
+ this.warnings = [];
38
+ return Promise.resolve(jsDoc);
84
39
  };
85
40
 
86
41
  // 扫描节点,提取字段
87
42
 
88
43
 
89
- ExtractJs.prototype.scanNode = function scanNode(AST) {
90
- var _this3 = this;
44
+ ExtractJs.prototype.scanNode = function scanNode(jsDoc) {
45
+ var _this2 = this;
91
46
 
92
- this.offSet = 0;
93
47
  return new Promise(function (resolve, reject) {
94
- var body = AST.program.body;
95
- body.forEach(function (node) {
96
- _this3.listAst(node);
97
- });
98
-
99
- if (_this3.fromHtml) {
100
- _this3.newCode = _this3.newCode.replace(/\(\[([^\n]*?)\]\)/g, function (match, val) {
101
- return "<%" + val + "%>";
102
- });
103
- }
104
- resolve(_this3.newCode);
105
- });
106
- };
107
-
108
- ExtractJs.prototype.getValue = function getValue(nodeArgs) {
109
- if (nodeArgs) {
110
- if (nodeArgs.value !== undefined) {
111
- return nodeArgs.value;
112
- }
113
-
114
- var value = "",
115
- left = nodeArgs.left;
116
-
117
- while (left && left.right) {
118
- value += left.right.value;
119
- left = left.left;
120
- }
121
-
122
- value = (left ? left.value : "") + value + (nodeArgs.right ? nodeArgs.right.value : "");
123
- return value;
124
- }
125
- return "";
126
- };
127
-
128
- ExtractJs.prototype.listAst = function listAst(astNode) {
129
- var _this4 = this;
130
-
131
- var type = (0, _index.getType)(astNode);
132
- if (type === "Object") {
133
- if (/^(BinaryExpression|BinaryExpression)$/i.test(astNode.type)) {
134
- return;
135
- }
136
-
137
- if (/^(CallExpression|StringLiteral)$/i.test(astNode.type)) {
138
- this.listNode(astNode);
139
- return;
140
- }
141
- // 对于注释的项不进行遍历
142
- var ignoreKeys = ["leadingComments", "trailingComments", "sourceElements", "loc", "id"];
143
- for (var key in astNode) {
144
- if (ignoreKeys.includes(key)) {
145
- continue;
146
- }
147
- this.listAst(astNode[key]);
148
- }
149
- } else if (type === "Array") {
150
- astNode.forEach(function (node) {
151
- _this4.listAst(node);
152
- });
153
- }
154
- };
155
-
156
- ExtractJs.prototype.listNode = function listNode(node) {
157
- switch (node.type) {
158
- case "CallExpression":
159
- if (node.callee) {
160
- var name = "";
161
- if (node.callee.type === "Identifier") {
162
- name = node.callee.name;
163
- } else {
164
- name = this.oldCode.substring(node.callee.start, node.callee.end);
165
- }
166
- if (/^(confirm|alert|document\.write(ln)?)$/i.test(name)) {
167
- // 解析表达式的值,并且重写函数
168
- if (node.arguments.length === 1) {
169
- this.addCallTrans(node.arguments[0]);
170
- }
171
- } else {
172
- // 对于字符串相关的表达式不进行提取
173
- }
174
- }
175
- break;
176
- case "StringLiteral":
177
- this.addStringTrans(node);
178
- break;
179
- }
180
- };
181
-
182
- // 给源码中的文本添加翻译函数
183
-
184
-
185
- ExtractJs.prototype.addStringTrans = function addStringTrans(node) {
186
- var res = this.getWord(node.value);
187
- if (!res) {
188
- return;
189
- }
190
- var htmlAst = htmlparser.parseDOM(res);
191
-
192
- if (htmlAst.length > 0) {
193
- if (htmlAst.length > 1 || htmlAst[0].type !== "text") {
194
- // 解析html
195
- res = this.analyseDom(htmlAst, [], res);
196
- } else {
197
- res = "_('" + res.replace(/'/g, "\\") + "')";
198
- }
199
- this.addTrans(res, { start: node.start, end: node.end });
200
- }
201
- };
202
-
203
- // 替换源码中的文本
204
-
205
-
206
- ExtractJs.prototype.addTrans = function addTrans(val, loc) {
207
- //去除无效的表达式
208
- val = val.replace(/'[\s]*' \+ /g, "").replace(/\+ '[\s]*'/g, "").replace(/^\s+|\s+$/g, "");
209
-
210
- this.newCode = this.newCode.splice(loc.start + this.offSet, loc.end + this.offSet, val);
211
- this.offSet += val.length - (loc.end - loc.start);
212
- };
213
-
214
- // 给源码中的表达式添加翻译函数
215
-
216
-
217
- ExtractJs.prototype.addCallTrans = function addCallTrans(node) {
218
- switch (node.type) {
219
- case "BinaryExpression":
220
- // 遍历参数
221
- var res = void 0,
222
- arr = [],
223
- args = "";
224
- res = this.analyseBinarry(node, arr);
225
-
226
- // 对于无string参与的表达式不进行翻译函数的添加
227
- if (!res.hasStr || !res.text) {
228
- break;
229
- }
230
-
231
- res = res.text;
232
- var htmlAst = htmlparser.parseDOM(res);
233
- if (htmlAst.length > 1 || htmlAst[0].type !== "text") {
234
- // 解析html
235
- res = this.analyseDom(htmlAst, arr, res);
236
- } else {
237
- // 将标记还原
238
- if (arr.length > 0) {
239
- args = ", [" + arr.join(", ") + "]";
240
- res = res.replace(/\{%s\}/g, function () {
241
- return "%s";
242
- });
243
- }
244
- res = "_('" + res.replace(/'/g, "\\") + "'" + args + ")";
245
- }
246
- this.addTrans(res, { start: node.start, end: node.end });
247
- break;
248
- case "StringLiteral":
249
- this.addStringTrans(node);
250
- break;
251
- }
252
- };
253
-
254
- ExtractJs.prototype.analyseBinarry = function analyseBinarry(node, args) {
255
- var res = "",
256
- right = void 0,
257
- left = node.left,
258
- hasStr = false;
259
-
260
- if (right = node.right) {
261
- if (right.type === "StringLiteral") {
262
- hasStr = true;
263
- res = right.value.replace(/\n/, "\\n") + res;
264
- } else {
265
- args.unshift(this.oldCode.substring(right.start, right.end));
266
- res = "{%s}" + res;
267
- }
268
- }
269
-
270
- switch (left.type) {
271
- case "BinaryExpression":
272
- var innerRes = this.analyseBinarry(left, args);
273
- hasStr = hasStr || innerRes.hasStr;
274
- res = innerRes.text + res;
275
- break;
276
- case "StringLiteral":
277
- hasStr = true;
278
- res = left.value.replace(/\n/, "\\n") + res;
279
- break;
280
- default:
281
- // 默认当做参数进行处理
282
- args.unshift(this.oldCode.substring(left.start, left.end));
283
- res = "{%s}" + res;
284
- break;
285
- }
286
- return {
287
- text: res,
288
- hasStr: hasStr
289
- };
290
- };
291
-
292
- ExtractJs.prototype.analyseDom = function analyseDom(ast, args, nodeHtml) {
293
- var res = this.analyseHtmlNode(ast, args, nodeHtml.toLowerCase().match(/<\/[a-z0-9]+?>/gi) || []),
294
- text = res.text,
295
- isTranStart = res.isTranStart;
296
-
297
- if (!isTranStart) {
298
- text = "'" + text;
299
- }
300
-
301
- if (res.isStrEnd) {
302
- text = text + "'";
303
- }
304
-
305
- return text;
306
- };
307
-
308
- // 解析html节点元素,生成对应的代码,此处解析的字符串htmlAst为将参数替换为{%s}后的ast
309
- // <td><input type='text' id='vid{%s}' value='{%s}'></td>
310
-
311
-
312
- ExtractJs.prototype.analyseHtmlNode = function analyseHtmlNode(ast, args, endTags) {
313
- var _this5 = this;
314
-
315
- var res = "",
316
- isStrEnd = true;
317
- ast.forEach(function (item) {
318
- if (item.children) {
319
- var nodeHtml = (isStrEnd ? "" : "+ '") + ("<" + item.name);
320
- for (var key in item.attribs) {
321
- var attr = item.attribs[key];
322
- if (/\{%s\}/g.test(attr)) {
323
- attr = attr.replace(/\{%s\}/g, function () {
324
- return "'+ " + args.shift() + " + '";
48
+ try {
49
+ var reg = _this2.option.templateExp;
50
+ if (reg.test(jsDoc)) {
51
+ var s1 = reg.toString();
52
+ var s2 = reg.source;
53
+ var s3 = "('|\")" + s2 + "\\1";
54
+ var newReg = new RegExp(s3, s1.slice(s1.lastIndexOf("/") + 1));
55
+
56
+ jsDoc = jsDoc
57
+ // 对于'<% xxx %>'添加翻译函数
58
+ .replace(newReg, function (match, p1, p2) {
59
+ return _this2.transWord(p2) ? "_(" + p1 + _this2.transWord(p2) + p1 + ")" : match;
60
+ });
61
+ if (reg.test(jsDoc)) {
62
+ _this2.warnings.push(' [文件检查]:[该文件中js代码部分可能存在格式不正确的翻译函数或字符串未添加翻译函数]');
63
+ // 对于<% xxx %>添加翻译函数
64
+ jsDoc = jsDoc.replace(reg, function (match, p1) {
65
+ return _this2.transWord(p1) ? _this2.transWord(p1) : match;
325
66
  });
326
- } else if (HANDLE_ATTRIBUTE.includes(key)) {
327
- // 处理input的value等属性
328
- attr = "'+ _('" + attr.replace(/'/g, "'") + "') + '";
329
- } else if (key === "value" && item.name === "input" && /button|submit|reset/gi.test(item.attribs["type"])) {
330
- attr = "'+ _('" + attr.replace(/'/g, "'") + "') + '";
331
67
  }
332
-
333
- nodeHtml += attr ? " " + key + "=\"" + attr + "\"" : " " + key;
334
- }
335
- if (/^(input|br)$/i.test(item.name)) {
336
- nodeHtml += "/>";
337
- isStrEnd = true;
338
- } else {
339
- nodeHtml += ">";
340
- var data = _this5.analyseHtmlNode(item.children, args, endTags);
341
- var endTag = "</" + item.name + ">";
342
- data.isTranStart && (nodeHtml += "' + ");
343
- nodeHtml += data.text;
344
- data.isStrEnd || (nodeHtml += " + '");
345
- // nodeHtml += ;
346
- if (endTags.length > 0) {
347
- if (endTags[0] === endTag) {
348
- endTags.shift();
349
- }
350
- nodeHtml += endTag;
351
- }
352
- }
353
- // 对于还有结余的标签全部补上
354
- while (endTags.length > 0) {
355
- nodeHtml += "" + endTags.shift();
356
68
  }
357
- res += nodeHtml;
358
- isStrEnd = true;
359
- } else {
360
- // 用于类似_(member[1])的词条不添加翻译
361
- if (/^\s*\{%s\}\s*$/.test(item.data)) {
362
- res += "'+ _(" + args.shift() + ")";
363
- isStrEnd = false;
364
- } else {
365
- var _data = _this5.analyseHtmlText(item, args);
366
- if (_data) {
367
- if (/^_/.test(_data)) {
368
- res += isStrEnd ? "' + " + _data : " + " + _data;
369
- isStrEnd = false;
370
- } else {
371
- res += _data;
372
- isStrEnd = true;
373
- }
69
+ // 对alert、confirm添加翻译函数
70
+ jsDoc = jsDoc.replace(/(alert|confirm)\((('|").*?\3)\)/gi, function (a, b, c) {
71
+ if (/_\(.*?\)/g.test(a)) {
72
+ return a;
374
73
  }
375
- }
376
- }
377
- });
378
-
379
- return {
380
- isStrEnd: isStrEnd,
381
- text: res,
382
- isTranStart: /^_/.test(res)
383
- };
384
- };
385
-
386
- // 解析html文本元素,生成对应的代码
387
-
388
-
389
- ExtractJs.prototype.analyseHtmlText = function analyseHtmlText(node, args) {
390
- var val = this.getWord(node.data).replace(/^\s+|\s+$/g, "");
391
- if (val) {
392
- var match = 0;
393
- val = val.replace(/\{%s\}/gi, function () {
394
- match++;
395
- return "%s";
396
- });
397
-
398
- if (match) {
399
- return "_('" + val.replace(/'/g, "\\") + "', [" + args.splice(0, match) + "])";
400
- } else {
401
- return "_('" + val.replace(/'/g, "\\") + "')";
402
- }
403
- } else {
404
- // html中ignore文本同样需要翻译
405
- val = node.data.replace(/^\s+|\s+$/g, "");
406
- if (val && !/^(&nbsp;)+$/gi.test(val)) {
407
- return "_('" + val.replace(/'/g, "\\") + "')";
408
- }
409
- }
410
- return node.data.replace(/^\s+|\s+$/g, "");
411
- };
412
-
413
- ExtractJs.prototype.listTenpAst = function listTenpAst() {
414
- var _this6 = this;
415
-
416
- this.tempNodes.forEach(function (item) {
417
- _this6.listAst(item);
418
- });
419
- this.tempNodes = [];
420
- };
421
-
422
- ExtractJs.prototype.correctCode = function correctCode(str) {
423
- return str.replace(this.option.ignoreCode, function (match) {
424
- if (match.length >= 4) {
425
- return "/*" + match.slice(2, -2) + "*/";
426
- } else {
427
- return Math.pow(10, match.length - 1) + "";
74
+ return a.replace(c, "_(" + c + ")");
75
+ });
76
+ resolve(jsDoc);
77
+ } catch (e) {
78
+ (0, _index.log)(e, _index.LOG_TYPE.ERROR);
428
79
  }
429
- }).replace(this.option.templateExp, function (match, val) {
430
- return "([" + val.replace(/;$/, " ") + "])";
431
80
  });
432
81
  };
433
82
 
package/dist/handle.js CHANGED
@@ -86,7 +86,9 @@ function addTrans(cfg) {
86
86
  baseReadPath: cfg.baseProPath,
87
87
  baseWritePath: cfg.baseProOutPath,
88
88
  ignoreCode: cfg.ignoreCode,
89
- templateExp: cfg.templateExp
89
+ templateExp: cfg.templateExp,
90
+ enPath: cfg.enPath,
91
+ otherLangPath: cfg.otherLangPath ? cfg.otherLangPath.split(",") : []
90
92
  });
91
93
  return extractOri.scanFile();
92
94
  }
package/dist/index.js CHANGED
File without changes
@@ -62,7 +62,7 @@ var COMMAD = {
62
62
  TRANS_ENCODE: 9
63
63
  };
64
64
 
65
- var COMMAD_TEXT = ["提取词条", "翻译文件", "翻译检查", "Excel转JSON", "JSON转Excel", "JSON合并", "添加翻译", "提取翻译", "翻译文件检查", "文件转码"];
65
+ var COMMAD_TEXT = ["提取词条", "翻译文件", "翻译检查", "Excel转JSON", "JSON转Excel", "JSON合并", "原厂代码添加翻译", "提取词条及json文件生成一个excel表格", "翻译文件检查", "文件转码"];
66
66
 
67
67
  var valid = {
68
68
  // 空或者存在的地址
@@ -277,6 +277,16 @@ var baseQuestions = [{
277
277
  default: function _default(answers) {
278
278
  return (0, _index.getDirname)(answers.baseProPath);
279
279
  }
280
+ }, {
281
+ type: "input",
282
+ name: "enPath",
283
+ message: "原厂代码存储英文词条的C文件地址:",
284
+ validate: valid.existFile
285
+ }, {
286
+ type: "input",
287
+ name: "otherLangPath",
288
+ message: "原厂代码存储其它语言词条的C文件地址,多个文件用逗号连接:",
289
+ default: ''
280
290
  }, {
281
291
  type: "input",
282
292
  name: "ignoreCode",
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.getNowFormatDate = exports.log = exports.trim = exports.getType = exports.deepClone = exports.LOG_TYPE = exports.getDirname = exports.partMerge = exports.mergeObject = exports.writeJson = exports.correctPath = exports.writeExcel = exports.string2Regexp = exports.writeTextFile = exports.copyFile = exports.createFolder = exports.scanFolder = exports.loadExcel = exports.loadJson = exports.loadFile = exports.loadJsonSync = exports.deepMerge = exports.decodeKey = exports.formatKey = undefined;
4
+ exports.escapeRegExp = exports.getNowFormatDate = exports.log = exports.trim = exports.getType = exports.deepClone = exports.LOG_TYPE = exports.getDirname = exports.partMerge = exports.mergeObject = exports.writeJson = exports.correctPath = exports.writeExcel = exports.string2Regexp = exports.writeTextFile = exports.copyFile = exports.createFolder = exports.scanFolder = exports.loadExcel = exports.loadJson = exports.loadFileSync = exports.loadFile = exports.loadJsonSync = exports.deepMerge = exports.decodeKey = exports.formatKey = undefined;
5
5
 
6
6
  var _typeof2 = require("babel-runtime/helpers/typeof");
7
7
 
@@ -78,6 +78,14 @@ function loadFile(src) {
78
78
  });
79
79
  }
80
80
 
81
+ function loadFileSync(src) {
82
+ try {
83
+ return _fs2.default.readFileSync(src, "utf8");
84
+ } catch (e) {
85
+ return e;
86
+ }
87
+ }
88
+
81
89
  /**
82
90
  * 异步加载json文件
83
91
  * 返回Promise,参数为Object
@@ -451,11 +459,17 @@ function getNowFormatDate() {
451
459
  var currentdate = year + month + strDate + hour + min + second;
452
460
  return currentdate;
453
461
  }
462
+
463
+ function escapeRegExp(string) {
464
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
465
+ }
466
+
454
467
  exports.formatKey = formatKey;
455
468
  exports.decodeKey = decodeKey;
456
469
  exports.deepMerge = deepMerge;
457
470
  exports.loadJsonSync = loadJsonSync;
458
471
  exports.loadFile = loadFile;
472
+ exports.loadFileSync = loadFileSync;
459
473
  exports.loadJson = loadJson;
460
474
  exports.loadExcel = loadExcel;
461
475
  exports.scanFolder = scanFolder;
@@ -474,4 +488,5 @@ exports.deepClone = deepClone;
474
488
  exports.getType = getType;
475
489
  exports.trim = trim;
476
490
  exports.log = log;
477
- exports.getNowFormatDate = getNowFormatDate;
491
+ exports.getNowFormatDate = getNowFormatDate;
492
+ exports.escapeRegExp = escapeRegExp;
package/index.js CHANGED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "b28-cli",
3
- "version": "1.6.7",
3
+ "version": "1.6.9",
4
4
  "description": "translate, ExcelToJson, JsonToExcel, getText, l18n",
5
5
  "main": "./src/index.js",
6
6
  "bin": {