Commit 50a420ef72457ab3ac7b6cb93fe0866154423ffc
1 parent
b7c25011
自动合并压缩打包
Showing
6 changed files
with
373 additions
and
2 deletions
src/main/java/com/bsth/StartCommand.java
| @@ -3,6 +3,7 @@ package com.bsth; | @@ -3,6 +3,7 @@ package com.bsth; | ||
| 3 | 3 | ||
| 4 | import java.util.concurrent.Executors; | 4 | import java.util.concurrent.Executors; |
| 5 | import java.util.concurrent.ScheduledExecutorService; | 5 | import java.util.concurrent.ScheduledExecutorService; |
| 6 | +import java.util.concurrent.TimeUnit; | ||
| 6 | 7 | ||
| 7 | import org.slf4j.Logger; | 8 | import org.slf4j.Logger; |
| 8 | import org.slf4j.LoggerFactory; | 9 | import org.slf4j.LoggerFactory; |
| @@ -39,10 +40,10 @@ public class StartCommand implements CommandLineRunner{ | @@ -39,10 +40,10 @@ public class StartCommand implements CommandLineRunner{ | ||
| 39 | invocationSecurityMetadataSourceService.loadResourceDefine(); | 40 | invocationSecurityMetadataSourceService.loadResourceDefine(); |
| 40 | 41 | ||
| 41 | /** | 42 | /** |
| 42 | - * 定时GPS实时数据更新线程,每次执行完成4秒后开始下一次 | 43 | + * GPS实时数据更新线程,每次执行完成4秒后开始下一次 |
| 43 | * 如果抛出异常,后续执行将被取消 | 44 | * 如果抛出异常,后续执行将被取消 |
| 44 | */ | 45 | */ |
| 45 | - //scheduler.scheduleWithFixedDelay(gpsRefreshThread, 0, 4, TimeUnit.SECONDS); | 46 | + scheduler.scheduleWithFixedDelay(gpsRefreshThread, 0, 4, TimeUnit.SECONDS); |
| 46 | } catch (Exception e) { | 47 | } catch (Exception e) { |
| 47 | e.printStackTrace(); | 48 | e.printStackTrace(); |
| 48 | } | 49 | } |
src/main/resources/fatso/exclude.txt
0 → 100644
src/main/resources/fatso/minifier.js
0 → 100644
| 1 | +/** | ||
| 2 | + * @author PanZhao | ||
| 3 | + * @date 2016年3月17日 下午12:44:06 | ||
| 4 | + */ | ||
| 5 | +var fs = require('fs'); | ||
| 6 | +var UglifyJS = require("uglify-js"); | ||
| 7 | + | ||
| 8 | +var minifier = { | ||
| 9 | + | ||
| 10 | + mergeAndMini: function(fileArray,scriptString, root, isIndex){ | ||
| 11 | + var len = fileArray.length; | ||
| 12 | + for(var i = 0; i < len; i ++){ | ||
| 13 | + fileArray[i] = root + fileArray[i].split('/').join('\\'); | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + var result, indoorRs; | ||
| 17 | + | ||
| 18 | + try { | ||
| 19 | + if(fileArray && len > 0) | ||
| 20 | + result = UglifyJS.minify(fileArray); | ||
| 21 | + | ||
| 22 | + if(scriptString) | ||
| 23 | + indoorRs = UglifyJS.minify(scriptString, {fromString: true}); | ||
| 24 | + } catch (e) { | ||
| 25 | + console.log(e); | ||
| 26 | + process.exit(); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + return {outside: result?result.code:'', inside: indoorRs?indoorRs.code:''}; | ||
| 30 | + }, | ||
| 31 | + mini: function(file){ | ||
| 32 | + return UglifyJS.minify(file).code; | ||
| 33 | + } | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +module.exports = minifier; | ||
| 0 | \ No newline at end of file | 37 | \ No newline at end of file |
src/main/resources/fatso/package.json
0 → 100644
| 1 | +{ | ||
| 2 | + "name": "fatso", | ||
| 3 | + "version": "1.0.0", | ||
| 4 | + "description": "子页面js检查、合并、压缩等处理", | ||
| 5 | + "main": "start.js", | ||
| 6 | + "scripts": { | ||
| 7 | + "test": "echo \"Error: no test specified\" && exit 1" | ||
| 8 | + }, | ||
| 9 | + "author": "panzhaov5", | ||
| 10 | + "license": "ISC", | ||
| 11 | + "dependencies": { | ||
| 12 | + "cheerio": "^0.20.0", | ||
| 13 | + "colors": "^1.1.2", | ||
| 14 | + "eventproxy": "^0.3.4", | ||
| 15 | + "uglify-js": "^2.6.2" | ||
| 16 | + } | ||
| 17 | +} |
src/main/resources/fatso/parse.js
0 → 100644
| 1 | +/** | ||
| 2 | + * @author PanZhao | ||
| 3 | + * @date 2016年3月17日 下午12:44:06 | ||
| 4 | + */ | ||
| 5 | +var fs = require('fs') | ||
| 6 | + ,cheerio = require('cheerio') | ||
| 7 | + ,minifier = require('./minifier') | ||
| 8 | + ,crypto = require("crypto"); | ||
| 9 | + | ||
| 10 | +var parse = { | ||
| 11 | + html: function(filePath, root){ | ||
| 12 | + var data = fs.readFileSync(filePath, 'utf-8') | ||
| 13 | + ,$ = cheerio.load(data) | ||
| 14 | + ,scriptString = ''; | ||
| 15 | + | ||
| 16 | + var script = $('script') | ||
| 17 | + ,linkArray = []; | ||
| 18 | + | ||
| 19 | + for(var i = 0, s; s = script[i++];){ | ||
| 20 | + //模板 | ||
| 21 | + if($(s).attr('type') == 'text/html') | ||
| 22 | + continue; | ||
| 23 | + //不参与的 | ||
| 24 | + if($(s).data('exclude') == 1) | ||
| 25 | + continue; | ||
| 26 | + | ||
| 27 | + //外部引用JS | ||
| 28 | + if($(s).attr('src') != null ){ | ||
| 29 | + var src = $(s).attr('src'); | ||
| 30 | + //独立自主第三方 | ||
| 31 | + if($(s).data('autocephaly') == 1){ | ||
| 32 | + var mini = minifier.mini(root + src) | ||
| 33 | + ,code = md5(mini); | ||
| 34 | + | ||
| 35 | + var newPath = src.substr(0, src.lastIndexOf('/')) + '/' +code + '.js'; | ||
| 36 | + //重命名文件 | ||
| 37 | + fs.renameSync(root + src, root + newPath); | ||
| 38 | + write(root + newPath, mini); | ||
| 39 | + | ||
| 40 | + $(s).attr('src', newPath); | ||
| 41 | + continue; | ||
| 42 | + }else | ||
| 43 | + linkArray.push(src); | ||
| 44 | + } | ||
| 45 | + //内部JS | ||
| 46 | + else{ | ||
| 47 | + scriptString = $(s).html(); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + $(s).remove(); | ||
| 51 | + } | ||
| 52 | + return {jsArray: linkArray, $: $, scriptString: scriptString}; | ||
| 53 | + } | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | + | ||
| 57 | +function md5(text){ | ||
| 58 | + return crypto.createHash("md5").update(text).digest("hex"); | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +function write(file, text){ | ||
| 62 | + fs.writeFile(file, text, function (err) { | ||
| 63 | + if (err){ | ||
| 64 | + console.log(err.toString().red); | ||
| 65 | + process.exit(); | ||
| 66 | + } | ||
| 67 | + console.log(file.green); | ||
| 68 | + }); | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +module.exports = parse; | ||
| 0 | \ No newline at end of file | 72 | \ No newline at end of file |
src/main/resources/fatso/start.js
0 → 100644
| 1 | +/** | ||
| 2 | + * @author PanZhao | ||
| 3 | + * @date 2016年3月17日 下午12:44:06 | ||
| 4 | + */ | ||
| 5 | +var fs = require('fs') | ||
| 6 | + ,colors = require('colors') | ||
| 7 | + ,child_process = require('child_process') | ||
| 8 | + ,EventProxy = require('eventproxy') | ||
| 9 | + ,parse = require('./parse') | ||
| 10 | + ,minifier = require('./minifier') | ||
| 11 | + ,crypto = require("crypto"); | ||
| 12 | + | ||
| 13 | +//不参与的目录 | ||
| 14 | +var excludes = ['scheduleApp'] | ||
| 15 | + ,ep = new EventProxy() | ||
| 16 | + ,pName = 'bsth_control' | ||
| 17 | + ,path = process.cwd() | ||
| 18 | + //根目录 | ||
| 19 | + ,root = path.substr(0, path.indexOf('\\src\\main')) | ||
| 20 | + ,workspace = root.substr(0, root.indexOf('\\' + pName)) | ||
| 21 | + //临时目录 | ||
| 22 | + ,dest = (workspace + '\\' + pName+'@fatso_copy').replace(/\//g,'\\') | ||
| 23 | + ,_static = '\\src\\main\\resources\\static' | ||
| 24 | + ,_pages = dest + _static + '\\pages'; | ||
| 25 | + | ||
| 26 | + | ||
| 27 | +//创建临时目录 | ||
| 28 | +fs.mkdir(dest, function(e){ | ||
| 29 | + if(e) | ||
| 30 | + logError('创建临时目录出错,请检查目录 ' + dest + '是否存在'); | ||
| 31 | + | ||
| 32 | + setTimeout(function(){ | ||
| 33 | + ep.emit('mvn-clean'); | ||
| 34 | + }, 500); | ||
| 35 | +}); | ||
| 36 | +//子进程 | ||
| 37 | +var cProcess; | ||
| 38 | + | ||
| 39 | +ep.tail('mvn-clean',function(){ | ||
| 40 | + //清理target | ||
| 41 | + logInfo('mvn clean...'); | ||
| 42 | + cProcess = child_process.exec("mvn clean",{cwd: workspace + '\\' + pName},function(error){ | ||
| 43 | + if(error) | ||
| 44 | + logError(error); | ||
| 45 | + | ||
| 46 | + logSuccess('mvn clean success'); | ||
| 47 | + | ||
| 48 | + ep.emit('copy-project'); | ||
| 49 | + }); | ||
| 50 | + output(cProcess); | ||
| 51 | +}); | ||
| 52 | + | ||
| 53 | +//复制项目副本 | ||
| 54 | +ep.tail('copy-project',function(){ | ||
| 55 | + logInfo('copy project...'); | ||
| 56 | + var xcopyCom = 'XCOPY '+ root.replace(/\//g,'\\') + ' ' + dest +' /e /exclude:'+path+'\\exclude.txt'; | ||
| 57 | + cProcess = child_process.exec(xcopyCom,{maxBuffer: 5000*1024},function(error){ | ||
| 58 | + if(error) | ||
| 59 | + logError(error); | ||
| 60 | + | ||
| 61 | + logSuccess('copy project success'); | ||
| 62 | + | ||
| 63 | + ep.emit('check-js'); | ||
| 64 | + }); | ||
| 65 | + //output(cProcess); | ||
| 66 | +}); | ||
| 67 | + | ||
| 68 | +//检查JS | ||
| 69 | +ep.tail('check-js', function(){ | ||
| 70 | + ep.emit('minifier-js'); | ||
| 71 | +}); | ||
| 72 | + | ||
| 73 | +//合并压缩JS | ||
| 74 | +ep.tail('minifier-js', function(){ | ||
| 75 | + logInfo('handle index.html...'); | ||
| 76 | + | ||
| 77 | + //先处理首页 | ||
| 78 | + ep.emit('handle-index', function(){ | ||
| 79 | + //递归处理片段 | ||
| 80 | + walk(dest + _static + '\\pages', function(item){ | ||
| 81 | + ep.emit('handle-fragment', item); | ||
| 82 | + }, | ||
| 83 | + function(){ | ||
| 84 | + ep.emit('package-jar'); | ||
| 85 | + }); | ||
| 86 | + }); | ||
| 87 | +}); | ||
| 88 | + | ||
| 89 | +//打包 | ||
| 90 | +ep.tail('package-jar', function(file){ | ||
| 91 | + var packageCom = 'mvn clean package'; | ||
| 92 | + cProcess = child_process.exec(packageCom,{maxBuffer: 5000*1024, cwd: dest},function(error){ | ||
| 93 | + if(error) | ||
| 94 | + logError(error); | ||
| 95 | + | ||
| 96 | + logSuccess('mvn package success'); | ||
| 97 | + | ||
| 98 | + console.log(('成功打包在 ' + dest + '\\target 目录下').cyan); | ||
| 99 | + }); | ||
| 100 | + | ||
| 101 | + output(cProcess); | ||
| 102 | +}); | ||
| 103 | + | ||
| 104 | +//处理片段 | ||
| 105 | +ep.tail('handle-fragment', function(file){ | ||
| 106 | + //要排除的文件 | ||
| 107 | + for(var i = 0, ex; ex = excludes[i++];){ | ||
| 108 | + if(file.indexOf(_pages + '\\' + ex) != -1) | ||
| 109 | + return false; | ||
| 110 | + } | ||
| 111 | + handleJavascript(file, function(mini, $){ | ||
| 112 | + var jsMini; | ||
| 113 | + if(mini.inside) | ||
| 114 | + jsMini = '(function(){' + mini.inside + '\n' + mini.outside + '})();'; | ||
| 115 | + else | ||
| 116 | + jsMini = '(function(){' + mini.outside + '})();'; | ||
| 117 | + | ||
| 118 | + write(file, $.html() + '<script>' + jsMini + '</script>'); | ||
| 119 | + }); | ||
| 120 | + | ||
| 121 | +}); | ||
| 122 | + | ||
| 123 | +//处理首页 | ||
| 124 | +ep.tail('handle-index', function(cb){ | ||
| 125 | + var index = dest + _static + '\\index.html'; | ||
| 126 | + handleJavascript(index, function(mini, $){ | ||
| 127 | + var jsMiniText = mini.inside + mini.outside; | ||
| 128 | + | ||
| 129 | + var code = md5(jsMiniText); | ||
| 130 | + fs.open( dest + _static + '\\assets\\js\\' + code + '.js', 'a', function(err, fd){ | ||
| 131 | + if(err) | ||
| 132 | + logError(err); | ||
| 133 | + | ||
| 134 | + fs.write(fd,jsMiniText, function(){ | ||
| 135 | + var ss = $('script'); | ||
| 136 | + | ||
| 137 | + $(ss[ss.length - 1]).before('<script src="/assets/js/'+code+'.js"></script>\n'); | ||
| 138 | + for(var i = 0, s; s = ss[i++];){ | ||
| 139 | + $(s).removeAttr('data-exclude') | ||
| 140 | + .removeAttr('flag') | ||
| 141 | + .removeAttr('data-autocephaly'); | ||
| 142 | + } | ||
| 143 | + write(index, $.html()); | ||
| 144 | + | ||
| 145 | + cb && cb(); | ||
| 146 | + }); | ||
| 147 | + }); | ||
| 148 | + }); | ||
| 149 | +}); | ||
| 150 | + | ||
| 151 | + | ||
| 152 | +function handleJavascript(item, cb){ | ||
| 153 | + //解析页面 | ||
| 154 | + var htmlResult = parse.html(item, dest + _static) | ||
| 155 | + ,jsArray = htmlResult.jsArray | ||
| 156 | + ,$ = htmlResult.$ | ||
| 157 | + ,scrStr = htmlResult.scriptString; | ||
| 158 | + | ||
| 159 | + //合并压缩 | ||
| 160 | + var mini = minifier.mergeAndMini(jsArray, scrStr, dest + _static); | ||
| 161 | + | ||
| 162 | + cb && cb(mini, $); | ||
| 163 | +} | ||
| 164 | + | ||
| 165 | +function removeJsLink(s, e){ | ||
| 166 | + var newArray = [], flag; | ||
| 167 | + | ||
| 168 | + for(var i = 0, si; si = s[i++];){ | ||
| 169 | + flag = 0; | ||
| 170 | + for(var j = 0,ei; ei = e[j++];){ | ||
| 171 | + if(si.indexOf(ei) != -1){ | ||
| 172 | + flag = -1; | ||
| 173 | + break; | ||
| 174 | + } | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + if(flag == 0){ | ||
| 178 | + newArray.push(si); | ||
| 179 | + } | ||
| 180 | + } | ||
| 181 | + return newArray; | ||
| 182 | +} | ||
| 183 | + | ||
| 184 | +function write(file, text){ | ||
| 185 | + fs.writeFile(file, text, function (err) { | ||
| 186 | + if (err){ | ||
| 187 | + console.log(err.toString().red); | ||
| 188 | + process.exit(); | ||
| 189 | + } | ||
| 190 | + console.log(file.green); | ||
| 191 | + }); | ||
| 192 | +} | ||
| 193 | + | ||
| 194 | +function md5(text){ | ||
| 195 | + return crypto.createHash("md5").update(text).digest("hex"); | ||
| 196 | +} | ||
| 197 | + | ||
| 198 | + | ||
| 199 | +function walk(path ,handleFile, over) { | ||
| 200 | + fs.readdir(path, function(err, files) { | ||
| 201 | + if (err) { | ||
| 202 | + console.log('read dir error'.red); | ||
| 203 | + } else { | ||
| 204 | + files.forEach(function(item) { | ||
| 205 | + var tmpPath = path + '\\' + item; | ||
| 206 | + fs.stat(tmpPath, function(err1, stats) { | ||
| 207 | + if (err1) { | ||
| 208 | + console.log('stat error'); | ||
| 209 | + } else { | ||
| 210 | + if (stats.isDirectory()) { | ||
| 211 | + walk(tmpPath, handleFile); | ||
| 212 | + } else if(item.indexOf('.html') != -1){ | ||
| 213 | + handleFile(tmpPath); | ||
| 214 | + } | ||
| 215 | + } | ||
| 216 | + }) | ||
| 217 | + }); | ||
| 218 | + | ||
| 219 | + over && over(); | ||
| 220 | + } | ||
| 221 | + }); | ||
| 222 | +} | ||
| 223 | + | ||
| 224 | +function logInfo(t){ | ||
| 225 | + console.log(t); | ||
| 226 | +} | ||
| 227 | + | ||
| 228 | +function logSuccess(t){ | ||
| 229 | + console.log(t.green); | ||
| 230 | +} | ||
| 231 | + | ||
| 232 | +function logError(e){ | ||
| 233 | + console.log(e.toString().red); | ||
| 234 | + process.exit(); | ||
| 235 | +} | ||
| 236 | + | ||
| 237 | +function output(cProcess){ | ||
| 238 | + //标准输出 | ||
| 239 | + cProcess.stdout.on('data', function (s) { | ||
| 240 | + console.log(s); | ||
| 241 | + }); | ||
| 242 | +} | ||
| 0 | \ No newline at end of file | 243 | \ No newline at end of file |