crabatool 1.0.13 → 1.0.18
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.
- package/.vscode/launch.json +17 -0
- package/{test/config.js → 8/craba.js} +41 -29
- package/8/package.json +17 -0
- package/8/readme.md +20 -0
- package/8/www/_Sys/UI/Grid/GridConfig.gspx +27 -0
- package/8/www/_Sys/UI/Grid/GridConfig.js +652 -0
- package/8/www/_Sys/UI/IFrame.gspx +7 -0
- package/8/www/_Sys/UI/IFrame.js +57 -0
- package/8/www/_Sys/UI/PanelConfig.gspx +55 -0
- package/8/www/_Sys/UI/PanelConfig.js +268 -0
- package/8/www/_Sys/UI/Print/ClientSelector.gspx +39 -0
- package/8/www/_Sys/UI/Print/ClientSelector.js +152 -0
- package/8/www/js/agency.js +2 -0
- package/8/www/js/craba.min.js +12 -0
- package/8/www/js/crabaEx.min.js +7 -0
- package/8/www/js/crabaNgp.js +1 -0
- package/8/www/js/echarts-all.js +1 -0
- package/8/www/js/math.min.js +10 -0
- package/8/www/skins/craba.min.css +174 -0
- package/8/www/skins/font/iconfont.eot +0 -0
- package/8/www/skins/font/iconfont.svg +1281 -0
- package/8/www/skins/font/iconfont.ttf +0 -0
- package/8/www/skins/font/iconfont.woff +0 -0
- package/8/www/skins/ui/icons/backTop.gif +0 -0
- package/8/www/skins/ui/icons/blank.gif +0 -0
- package/8/www/skins/ui/icons/browsers.jpg +0 -0
- package/8/www/skins/ui/icons/grid.png +0 -0
- package/8/www/skins/ui/icons/nodata.png +0 -0
- package/8/www/skins/ui/print/new.png +0 -0
- package/8/www/skins/ui/print/old.png +0 -0
- package/8/www/skins/ui/print/settings.gif +0 -0
- package/8/www/skins/ui/tree/first.gif +0 -0
- package/8/www/skins/ui/tree/firstNo.gif +0 -0
- package/8/www/skins/ui/tree/last.gif +0 -0
- package/8/www/skins/ui/tree/line.gif +0 -0
- package/8/www/skins/ui/tree/node.gif +0 -0
- package/8/www/skins/ui/tree/node0.gif +0 -0
- package/8/www/skins/ui/tree/none.gif +0 -0
- package/index.js +71 -28
- package/lib/config.js +19 -2
- package/lib/server.js +12 -10
- package/lib/utils.js +99 -21
- package/package.json +5 -3
- package/test/ngp.js +38 -3
- package/test/readme.md +10 -10
- package/test/test.js +38 -0
- package/tool/checkjs.js +93 -0
- package/{client → tool}/client.js +2 -2
- package/{merge → tool}/merge.js +11 -11
- package/{lib → tool}/ngptool.js +26 -26
- package/tool/progressbar.js +10 -6
- package/{lib → tool}/sockettool.js +9 -10
- package/tool/upgrade.js +121 -39
package/tool/checkjs.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
var utils = require('../lib/utils.js');
|
|
2
|
+
var config = require('../lib/config.js');
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
var ProgressBar = require('./progressbar.js');
|
|
5
|
+
var jschardet = require('iconv-jschardet');
|
|
6
|
+
var path = require('path');
|
|
7
|
+
|
|
8
|
+
module.exports.start = function() {
|
|
9
|
+
var jsFiles = utils.getFiles({ source: config.webPath, exts: '.js' });
|
|
10
|
+
|
|
11
|
+
var fileSize = 300;
|
|
12
|
+
var errList = [];
|
|
13
|
+
var errRegList = [];
|
|
14
|
+
var bigList = [];
|
|
15
|
+
var notUtf8List = [];
|
|
16
|
+
var utf8List = [];
|
|
17
|
+
var blackList = ['craba.min.js', 'crabaEx.min.js', 'crabaNgp.js', 'echarts-all.js', 'echarts.min.js', 'math.min.js'];
|
|
18
|
+
// let、const、=>、async、await、then、funName(){}
|
|
19
|
+
var dangerousJs = ['\\slet\\s', '\\sconst\\s', '\=\>', '\\sasync\\s', '\\sawait\\s', '\\.then\\('];
|
|
20
|
+
var noFunction = new RegExp('\w.*\(.*\)(\s*|\n){');
|
|
21
|
+
|
|
22
|
+
var pb = new ProgressBar('检查进度', jsFiles.length);
|
|
23
|
+
jsFiles.forEach(function(filePath) {
|
|
24
|
+
pb.value++;
|
|
25
|
+
pb.render({ value: pb.value, text: filePath });
|
|
26
|
+
|
|
27
|
+
var byte = fs.readFileSync(filePath);
|
|
28
|
+
var stats = fs.statSync(filePath);
|
|
29
|
+
if (stats.size / 1024 > fileSize) { // 大于300k的js视为大文件
|
|
30
|
+
if (!blackList.includes(path.basename(filePath))) {
|
|
31
|
+
bigList.push(`路径:${filePath} 大小:${parseInt(stats.size / 1024)}kb`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
var content = byte.toString();
|
|
36
|
+
var ret = jschardet.detect(byte);
|
|
37
|
+
if (utils.isUtf8(byte, ret.encoding) || ret.encoding == 'UTF-8') {
|
|
38
|
+
utf8List.push(filePath);
|
|
39
|
+
} else {
|
|
40
|
+
var reg = /[\u4e00-\u9FA5]+/;
|
|
41
|
+
if (!reg.test(content)) { // 不包含中文的情况,jschardet把utf-8也认为是gbk了
|
|
42
|
+
utf8List.push(filePath);
|
|
43
|
+
} else {
|
|
44
|
+
notUtf8List.push(`路径:${filePath} 编码:${ret.encoding}`);
|
|
45
|
+
content = utils.toUtf8(byte);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
var reg = new RegExp(dangerousJs.join('|'), 'ig');
|
|
50
|
+
if (reg.test(content)) { // 先检查是否有危险字符
|
|
51
|
+
errList.push(filePath);
|
|
52
|
+
|
|
53
|
+
var nreg = new RegExp(dangerousJs.join('|'), 'ig');
|
|
54
|
+
var all = content.matchAll(nreg);
|
|
55
|
+
var m = [];
|
|
56
|
+
var v = all.next()
|
|
57
|
+
while (!v.done) {
|
|
58
|
+
m.push(`不兼容字符:${v.value[0]} 位置:${v.value.index}`);
|
|
59
|
+
v = all.next()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
errRegList.push({ errList: m });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
});
|
|
66
|
+
pb.clear('检验完成');
|
|
67
|
+
|
|
68
|
+
var info = [];
|
|
69
|
+
info.push(`检验结果:一共检查${jsFiles.length}个js文件`);
|
|
70
|
+
info.push(`语法异常文件数:【${errList.length}】个`);
|
|
71
|
+
info.push(`utf8文件数:【${utf8List.length}】个`);
|
|
72
|
+
info.push(`非utf8文件数:【${notUtf8List.length}】个`);
|
|
73
|
+
console.log(info.join('\r\n'));
|
|
74
|
+
|
|
75
|
+
if (errList.length > 0) {
|
|
76
|
+
console.log('\r\n:::语法异常文件清单如下:::');
|
|
77
|
+
errList.forEach((s, index) => {
|
|
78
|
+
|
|
79
|
+
console.log(s);
|
|
80
|
+
|
|
81
|
+
var m = errRegList[index];
|
|
82
|
+
console.log(m);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (notUtf8List.length > 0) {
|
|
87
|
+
console.log('\r\n:::非utf8文件清单如下:::', notUtf8List);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (bigList.length > 0) {
|
|
91
|
+
console.log(`\r\n:::超过${fileSize}kb的文件清单如下:::`, bigList);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
}
|
|
16
16
|
});
|
|
17
17
|
ngp_socket.add_close(function(e) {
|
|
18
|
-
$common.showInfo('
|
|
18
|
+
$common.showInfo('服务器已退出,前端自动刷新服务关闭');
|
|
19
19
|
});
|
|
20
20
|
ngp_socket.add_error(function(e) {
|
|
21
|
-
$common.showError('
|
|
21
|
+
$common.showError('服务器异常,前端自动刷新服务异常');
|
|
22
22
|
});
|
|
23
23
|
})();
|
package/{merge → tool}/merge.js
RENAMED
|
@@ -38,11 +38,11 @@ function readFileList(folder, filesList) {
|
|
|
38
38
|
|
|
39
39
|
var files = fs.readdirSync(folder);
|
|
40
40
|
files.forEach(function(itm, index) {
|
|
41
|
-
var file = folder
|
|
41
|
+
var file = path.join(folder, itm);
|
|
42
42
|
var stat = fs.statSync(file);
|
|
43
43
|
if (stat.isDirectory()) {
|
|
44
44
|
//递归读取文件
|
|
45
|
-
readFileList(
|
|
45
|
+
readFileList(file, filesList)
|
|
46
46
|
} else {
|
|
47
47
|
if (filesList.indexOf(file) >= 0) {
|
|
48
48
|
return;
|
|
@@ -66,7 +66,9 @@ function buildFolder(folderInPath, fileOutPath) {
|
|
|
66
66
|
|
|
67
67
|
filesList.forEach(function(filePath) {
|
|
68
68
|
if (!filePath) return;
|
|
69
|
-
|
|
69
|
+
if (!fs.existsSync(filePath)) return;
|
|
70
|
+
|
|
71
|
+
utils.debug(filePath);
|
|
70
72
|
filePath = getAbsPath(filePath);
|
|
71
73
|
var stats = fs.statSync(filePath);
|
|
72
74
|
if (!stats.isFile()) return;
|
|
@@ -81,7 +83,7 @@ function buildFolder(folderInPath, fileOutPath) {
|
|
|
81
83
|
fileOutPath = getAbsPath(fileOutPath);
|
|
82
84
|
mkdirsSync(npath.dirname(fileOutPath));
|
|
83
85
|
fs.writeFileSync(fileOutPath, __bom + __filesContent.join('\n'), 'utf8'); // "\ufeff"->> utf-8 BOM标记
|
|
84
|
-
utils.
|
|
86
|
+
utils.debug("out file:" + fileOutPath);
|
|
85
87
|
}
|
|
86
88
|
};
|
|
87
89
|
|
|
@@ -122,7 +124,7 @@ function replaceBlackFunction(content) {
|
|
|
122
124
|
}
|
|
123
125
|
|
|
124
126
|
function compressFile(filePath) {
|
|
125
|
-
utils.
|
|
127
|
+
utils.debug("compress and merge:" + filePath);
|
|
126
128
|
|
|
127
129
|
var content = fs.readFileSync(filePath);
|
|
128
130
|
|
|
@@ -175,13 +177,11 @@ __blackFileName = args[3]; // 黑名单文件
|
|
|
175
177
|
|
|
176
178
|
if (!__inPath || !__outPath) throw ("args参数不能为空");
|
|
177
179
|
|
|
178
|
-
utils.
|
|
179
|
-
utils.
|
|
180
|
-
utils.
|
|
180
|
+
utils.debug("source files:" + __inPath);
|
|
181
|
+
utils.debug("out file:" + __outPath);
|
|
182
|
+
utils.debug("start merge...\r\n");
|
|
181
183
|
|
|
182
184
|
var startT = new Date();
|
|
183
185
|
buildFolder(__inPath, __outPath);
|
|
184
186
|
var endT = new Date();
|
|
185
|
-
utils.log(
|
|
186
|
-
utils.log("Total Times:" + (endT.getTime() - startT.getTime()));
|
|
187
|
-
utils.log("\r\n-------压缩合并任务完成----------use -ignorecompress:" + __ignoreCompress);
|
|
187
|
+
utils.log(`打包完成\t 耗时:${endT.getTime() - startT.getTime()}毫秒\t-ignorecompress:${__ignoreCompress}\t合并文件数:${(__filesContent.length / 2)}个`);
|
package/{lib → tool}/ngptool.js
RENAMED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
var config = require('
|
|
1
|
+
var config = require('../lib/config.js');
|
|
2
2
|
var path = require('path');
|
|
3
3
|
var chokidar = require('chokidar');
|
|
4
4
|
var fs = require('fs');
|
|
5
5
|
var fork = require('child_process').fork;
|
|
6
|
-
var utils = require('
|
|
6
|
+
var utils = require('../lib/utils.js');
|
|
7
7
|
|
|
8
|
-
var mergeJs = path.join(__dirname, '
|
|
9
|
-
var log = console.dir.bind(console);
|
|
8
|
+
var mergeJs = path.join(__dirname, 'merge.js');
|
|
10
9
|
var compressArgs = config.ignoreCompress ? ['-ignorecompress'] : []; // 是否压缩
|
|
11
10
|
|
|
12
11
|
// 打包biz.js
|
|
13
12
|
function mergeBiz() {
|
|
14
13
|
var bizPath = path.join(config.webPath, '/js/biz');
|
|
15
|
-
|
|
14
|
+
if (!fs.existsSync(bizPath)) {
|
|
15
|
+
return utils.error('error biz.js关联打包的路径不存在,已经取消打包功能。如需打包请先创建目录:' + bizPath);
|
|
16
|
+
}
|
|
16
17
|
|
|
17
18
|
var watcher = chokidar.watch([bizPath], {
|
|
18
19
|
persistent: true,
|
|
@@ -20,15 +21,15 @@ function mergeBiz() {
|
|
|
20
21
|
});
|
|
21
22
|
|
|
22
23
|
var watchAction = function() {
|
|
23
|
-
|
|
24
|
+
utils.debug('打包中');
|
|
24
25
|
// 这里进行文件更改后的操作
|
|
25
26
|
var p = fork(mergeJs, [`${[bizPath]}`, `${[path.join(config.webPath, '/js/biz.js')]}`, compressArgs])
|
|
26
27
|
p.on('exit', code => {
|
|
27
|
-
|
|
28
|
+
utils.debug('打包完成');
|
|
28
29
|
});
|
|
29
30
|
}
|
|
30
31
|
watcher
|
|
31
|
-
.on('ready', () =>
|
|
32
|
+
.on('ready', () => utils.debug(`${bizPath},Initial scan complete. Ready for changes.`))
|
|
32
33
|
//.on('add', path => log(`File ${path} has been added`))
|
|
33
34
|
.on('change', path => watchAction({ event: 'change', eventPath: path }))
|
|
34
35
|
.on('unlink', path => watchAction({ event: 'remove', eventPath: path }));
|
|
@@ -38,14 +39,20 @@ function mergeBiz() {
|
|
|
38
39
|
|
|
39
40
|
// 打包生成jxc/js/init.js
|
|
40
41
|
function mergeInit() {
|
|
42
|
+
var modJs = path.join(config.webPath, '/js/' + config.modName + '.js'); // jxc/js/jxc.js
|
|
43
|
+
if (!fs.existsSync(modJs)) {
|
|
44
|
+
return utils.error(`error 模块对应的资源不存在:${modJs},已经取消打包功能。如需打包,请先将${config.modName}/js/init.js修改为:${config.modName}/js/${config.modName}.js`);
|
|
45
|
+
}
|
|
46
|
+
|
|
41
47
|
var inits = [];
|
|
48
|
+
inits.push(modJs);
|
|
42
49
|
|
|
43
50
|
var watchAction = function() {
|
|
44
|
-
|
|
51
|
+
utils.debug('打包中');
|
|
45
52
|
// 这里进行文件更改后的操作
|
|
46
53
|
var p = fork(mergeJs, [`${[inits]}`, `${[path.join(config.webPath, '/js/init.js')]}`, compressArgs])
|
|
47
54
|
p.on('exit', code => {
|
|
48
|
-
|
|
55
|
+
utils.debug('打包完成');
|
|
49
56
|
});
|
|
50
57
|
}
|
|
51
58
|
|
|
@@ -55,24 +62,21 @@ function mergeInit() {
|
|
|
55
62
|
usePolling: true
|
|
56
63
|
});
|
|
57
64
|
watcher
|
|
58
|
-
.on('ready', () =>
|
|
65
|
+
.on('ready', () => utils.debug(`${jsPath},Initial scan complete. Ready for changes.`))
|
|
59
66
|
//.on('add', path => log(`File ${path} has been added`))
|
|
60
67
|
.on('change', path => watchAction({ event: 'change', eventPath: path }))
|
|
61
68
|
.on('unlink', path => watchAction({ event: 'remove', eventPath: path }));
|
|
62
69
|
}
|
|
63
70
|
|
|
64
|
-
var modJs = path.join(config.webPath, '/js/' + config.modName + '.js'); // jxc/js/jxc.js
|
|
65
|
-
if (!fs.existsSync(modJs)) {
|
|
66
|
-
console.error(`模块对应的资源不存在:${modJs},请将${config.modName}/js/init.js先修改为:${config.modName}/js/${config.modName}.js`);
|
|
67
|
-
}
|
|
68
|
-
inits.push(modJs);
|
|
69
71
|
watchJs(modJs);
|
|
70
72
|
|
|
71
73
|
config.childModList.forEach(childName => {
|
|
72
74
|
var childInitPath = path.join(config.webPath, childName, 'js/init.js');
|
|
73
75
|
inits.push(childInitPath);
|
|
74
|
-
utils.mkdirsSync(childInitPath);
|
|
75
|
-
|
|
76
|
+
//utils.mkdirsSync(childInitPath);
|
|
77
|
+
if (!fs.existsSync(childInitPath)) {
|
|
78
|
+
utils.error('error 监听打包的资源不存在' + childInitPath);
|
|
79
|
+
}
|
|
76
80
|
watchJs(childInitPath);
|
|
77
81
|
});
|
|
78
82
|
inits = inits.join(',');
|
|
@@ -80,12 +84,8 @@ function mergeInit() {
|
|
|
80
84
|
watchAction();// 启动就打包一次
|
|
81
85
|
}
|
|
82
86
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
utils.mkdirsSync(config.webPath);
|
|
87
|
-
|
|
88
|
-
mergeBiz(); // 如果有需要
|
|
87
|
+
utils.log('启动打包服务', config.childModList);
|
|
88
|
+
utils.mkdirsSync(config.webPath);
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
mergeBiz(); // 如果有需要
|
|
91
|
+
mergeInit(); // 如果有需要
|
package/tool/progressbar.js
CHANGED
|
@@ -7,10 +7,14 @@ function ProgressBar(description, bar_length) {
|
|
|
7
7
|
this.description = description || 'Progress'; // 命令行开头的文字信息
|
|
8
8
|
this.length = bar_length || 25; // 进度条的长度(单位:字符),默认设为 25
|
|
9
9
|
|
|
10
|
+
var that = this;
|
|
11
|
+
that.value = 0;
|
|
12
|
+
|
|
10
13
|
// 刷新进度条图案、文字的方法
|
|
11
|
-
|
|
12
|
-
if (!opts.total) opts.total =
|
|
13
|
-
|
|
14
|
+
that.render = function(opts) {
|
|
15
|
+
if (!opts.total) opts.total = this.length;
|
|
16
|
+
that.value = opts.value;
|
|
17
|
+
var percent = (opts.value / opts.total).toFixed(4); // 计算进度(子任务的 完成数 除以 总数)
|
|
14
18
|
var cell_num = Math.floor(percent * this.length); // 计算需要多少个 █ 符号来拼凑图案
|
|
15
19
|
|
|
16
20
|
// 拼接黑色条
|
|
@@ -26,14 +30,14 @@ function ProgressBar(description, bar_length) {
|
|
|
26
30
|
}
|
|
27
31
|
|
|
28
32
|
// 拼接最终文本
|
|
29
|
-
var cmdText = this.description + ': ' + (100 * percent).toFixed(2) + '% ' + cell + empty + ' ' + (opts.text || '');
|
|
33
|
+
var cmdText = this.description + ': ' + (100 * percent).toFixed(2) + '% ' + cell + empty + ' ' + (opts.text || '') + '\r\n';
|
|
30
34
|
|
|
31
35
|
// 在单行输出文本
|
|
32
36
|
slog(cmdText);
|
|
33
37
|
};
|
|
34
38
|
|
|
35
|
-
|
|
36
|
-
slog(text || '');
|
|
39
|
+
that.clear = function(text) {
|
|
40
|
+
slog((text || '') + '\r\n');
|
|
37
41
|
}
|
|
38
42
|
}
|
|
39
43
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var utils = require('
|
|
2
|
-
var config = require('
|
|
1
|
+
var utils = require('../lib/utils.js');
|
|
2
|
+
var config = require('../lib/config.js');
|
|
3
3
|
var ws = require("nodejs-websocket");
|
|
4
4
|
var path = require('path');
|
|
5
5
|
var chokidar = require('chokidar');
|
|
@@ -7,7 +7,7 @@ var fs = require('fs');
|
|
|
7
7
|
var exts = [".js", ".css", ".html", ".gspx"];
|
|
8
8
|
|
|
9
9
|
function watchWebChange() {
|
|
10
|
-
utils.
|
|
10
|
+
utils.debug("创建文件变动监听");
|
|
11
11
|
|
|
12
12
|
var watcher = chokidar.watch([config.webPath], {
|
|
13
13
|
persistent: true,
|
|
@@ -19,37 +19,36 @@ function watchWebChange() {
|
|
|
19
19
|
if (!exts.includes(ext)) return;
|
|
20
20
|
|
|
21
21
|
utils.log('文件变动,刷新浏览器');
|
|
22
|
-
console.log(args);
|
|
22
|
+
//console.log(args);
|
|
23
23
|
|
|
24
24
|
broadcast('refresh');
|
|
25
25
|
}
|
|
26
26
|
watcher
|
|
27
|
-
.on('ready', () => { utils.
|
|
27
|
+
.on('ready', () => { utils.debug('watch webPath change ready !!') })
|
|
28
28
|
.on('change', path => watchAction({ event: 'change', eventPath: path }))
|
|
29
29
|
.on('unlink', path => watchAction({ event: 'remove', eventPath: path }));
|
|
30
30
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
function createSocket() {
|
|
34
|
-
utils.log("监听浏览器");
|
|
35
34
|
socketServer = ws.createServer(function(conn) {
|
|
36
35
|
conn.on("text", function(str) {
|
|
37
|
-
utils.
|
|
36
|
+
utils.debug("收到的信息为:" + str);
|
|
38
37
|
});
|
|
39
38
|
conn.on('connect', function(code, reason) {
|
|
40
39
|
utils.log('connect连接成功');
|
|
41
40
|
broadcast('ws connect success');
|
|
42
41
|
});
|
|
43
42
|
conn.on("close", function(code, reason) {
|
|
44
|
-
utils.
|
|
43
|
+
utils.debug("connect关闭");
|
|
45
44
|
});
|
|
46
45
|
conn.on("error", function(code, reason) {
|
|
47
|
-
|
|
46
|
+
|
|
48
47
|
});
|
|
49
48
|
}).listen(config.socketPort);
|
|
50
49
|
|
|
51
50
|
socketServer.on('connection', function(code, reason) {
|
|
52
|
-
utils.
|
|
51
|
+
utils.debug('connection连接成功');
|
|
53
52
|
broadcast('ws connection success');
|
|
54
53
|
});
|
|
55
54
|
}
|
package/tool/upgrade.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
var config = require('../lib/config.js');
|
|
2
2
|
var path = require('path');
|
|
3
3
|
var fs = require('fs');
|
|
4
|
-
var request = require('request');
|
|
5
4
|
var requestSync = require('sync-request');
|
|
6
5
|
var utils = require('../lib/utils.js');
|
|
7
6
|
var axios = require('axios');
|
|
@@ -10,26 +9,18 @@ var StreamZip = require('node-stream-zip');
|
|
|
10
9
|
|
|
11
10
|
class Upgrade {
|
|
12
11
|
constructor() {
|
|
13
|
-
this.
|
|
14
|
-
this.TempPath = path.join(__dirname, 'temp');
|
|
15
|
-
this.ImageCache = path.join(this.TempPath, "_img");
|
|
16
|
-
this.JsonFile = path.join(this.TempPath, "craba.json");
|
|
17
|
-
this.SaveFilePath = path.join(this.TempPath, "_CrabaUpdate");
|
|
18
|
-
this.CachePath = path.join(this.SaveFilePath, "_unzip");
|
|
19
|
-
this.WebPath = path.join(this.SaveFilePath, "_www");
|
|
20
|
-
this.ServerPath = path.join(this.SaveFilePath, "_server");
|
|
21
|
-
this.CacheFileName = path.join(this.SaveFilePath, "craba.zip");
|
|
12
|
+
this.serverFolder = 'crabatool';
|
|
22
13
|
}
|
|
23
14
|
|
|
24
15
|
getVersionUrl() {
|
|
25
|
-
var url =
|
|
16
|
+
var url = config.Host;
|
|
26
17
|
if (!url.endsWith("/")) url += "/";
|
|
27
18
|
url += "setup/crabaConfig.json";
|
|
28
19
|
return url;
|
|
29
20
|
}
|
|
30
21
|
|
|
31
22
|
getCrabaFileUrl() {
|
|
32
|
-
var url =
|
|
23
|
+
var url = config.Host;
|
|
33
24
|
if (!url.endsWith("/")) url += "/";
|
|
34
25
|
url += "setup/craba.zip";
|
|
35
26
|
return url;
|
|
@@ -55,8 +46,8 @@ class Upgrade {
|
|
|
55
46
|
}
|
|
56
47
|
|
|
57
48
|
getNewVersion() {
|
|
58
|
-
var version = "error";
|
|
59
|
-
var vDate = "error";
|
|
49
|
+
var version = "network error";
|
|
50
|
+
var vDate = "network error";
|
|
60
51
|
|
|
61
52
|
try {
|
|
62
53
|
var res = requestSync("GET", this.getVersionUrl());
|
|
@@ -71,71 +62,162 @@ class Upgrade {
|
|
|
71
62
|
}
|
|
72
63
|
|
|
73
64
|
updateCraba(cb) {
|
|
65
|
+
utils.cleardirsSync(config.SaveFilePath);
|
|
66
|
+
|
|
67
|
+
this.cb = cb;
|
|
68
|
+
this.projectPath = config.webPath;
|
|
74
69
|
var localData = this.getLocalVersion();
|
|
75
70
|
var newData = this.getNewVersion();
|
|
76
71
|
if (localData.version == newData.version) {
|
|
77
|
-
console.log('
|
|
72
|
+
console.log(':::本地已经是最新版本,不需要更新:::');
|
|
78
73
|
if (cb) cb();
|
|
79
74
|
return;
|
|
80
75
|
}
|
|
81
76
|
|
|
82
|
-
this.
|
|
83
|
-
this.
|
|
77
|
+
this.done = this._updateDone;
|
|
78
|
+
this.downloadCraba();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
createProject(options) {
|
|
82
|
+
|
|
83
|
+
var projectPath = "";
|
|
84
|
+
if (options.type == 1) {
|
|
85
|
+
projectPath = path.join(options.projectPath, 'www');
|
|
86
|
+
} else if (options.type == 2) {
|
|
87
|
+
projectPath = `${options.modName}/web/src/main/resources/static/${options.modName}`;
|
|
88
|
+
projectPath = path.join(options.projectPath, projectPath);
|
|
89
|
+
projectPath = projectPath;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.done = this._createDone;
|
|
93
|
+
this.options = options;
|
|
94
|
+
this.projectPath = projectPath;
|
|
95
|
+
|
|
96
|
+
this.downloadCraba()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
_updateDone() {
|
|
100
|
+
this.pb.clear('完成');
|
|
101
|
+
|
|
102
|
+
if (this.cb) {
|
|
103
|
+
this.cb();
|
|
104
|
+
delete this.cb;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 可能文件占用中
|
|
108
|
+
setTimeout(function() {
|
|
109
|
+
//utils.cleardirsSync(config.SaveFilePath);
|
|
110
|
+
}, 1000);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
_createDone() {
|
|
114
|
+
// 拷贝server文件
|
|
115
|
+
utils.copyFiles(config.ServerPath, path.join(this.options.projectPath, this.serverFolder), null, utils.createDelegate(this, function(fileName) {
|
|
116
|
+
this.pb.render({ value: ++this.pb.value, text: fileName });
|
|
117
|
+
}));
|
|
118
|
+
|
|
119
|
+
// 拷贝www 案例文件
|
|
120
|
+
if (this.options.type == 1) { // 普通项目,拷贝demo页面
|
|
121
|
+
utils.copyFiles(config.WebPath, this.projectPath, ['ngp'], utils.createDelegate(this, function(fileName) {
|
|
122
|
+
this.pb.render({ value: ++this.pb.value, text: fileName });
|
|
123
|
+
}));
|
|
124
|
+
} else {
|
|
125
|
+
var that = this;
|
|
126
|
+
utils.copyFiles(path.join(config.WebPath, 'ngp'), this.projectPath, null, utils.createDelegate(this, function(fileName, bytes) {
|
|
127
|
+
this.pb.render({ value: ++this.pb.value, text: fileName });
|
|
128
|
+
return bytes.toString().replaceAll('${modName}', that.options.modName);
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 重写craba.js的配置
|
|
133
|
+
this._rewriteNode(); // 创建node启动脚本
|
|
134
|
+
|
|
135
|
+
this._updateDone();
|
|
136
|
+
|
|
137
|
+
console.log('项目创建完成,请切换到新项目下继续操作。' + path.join(this.options.projectPath, this.serverFolder));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
_rewriteNode() {
|
|
141
|
+
/*
|
|
142
|
+
webPath: "${this.projectPath}",
|
|
143
|
+
|
|
144
|
+
//【必填】
|
|
145
|
+
// 告诉工具当前项目前端服务器端口地址
|
|
146
|
+
port: ${10000 + parseInt(Math.random() * 1000)},
|
|
147
|
+
*/
|
|
148
|
+
var nodeJs = path.join(path.join(this.options.projectPath, this.serverFolder), 'craba.js');
|
|
149
|
+
var content = fs.readFileSync(nodeJs).toString();
|
|
150
|
+
content = content.replace('${webPath}', this.projectPath.replaceAll('\\', '/'));
|
|
151
|
+
content = content.replace('${port}', (10000 + parseInt(Math.random() * 1000)));
|
|
152
|
+
content = content.replace('${modName}', this.options.modName ? this.options.modName : '');
|
|
153
|
+
fs.writeFileSync(nodeJs, content);
|
|
154
|
+
}
|
|
84
155
|
|
|
85
|
-
|
|
156
|
+
downloadCraba() {
|
|
157
|
+
this.pb = new ProgressBar('下载进度', 50);
|
|
158
|
+
this.pb.render({ value: 10, text: '下载资源' });
|
|
159
|
+
utils.mkdirsSync(config.SaveFilePath); // 创建缓存目录
|
|
160
|
+
this.downloadFile(this.getCrabaFileUrl(), config.CacheFileName, utils.createDelegate(this, this._doCrabaLoaded));
|
|
86
161
|
}
|
|
87
162
|
|
|
88
163
|
_doCrabaLoaded(filePath) {
|
|
89
164
|
if (!fs.existsSync(filePath)) {// 检查文件
|
|
90
|
-
console.log("
|
|
165
|
+
console.log(":::服务不可用,拉取文件失败,请联系平台管理员:::");
|
|
91
166
|
return;
|
|
92
167
|
}
|
|
93
|
-
this.pb.render({ completed: 50, text: '下载完成' });
|
|
94
168
|
|
|
169
|
+
this.pb.render({ value: 50, text: '下载完成' });
|
|
170
|
+
var that = this;
|
|
95
171
|
//console.log(filePath);
|
|
96
|
-
var zip = new StreamZip({ file: filePath });
|
|
172
|
+
var zip = new StreamZip({ file: filePath, storeEntries: true, skipEntryNameValidation: true });
|
|
97
173
|
zip.on('ready', () => {
|
|
98
|
-
that.pb.render({
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
174
|
+
that.pb.render({ value: 70, text: '解压并拷贝文件' });
|
|
175
|
+
|
|
176
|
+
utils.mkdirsSync(config.CachePath);
|
|
177
|
+
|
|
178
|
+
zip.extract(null, config.CachePath, (err, count) => {
|
|
102
179
|
zip.close();
|
|
103
|
-
|
|
180
|
+
|
|
181
|
+
that._copyFiles();
|
|
104
182
|
});
|
|
105
183
|
});
|
|
106
184
|
}
|
|
107
185
|
|
|
108
|
-
|
|
186
|
+
_copyFiles() {
|
|
187
|
+
var dest = this.projectPath;
|
|
188
|
+
var source = config.CachePath;
|
|
189
|
+
var ignores = ["server", "www"];
|
|
190
|
+
|
|
191
|
+
utils.copyFiles(source, dest, ignores, utils.createDelegate(this, function(fileName) {
|
|
192
|
+
this.pb.render({ value: ++this.pb.value, text: fileName });
|
|
193
|
+
}));
|
|
109
194
|
|
|
195
|
+
if (this.done) {
|
|
196
|
+
this.done();
|
|
197
|
+
}
|
|
110
198
|
}
|
|
111
199
|
|
|
112
200
|
downloadFile(uri, filePath, callback) {
|
|
113
201
|
if (!uri || !filePath) throw new Error('uri或filePath不能为空');
|
|
114
202
|
|
|
115
203
|
//console.log('下载craba文件');
|
|
116
|
-
|
|
204
|
+
var that = this;
|
|
117
205
|
// 获取远端图片
|
|
118
206
|
axios({
|
|
119
207
|
method: 'get',
|
|
120
208
|
url: this.getCrabaFileUrl(),
|
|
121
209
|
responseType: 'stream'
|
|
122
210
|
}).then(function(response) {
|
|
123
|
-
that.pb.render({ completed: 40, text: '下载资源' });
|
|
124
211
|
response.data
|
|
125
|
-
.pipe(fs.createWriteStream(
|
|
212
|
+
.pipe(fs.createWriteStream(config.CacheFileName))
|
|
126
213
|
.on('close', () => {
|
|
127
214
|
if (callback) callback(filePath);
|
|
128
215
|
});
|
|
129
216
|
}).catch(function(error) {
|
|
130
|
-
that.pb.clear('[内网才能访问],出错了:'
|
|
217
|
+
that.pb.clear('[内网才能访问],出错了:');
|
|
218
|
+
console.log(error);
|
|
131
219
|
});
|
|
132
220
|
}
|
|
133
|
-
|
|
134
|
-
_deleteCache() {
|
|
135
|
-
this.pb.render({ completed: 10, text: '清理缓存' });
|
|
136
|
-
utils.cleardirsSync(this.SaveFilePath);
|
|
137
|
-
utils.mkdirsSync(this.SaveFilePath); // 创建缓存目录
|
|
138
|
-
this.pb.render({ completed: 20, text: '清理完成' });
|
|
139
|
-
}
|
|
140
221
|
}
|
|
141
|
-
|
|
222
|
+
|
|
223
|
+
module.exports = new Upgrade();
|