UNPKG

4.13 kBJavaScriptView Raw
1"use strict";exports.__esModule=!0;var tslib_1=require("tslib"),LarkLogger_1=require("@kemao/lark/Server/Log/LarkLogger/LarkLogger"),FileSystemWatch_1=require("@kemao/package-tools/FileSystemWatch/FileSystemWatch"),FileSystemWatchDto_1=require("@kemao/package-tools/FileSystemWatch/FileSystemWatchDto"),ejs=require("ejs"),fs=require("fs-extra"),html_minifier_1=require("html-minifier"),Path=require("path"),uuid=require("uuid"),BrowserBase_1=require("../Core/BrowserBase"),LarkDate_1=require("../Core/LarkDate"),logger=new LarkLogger_1.LarkLogger("BrowserAsset"),BrowserAsset=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.packageTools=null,t.versionType="none",t.version=null,t.outDir="build/Browser",t.ejsData={version:null,config:null,uuid:uuid.v4(),buildDate:LarkDate_1.LarkDate.format(new Date,"yyyymmddhhMMssuuu")},t.bootscriptFileList=[],t.watchList=[/\.bootstrap\.ts?/,/\.test.ts?/,/\.ejs/,/\.dist\./],t.rootDir="./",t.sourceDir="src/Browser",t.watch=!1,t}return tslib_1.__extends(t,e),t.prototype.setEjsData=function(t){this.ejsData.config=t},t.prototype.setWatch=function(t){this.watch=t},t.prototype.setSourceDir=function(t){this.sourceDir=t},t.prototype.setVersionType=function(t){this.versionType=t},t.prototype.getVersionOutDir=function(){return""===this.version.getVersion()?this.outDir:this.outDir+"/"+this.version.getVersion()},t.prototype.setOutDir=function(t){this.outDir=t},t.prototype.isWatch=function(){return this.watch},t.prototype.scan=function(){return tslib_1.__awaiter(this,void 0,void 0,function(){var e,r=this;return tslib_1.__generator(this,function(t){switch(t.label){case 0:return(e=new FileSystemWatch_1.FileSystemWatch).setRootDir(this.rootDir+"/"+this.sourceDir),e.setWatch(this.watch),e.setWatchList(this.watchList),e.addEventListener("data",function(c){return tslib_1.__awaiter(r,void 0,void 0,function(){var e,r,s,i,o,a,n,u;return tslib_1.__generator(this,function(t){return logger.log("fileSystemWatch#data",c.filename),e=c.filename,r=Path.resolve(this.rootDir+"/"+this.sourceDir),s=c.filename.replace(r,""),i=Path.resolve(this.rootDir+"/"+this.outDir+s),c.mode!==FileSystemWatchDto_1.E_FILE_MODE.FOUND&&c.mode!==FileSystemWatchDto_1.E_FILE_MODE.RENAME&&c.mode!==FileSystemWatchDto_1.E_FILE_MODE.DELETED||(/\.bootstrap\.ts?/.test(e)||/\.test\.ts?/.test(e)||/\.test\.ejs/.test(e)||/\.bootstrap\.ejs/.test(e))&&(this.bootscriptFileList.push(e),c.mode!==FileSystemWatchDto_1.E_FILE_MODE.RENAME&&c.mode!==FileSystemWatchDto_1.E_FILE_MODE.DELETED||this.dispatchEvent("rebuild")),c.mode!==FileSystemWatchDto_1.E_FILE_MODE.FOUND&&c.mode!==FileSystemWatchDto_1.E_FILE_MODE.CHANGE||(o=Path.dirname(i),/\.htm?/.test(e)&&!1===/\.bootstrap\.htm?/.test(e)&&(logger.log("copyFileSync",c.filename,i),fs.ensureDirSync(o),fs.copyFileSync(c.filename,i)),/\.bootstrap\.ejs/.test(e)&&this.outputHTML(e,o),!0===this.isWatch()&&/\.test\.ejs/.test(e)&&this.outputHTML(e,o),/\.dist$/.test(Path.parse(e).name)&&(a=c.filename.replace(r,""),n=Path.dirname(a)+"/"+Path.basename(a).replace(".dist",""),u=Path.resolve(this.rootDir+"/"+this.getVersionOutDir()+n),logger.log("copyFileSync",c.filename,u),fs.ensureDirSync(Path.dirname(u)),fs.copyFileSync(c.filename,u))),c.mode===FileSystemWatchDto_1.E_FILE_MODE.DELETED&&!1===/\.bootstrap\.ts?/.test(e)&&fs.removeSync(i),[2]})})}),[4,e.on()];case 1:return t.sent(),[2]}})})},t.prototype.outputHTML=function(o,a){return tslib_1.__awaiter(this,void 0,void 0,function(){var e,r,s,i;return tslib_1.__generator(this,function(t){return logger.log("outputHTML",o),e=o.replace(this.sourceDir,""),r=this.getDistHTMLContent(fs.readFileSync(o,"utf-8")),s=Path.basename(e.replace(".bootstrap.ejs",".html").replace(".test.ejs",".test.html")),i=a+"/"+s,fs.outputFileSync(i,r),[2]})})},t.prototype.getDistHTMLContent=function(t){return t=ejs.render(t,this.ejsData,{rmWhitespace:!0}),t=html_minifier_1.minify(t,{collapseWhitespace:!0})},t.prototype.getBootScriptFileList=function(){for(var t=[],e=0,r=this.bootscriptFileList;e<r.length;e++){var s=r[e];fs.existsSync(s)&&t.push(s)}return t},t}(BrowserBase_1.BrowserBase);exports.BrowserAsset=BrowserAsset;
\No newline at end of file