UNPKG

fsk-cmp

Version:

fsk-cmp for liepinfe

128 lines (124 loc) 4.65 kB
/** * Created by panyanming on 15/3/4. * cmp is fsk sub module only for liepinfe */ var fs = require('fs'), cwd = process.cwd(), isWin = /win\d{2}/i.test(process.platform), tplextname = '.tpl', tplextnamereg =/\.tpl$/, join = require('path').join, extname = require('path').extname, tplcmp = require(join(__dirname,'tplcompile.js')), colors = require('colors'), readdirSync = require('fsk-readdir').readdirSync, execseajs = require('fsk-seajs').execseajs, exec = require('child_process').exec, argv = require('optimist') .alias('seajs','seajs')//开启seajs支持 true or false .alias('less','less')//开始less支持 true or false .alias('tpl','tpl')//开始tpl支持 true or false .alias('watch', 'watch')//监听tpl文件改变后编译 .argv; //var absreg = /(core|dev)\/([a-z]+)?\/?(pc|h5)\/(?:trunk|branches\/[^\/]+)(\/[^\s]+)/i; var concatUrl = '//concat.lietou-static.com'; var absreg = /(\/(?:core|dev)\/)([a-z]+\/)?(pc|h5)\/(?:trunk|branches\/[^\/]+)(\/[^\s]+)/i; if(argv.watch){ var fileArr=[],timer='',cmpfile = ''; if(!isWin){ fs.watch(cwd,{ persistent: true, recursive: true},function(event,filename){ if (filename && tplextnamereg.test(filename)) { fileArr.push(join(cwd,filename)); } if(fileArr.length){ clearTimeout(timer); timer = setTimeout(function(){ fileArr = uniqueFileArray(fileArr); while(cmpfile=fileArr.pop()) { cmptplfile(cmpfile); } },1500); } }); }else{ var files = readdirSync(cwd,tplextname,'',true); files.forEach(function(val){ fs.watchFile(val,function(curr, prev){ cmptplfile(val); }); }); } }else{ if(argv.seajs){ execseajs({ parseId : function(filepath){ //将windows下的反斜线转成斜线 return absolutePath(filepath.replace(/\\/g, "/")); }, dir : cwd, write : true }); } if(argv.less){ var lessfiles = readdirSync(cwd,'.less','',true); lessfiles.forEach(function(file){ var commond = "lessc "+file+" "+file.replace(/\.less$/,'.css'); exec(commond,function(err,std){ if(err){ console.log(('Less::'+file+' error!').cyan); }else{ console.log(('Less::'+file+' success!').green); } }); }); } if(!argv.seajs && !argv.less){ argv.tpl = true; } if(argv.tpl){ var files = readdirSync(cwd,tplextname,'',true); files.forEach(function(val){ cmptplfile(val); }); } } function absolutePath(file){ var execret = absreg.exec(file); console.log(execret); //return execret && prefixdict[(execret[2]||execret[1])+'-'+execret[3]] ? prefixdict[(execret[2]||execret[1])+'-'+execret[3]]+execret[4] : (prefixdict[file] || '//'+(execret[2]||execret[1])+'.'+execret[3]+'.lietou-static.com'+execret[4]); return execret && (concatUrl+ execret[1] + (execret[2]||'') + execret[3] + execret[4]); } function cmptplfile(file){ var tpl = fs.readFileSync(file),html,pathname,tplpath; if(tpl){ tpl = tpl.toString().trim(); if(tpl){ if(!/\/(v2|v3)\//.test(file)){ html = tplcmp.precompile(tpl); pathname = file.replace(/\\/g,'/'); //console.log(absolutePath(pathname)); //return false; //tplpath = pathname.replace(tplextnamereg,'.js'); //tplpath = absolutePath(pathname).replace(tplextnamereg,'.js'); html = tplcmp.templete('', html); writefile(file.replace(tplextnamereg,'.js'),html,''); } } }else{ console.log(('fsk-cmp:: 读取文件出错 >>>>>>> '+file).red); } } function writefile(file,data,tplpath){ fs.writeFile(file,data,function(err){ var status = '',outcolor = ''; if(!err){ status = 'sucess', outcolor = 'green'; }else{ status = 'error', outcolor = 'red'; } console.log(('fsk-cmp:: ' + (tplpath||file) + ' >>>>>>> '+status)[outcolor]); }); } function uniqueFileArray(arr){ return arr.length ? ("\x0f"+ arr.sort().join("\x0f\x0f") +"\x0f").replace(/(\x0f[^\x0f]+\x0f)\1+/g,'$1').replace(/^\x0f|\x0f$/g,'').split("\x0f\x0f") : arr; }