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.
@@ -20,7 +20,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
20
20
  * 词条提取、翻译基类
21
21
  */
22
22
  var Extract = function () {
23
- function Extract(option) {
23
+ function Extract(option, words) {
24
24
  (0, _classCallCheck3.default)(this, Extract);
25
25
 
26
26
  this.option = Object.assign({}, {
@@ -29,45 +29,44 @@ var Extract = function () {
29
29
  ignoreCode: /<!--\s*hide|-->/g,
30
30
  // 将对应的词条全部修改为'/**<%%>**/window.MS'
31
31
  // templateExp: /(\=|\+|\-|\*|\/|\s|\(|\[|\{)\s*<%.*?%>/g
32
- templateExp: /<%([^\n]*?)%>/g,
32
+ templateExp: /<%\s*multilang\(([^\n]*?)\);*\s*%>/g ///<%([^\n]*?)%>/g,只能存在一个捕获组
33
33
  // 自定义不提词条规则,可以是正则也可以是function
34
- customRules: []
34
+ // customRules: []
35
35
  }, option);
36
- this.init();
36
+ this.init(words);
37
37
  }
38
38
 
39
- Extract.prototype.init = function init() {
40
- var _this = this;
41
-
39
+ Extract.prototype.init = function init(words) {
40
+ this.warnings = [];
41
+ this.words = words;
42
42
  // 记录当前的文件路径
43
43
  this.curFilePath = "";
44
- // 提取的词条,去除了重复项,当为翻译模式时间,只存储未被翻译的词条
45
- this.words = [];
46
44
  // 是否正在处理文件
47
45
  this.isWorking = false;
48
46
  // 待处理文件列表
49
47
  this.handleList = [];
50
48
  this.ignoreRE = _config.IGNORE_REGEXP.slice(0);
51
49
  this.ignoreFuns = _config.IGNORE_FUNCTIONS.slice(0);
52
-
53
- var customRules = this.option.customRules;
54
- if (Array.isArray(customRules)) {
55
- customRules.forEach(function (item) {
56
- if (typeof item === "function") {
57
- _this.ignoreFuns.push(item);
58
- } else {
59
- _this.ignoreRE.push(item);
60
- }
61
- });
62
- } else if (typeof customRules === "function") {
63
- this.ignoreFuns.push(customRules);
64
- } else if (customRules) {
65
- this.ignoreRE.push((0, _index.string2Regexp)(customRules));
66
- }
50
+ this.suspectLine = [];
51
+
52
+ // let customRules = this.option.customRules;
53
+ // if (Array.isArray(customRules)) {
54
+ // customRules.forEach(item => {
55
+ // if (typeof item === "function") {
56
+ // this.ignoreFuns.push(item);
57
+ // } else {
58
+ // this.ignoreRE.push(item);
59
+ // }
60
+ // });
61
+ // } else if (typeof customRules === "function") {
62
+ // this.ignoreFuns.push(customRules);
63
+ // } else if (customRules) {
64
+ // this.ignoreRE.push(string2Regexp(customRules));
65
+ // }
67
66
  };
68
67
 
69
68
  Extract.prototype.handleFile = function handleFile(filePath) {
70
- var _this2 = this;
69
+ var _this = this;
71
70
 
72
71
  // log(`开始提取文件-${filePath}`);
73
72
  this.isWorking = true;
@@ -75,17 +74,34 @@ var Extract = function () {
75
74
  return (0, _index.loadFile)(filePath).then(function (data) {
76
75
  // 写入文件
77
76
  (0, _index.log)("\u6DFB\u52A0\u7FFB\u8BD1\u51FD\u6570-" + filePath);
78
- return _this2.transNode(data);
77
+ return _this.transNode(data);
79
78
  }).then(function (AST) {
80
- return _this2.scanNode(AST);
79
+ return _this.scanNode(AST);
81
80
  }).then(function (fileData) {
82
- (0, _index.writeTextFile)(_path2.default.resolve(_this2.option.baseWritePath, _path2.default.relative(_this2.option.baseReadPath, _this2.curFilePath)), fileData);
83
- _this2.complete();
84
- return _this2.startTrans();
81
+ (0, _index.writeTextFile)(_path2.default.resolve(_this.option.baseWritePath, _path2.default.relative(_this.option.baseReadPath, _this.curFilePath)), fileData);
82
+ _this.complete();
83
+ // 记录可能需要人工二次审核的词条
84
+ var matchs = fileData.match(/(alert|confirm)\(.*?\)/gi);
85
+ if (matchs) {
86
+ matchs = matchs.filter(function (item) {
87
+ return !(/(alert|confirm)\((.).*?\2\)/gi.test(item) || /_\(.*?\)/g.test(item) || !/\s/g.test(item));
88
+ });
89
+ matchs = matchs.map(function (item) {
90
+ return " [\u7FFB\u8BD1\u51FD\u6570\u7F3A\u5931\uFF1F]\uFF1A[" + item + "]";
91
+ });
92
+ }
93
+ if (matchs && matchs.length > 0 || _this.warnings.length > 0) {
94
+ var _suspectLine;
95
+
96
+ (_suspectLine = _this.suspectLine).push.apply(_suspectLine, ["#--#--#--#--#--# \u6587\u4EF6\u5730\u5740\u3010" + _this.curFilePath + "\u3011 #--#--#--#--#--#"].concat(Array.from(new Set(_this.warnings)), matchs || []));
97
+ }
98
+
99
+ return _this.startTrans();
85
100
  }).catch(function (error) {
86
- _this2.copyFile(filePath);
87
- (0, _index.log)("\u6587\u4EF6[" + filePath + "]\u5904\u7406\u51FA\u9519- " + error.message, _index.LOG_TYPE.ERROR);
88
- return _this2.startTrans();
101
+ _this.copyFile(filePath);
102
+ (0, _index.log)("\u6587\u4EF6[" + filePath + "]\u5904\u7406\u51FA\u9519", _index.LOG_TYPE.ERROR);
103
+ (0, _index.log)(error, _index.LOG_TYPE.ERROR);
104
+ return _this.startTrans();
89
105
  });
90
106
  };
91
107
 
@@ -98,92 +114,29 @@ var Extract = function () {
98
114
  return Promise.resolve(data);
99
115
  };
100
116
 
101
- Extract.prototype.setAttr = function setAttr(attr, value) {
102
- if (Object.prototype.toString.call(attr) === "[object Object]") {
103
- for (var key in attr) {
104
- this.setSingleAttr(key, attr[key]);
105
- }
106
- } else {
107
- this.setSingleAttr(attr, value);
108
- }
109
- };
110
-
111
- Extract.prototype.setSingleAttr = function setSingleAttr(attr, value) {
112
- this.option[attr] = value;
113
- };
114
-
115
117
  Extract.prototype.startTrans = function startTrans() {
116
118
  // 当一个文件执行完成,立即执行下一个指令
117
119
  if (this.handleList.length > 0) {
118
120
  return this.handleFile(this.handleList.shift());
119
121
  }
120
- return Promise.resolve("done");
122
+ return Promise.resolve(this.suspectLine);
121
123
  };
122
124
 
123
125
  Extract.prototype.addTask = function addTask(filePath) {
124
126
  this.handleList.push(filePath);
125
127
  };
126
128
 
127
- Extract.prototype.addWord = function addWord(word) {
128
- if (!this.words.includes(word)) {
129
- this.words.push(word);
130
- }
131
- };
132
-
133
- Extract.prototype.addWords = function addWords(words) {
134
- var _this3 = this;
135
-
136
- words.forEach(function (word) {
137
- _this3.addWord(word);
138
- });
139
- };
140
-
141
- Extract.prototype.getWord = function getWord(val) {
142
- if (!val || /^\s*$/.test(val)) {
143
- return "";
144
- }
145
-
146
- // 经过处理的字符串
147
- if (/\{%s\}/i.test(val)) {
148
- return val;
149
- }
150
-
151
- var skip = this.ignoreRE.some(function (item) {
152
- return item.test(val);
153
- });
154
- if (skip) {
155
- return "";
129
+ Extract.prototype.transWord = function transWord(val) {
130
+ var word = this.words[val];
131
+ if (word === undefined) {
132
+ this.warnings.push(" [\u8BCD\u6761\u4E0D\u5B58\u5728]\uFF1A[" + val + "]");
156
133
  }
157
-
158
- for (var i = 0, l = this.ignoreFuns.length; i < l; i++) {
159
- var fun = this.ignoreFuns[i],
160
- str = val.replace(/(^\s+)|(\s+$)/g, "");
161
-
162
- if (typeof fun === "function") {
163
- if (skip = fun(str)) {
164
- break;
165
- }
166
- }
167
- }
168
- if (skip) {
169
- return "";
170
- }
171
-
172
- var addValue = "";
173
-
174
- //中英文都提取
175
- if (/[a-z]/i.test(val) || /[\u4e00-\u9fa5]/.test(val)) {
176
- addValue = val;
177
- }
178
-
179
- return addValue;
134
+ return word;
180
135
  };
181
136
 
182
137
  Extract.prototype.complete = function complete() {
183
138
  this.isWorking = false;
184
139
  this.option.onComplete && this.option.onComplete(this.curFilePath, this.words);
185
- // 重置提取的词条
186
- this.words = [];
187
140
  };
188
141
 
189
142
  return Extract;
@@ -35,20 +35,23 @@ var JSDOM = jsdom.JSDOM;
35
35
  var ExtractHTML = function (_Extract) {
36
36
  (0, _inherits3.default)(ExtractHTML, _Extract);
37
37
 
38
- function ExtractHTML(option) {
38
+ function ExtractHTML(option, words) {
39
39
  (0, _classCallCheck3.default)(this, ExtractHTML);
40
40
 
41
- var _this = (0, _possibleConstructorReturn3.default)(this, _Extract.call(this, option));
41
+ var _this = (0, _possibleConstructorReturn3.default)(this, _Extract.call(this, option, words));
42
42
 
43
43
  _this.extractJS = new _extract_js_ori2.default({
44
44
  ignoreCode: _this.option.ignoreCode,
45
45
  templateExp: _this.option.templateExp
46
- });
46
+ }, words);
47
47
  _this.jsHandleList = [];
48
48
  return _this;
49
49
  }
50
50
 
51
51
  ExtractHTML.prototype.transNode = function transNode(html) {
52
+ var _this2 = this;
53
+
54
+ html = html.replace(/\s*<!--\s*<(\/?)script>\s*-->/ig, '');
52
55
  this.oldHtml = html;
53
56
  this.scripts = [];
54
57
  this.getHeaderTag(html);
@@ -59,7 +62,7 @@ var ExtractHTML = function (_Extract) {
59
62
  var dom = new JSDOM(html, {
60
63
  virtualConsole: virtualConsole
61
64
  });
62
- var document = dom.window.document;
65
+ var document = _this2.document = dom.window.document;
63
66
  resolve(document);
64
67
  } catch (err) {
65
68
  reject(err);
@@ -76,34 +79,58 @@ var ExtractHTML = function (_Extract) {
76
79
 
77
80
 
78
81
  ExtractHTML.prototype.scanNode = function scanNode(document) {
79
- var _this2 = this;
82
+ var _this3 = this;
80
83
 
84
+ this.warnings = [];
81
85
  // 遍历各节点
82
86
  this.listNode(document.documentElement);
83
87
 
84
88
  return this.nextJsTask().then(function () {
85
89
  // ͨ通过正则替换,为了规避jsdom对html中的特殊字符串进行编码
86
90
  var outHtml = document.documentElement.innerHTML;
87
- var match = outHtml.match(/<script\b[^>]*>[\s\S]*?<\/script>/g);
88
- outHtml = _this2.oldHtml.replace(/<script\b[^>]*>[\s\S]*?<\/script>/g, function () {
89
- return match.shift();
90
- });
91
+ return _this3.handleHtml(outHtml);
92
+ });
93
+ };
94
+
95
+ ExtractHTML.prototype.handleHtml = function handleHtml(htmlCode) {
96
+ var _this4 = this;
97
+
98
+ var oldHtml = this.oldHtml;
99
+ var match = htmlCode.match(/<script\b[^>]*>[\s\S]*?<\/script>/gi);
100
+ htmlCode = oldHtml.replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, function () {
101
+ return match.shift();
102
+ });
103
+
104
+ var templateExp = this.option.templateExp;
91
105
 
92
- return outHtml;
106
+ return htmlCode.replace(templateExp, function (match, p1, index) {
107
+ var reg = (0, _index.escapeRegExp)(match);
108
+ var tempReg = templateExp.toString();
109
+ reg += templateExp.source;
110
+ reg = new RegExp(reg, tempReg.slice(tempReg.lastIndexOf("/") + 1));
111
+ var word = _this4.transWord(p1);
112
+ if (reg.test(htmlCode) && word && !/\s/.test(htmlCode[index + match.length])) {
113
+ return "word ";
114
+ }
115
+ return word || match;
93
116
  });
94
117
  };
95
118
 
96
119
  ExtractHTML.prototype.handleJsTask = function handleJsTask(child) {
97
- var _this3 = this;
120
+ var _this5 = this;
98
121
 
99
- return this.extractJS.transNode(child.nodeValue, true).then(function (AST) {
100
- return _this3.extractJS.scanNode(AST);
122
+ return this.extractJS.transNode(child.innerHTML, true).then(function (AST) {
123
+ return _this5.extractJS.scanNode(AST);
101
124
  }).then(function (fileData) {
125
+ var _warnings;
126
+
102
127
  // 写入文件
103
- child.nodeValue = fileData;
104
- return _this3.nextJsTask();
105
- }).catch(function () {
106
- return _this3.nextJsTask();
128
+ child.innerHTML = fileData;
129
+ (_warnings = _this5.warnings).push.apply(_warnings, _this5.extractJS.warnings);
130
+ return _this5.nextJsTask();
131
+ }).catch(function (e) {
132
+ (0, _index.log)(e, _index.LOG_TYPE.ERROR);
133
+ return _this5.nextJsTask();
107
134
  });
108
135
  };
109
136
 
@@ -126,25 +153,24 @@ var ExtractHTML = function (_Extract) {
126
153
 
127
154
  var firstChild = element.firstChild,
128
155
  nextSibling = element.nextSibling,
129
- nodeType = element.nodeType,
130
156
  nodeName = element.nodeName.toLowerCase();
131
- // 处理html节点
132
- // nodeType: 1-元素 2-属性 3-文本内容 8-代表注释
133
- if (nodeType === 1 && nodeName == "script") {
134
- if (firstChild && firstChild.nodeValue && (0, _index.trim)(firstChild.nodeValue)) {
135
- this.addJsTask(firstChild);
136
- }
137
- } else {
138
- // 处理子节点
139
- if (firstChild) {
140
- this.listNode(firstChild);
141
- }
157
+
158
+ if (nodeName == "script") {
159
+ this.addJsTask(element);
160
+ nextSibling && this.listNode(nextSibling);
161
+ return;
142
162
  }
143
163
 
144
- // 处理兄弟节点
145
- if (nextSibling) {
146
- this.listNode(nextSibling);
164
+ // noscript内的文本不处理
165
+ if (nodeName === "noscript" || nodeName == "style") {
166
+ nextSibling && this.listNode(nextSibling);
167
+ return;
147
168
  }
169
+ // 处理子节点
170
+ firstChild && this.listNode(firstChild);
171
+
172
+ // 处理兄弟节点
173
+ nextSibling && this.listNode(nextSibling);
148
174
  };
149
175
 
150
176
  return ExtractHTML;