/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/tmodjs@1.0.4/src/tmod.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
/*!
 * TmodJS - AOT Template Compiler
 * https://github.com/aui/tmodjs
 * Released under the MIT, BSD, and GPL Licenses
 */
"use strict";var version=require("../package.json").version,AOTcompile=require("./AOTcompile.js"),defaults=require("./defaults.js"),runtime=require("./runtime.js"),uglify2=require("./uglify2.js"),stdout=require("./stdout.js"),watch=require("./watch.js"),path=require("./path.js"),semver=require("semver"),fs=require("fs"),vm=require("vm"),events=require("events"),crypto=require("crypto"),child_process=require("child_process"),exec=child_process.exec,DEBUG_File=".debug.js",CACHE_DIR=".cache",log=function(t){console.log(t)},Tmod=function(t,e){this.base=path.resolve(t),this.options=e=this.getConfig(e),!1!==e.output?(this.output=path.resolve(this.base,e.output),this.runtime=path.resolve(this.output,e.runtime)):this.output=defaults.output,this._cache={},this._clear(),this._initEngine(),events.EventEmitter.call(this),this.on("newListener",(function(t,e){"watch"===t&&(this.log("\n[green]Waiting...[/green]\n\n"),watch(this.base,function(t){this.emit("watch",t)}.bind(this),function(t){return this.filter(t)&&t!==this.output}.bind(this),fs))})),this.on("change",(function(t){var e=(new Date).toLocaleTimeString();this.log("[grey]"+e+"[/grey]\n")})),this.on("delete",(function(t){var e=(new Date).toLocaleTimeString();this.log("[grey]"+e+"[/grey]\n"),this.log("[red]-[/red] "+t.id+"\n")})),this.on("load",(function(t,e){if(t)return this.log("[red]•[/red] "),void this.log(e.id);e.modified?this.log("[green]•[/green] "):this.log("[grey]•[/grey] "),this.log(e.id)})),this.on("compile",(function(t,e){t?this.log(" [inverse][red]{{Syntax Error}}[/red][/inverse]\n\n"):(this.log(this.options.debug?" [grey]<DEBUG>[/grey]":""),this.log(" [grey]:v"+e.version+"[/grey]"),this.log("\n"))})),this.on("debug",(function(t){this.log("[red]Debug info:[/red]\n"),t.line&&t.source&&this.log("[red]"+t.line+": "+t.source+"[/red]\n"),this.log("[red]"+t.message+"[/red]\n")})),this.on("combo",(function(t,e){t&&this.log("[red]"+t+"[/red]\n")})),this._buildRuntime()};Tmod.defaults=defaults,Tmod.prototype={__proto__:events.EventEmitter.prototype,getConfig:function(){var t=arguments[0];if(!t)return this.options;var e=path.join(this.base,"package.json"),i=Tmod.defaults,s=null,n=null,o={};if(fs.existsSync(e)){var r=fs.readFileSync(e,"utf-8");r&&(s=JSON.parse(r))}s||(s={name:"template",version:"1.0.0",dependencies:{tmodjs:"1.0.0"},"tmodjs-config":{}}),s.dependencies||(s.dependencies=s.devDependencies);var a=s.dependencies.tmodjs.replace(/^~/,"");try{semver.lt(version,a)&&(this.log("[red]You must upgrade to the latest version of tmodjs![/red]\n"),this.log("Local:  "+version+"\n"),this.log("Target: "+a+"\n"),process.exit(1))}catch(t){}for(n in s.dependencies.tmodjs=version,i)o[n]=i[n];for(n in s["tmodjs-config"])o[n]=s["tmodjs-config"][n];for(n in t)void 0!==t[n]&&(o[n]=t[n]);return o=this._fixConfig(o,i,s["tmodjs-config"],t),s["tmodjs-config"]=o,this["package.json"]=s,this.projectVersion=s.version,o},saveConfig:function(){var t=path.join(this.base,"package.json"),e="tmodjs-config",i=this["package.json"],s=i[e],n=Object.keys(Tmod.defaults);i[e]=JSON.parse(JSON.stringify(s,n));var o=JSON.stringify(i,null,4);return fs.writeFileSync(t,o,"utf-8"),t},compile:function(t){var e,i=this,s=!1;if(t){var n="string"==typeof t?[t]:t;n=n.map((function(t){return path.resolve(i.base,t)})),e=function(t){t.forEach((function(t){s||(s=!i._compile(t))}))},e(n),!s&&this.options.combo&&this._combo()}else(e=function(t){t!==i.output&&fs.readdirSync(t).forEach((function(n){s||(fs.statSync(path.join(t,n)).isDirectory()?e(path.join(t,n)):i.filterBasename(n)&&i.filterExtname(n)&&(s=!i._compile(path.join(t,n))))}))})(this.base),!s&&this.options.combo&&this._combo()},filter:function(t){if(fs.existsSync(t)){if(fs.statSync(t).isDirectory()){var e=t.split(path.sep),i=e[e.length-1];return!!this.filterBasename(i)}return this.filterBasename(path.basename(t))&&this.filterExtname(path.extname(t))}return!1},filterBasename:function(t){return!/^\.|[^\w\.\-$]/.test(t)},filterExtname:function(t){return/\.(html|htm|tpl)$/i.test(t)},watch:function(){this.on("watch",(function(t){var e=t.type,i=t.fstype,s=t.target,n=t.parent,o=path.join(n,s);if(s&&"file"===i&&this.filter(o))if("delete"===e){this.emit("delete",{id:this._toId(s),sourceFile:s});var r=o.replace(path.extname(o),"");r=r.replace(this.base,this.output)+".js",this._fsUnlink(r),this._removeCache(s),this.options.combo&&this._combo()}else/updated|create/.test(e)&&(this.emit("change",{id:this._toId(s),sourceFile:s}),this._compile(o)&&this.options.combo&&this._combo())}))},log:function(t){this.options.verbose&&stdout(t)},_fixConfig:function(t,e,i,s){var n=process.cwd(),o=this.base;return t.type=t.type.toLowerCase(),Array.isArray(t.combo)&&!t.combo.length?t.combo=!1:t.combo=!!t.combo,"templatejs"===t.type&&(t.type="default"),"default"===t.type||"global"===t.type?delete t.alias:delete t.combo,s.output&&(t.output=path.relative(o,path.resolve(n,s.output))),s.syntax&&/\.js$/.test(s.syntax)&&(t.syntax=path.relative(o,path.resolve(n,s.syntax))),s.helpers&&(t.helpers=path.relative(o,path.resolve(n,s.helpers))),t},_fsWrite:function(t,e,i){this._fsMkdir(path.dirname(t)),fs.writeFileSync(t,e,i||"utf-8")},_fsRead:function(t,e){if(fs.existsSync(t))return fs.readFileSync(t,e||"utf-8")},_fsMkdir:function(t){for(var e=t,i=[];!fs.existsSync(e);)i.unshift(e),e=path.dirname(e);i.forEach((function(t){fs.mkdirSync(t)}))},_fsRmdir:function(t){var e=function(t){if(fs.existsSync(t)&&fs.statSync(t).isDirectory()){var i=fs.readdirSync(t);i.length?(i.forEach((function(i){var s=path.join(t,i);fs.statSync(s).isDirectory()?e(s):fs.unlinkSync(s)})),fs.rmdirSync(t)):fs.rmdirSync(t)}};e(t)},_fsUnlink:function(t){return fs.existsSync(t)&&fs.unlinkSync(t)},_getMd5:function(t){return crypto.createHash("md5").update(t).digest("hex")},_getMetadata:function(t){var e=t.match(/\/\*TMODJS\:(.*?)\*\//);if(e)return JSON.parse(e[1])},_removeMetadata:function(t){var e=this._getMetadata(t)||{},i="";return e.version&&(i="/*v:"+e.version+"*/"),t.replace(/^\/\*TMODJS\:(?:.*)\*\//,i)},_setMetadata:function(t,e){return t="/*TMODJS:"+(e=JSON.stringify(e||{}))+"*/\n"+t.replace(/\/\*TMODJS\:(.*?)\*\//,"")},_debug:function(t,e){var i=t.debugFile,s=t.temp;s="/*! <DEBUG:"+t.id+"> */\n"+s,this._fsWrite(i,s,this.options.charset),exec("node "+i,(function(t,i,s){var n=t?t.message:"";n=n.replace(/^Command\sfailed\:|\s*SyntaxError[\w\W]*$/g,"").trim(),e(n)}))},_buildRuntime:function(t,e,i){t=t||"",e=e||{},i=i||function(){};var s=null,n=runtime({type:this.options.type,helpers:this._helpersCode,templates:t});if(n=this._setMetadata(n,e),!1!==this.options.output){try{this._fsMkdir(path.dirname(this.runtime)),fs.writeFileSync(this.runtime,n,this.options.charset)}catch(t){s=t}this.options.debug&&this._beautify(this.runtime),!this.options.debug&&this.options.minify&&this._minify(this.runtime)}i.call(this,s,n)},_getUglifyOptions:function(){return{reserved:"require",comments:"/TMODJS\\:|^v\\:\\d+/",compress:{warnings:!1}}},_uglify:function(t,e){var i;try{i=uglify2(t,t,e)}catch(e){var s=new Error("Uglification failed.");e.message&&(s.message+="\n"+e.message+". \n",e.line&&(s.message+="Line "+e.line+" in "+t+"\n")),s.origError=e,console.log(s)}try{i&&fs.writeFileSync(t,i.output,this.options.charset)}catch(t){}},_beautify:function(t){var e=this._getUglifyOptions();e.mangle=!1,e.beautify=!0,this._uglify(t,e)},_minify:function(t){var e=this._getUglifyOptions();e.mangle={},e.beautify=!1,e.ascii_only=!0,this._uglify(t,e)},_combo:function(){var t=[],e="",i=this._getCache(),s="",n=Date.now();for(var o in i)s=i[o],e+=s=this._removeMetadata(s),t.push(o);var r={};this.options.debug&&(r.debug=!0),this.options.combo&&(r.version=this.projectVersion),this._buildRuntime(e,r,(function(e,i){this.emit("combo",e,{build:n,output:i,outputFile:this.runtime,sourcefiles:t})}))},_toId:function(t){var e=path.extname(t);return t.replace(this.base+"/","").replace(e,"")},_compile:function(t){var e="",i=null,s=null,n=null,o=t.replace(path.extname(t),".js").replace(this.base,this.output),r=this._getCache(t),a={},h={},c=0,u=this.options.debug,l=this.options.combo;try{e=fs.readFileSync(t,this.options.charset)}catch(t){i=t}var f=this._getMd5(e+JSON.stringify(this["package.json"]));l&&(o=o.replace(this.output,path.join(this.output,CACHE_DIR))),!r&&fs.existsSync(o)&&(r=this._fsRead(o,this.options.charset)),r&&(c=(h=this._getMetadata(r)||{}).version||0);var p=!this.options.cache||!r||h.debug||u||f!==h.md5,d=this._toId(t);if(this.emit("load",i,{id:d,modified:p,sourceFile:t,source:e,outputFile:o}),!i){try{p&&(r=(a=this.template.AOTcompile(e,{filename:d,alias:this.options.alias,type:this.options.type,compress:this.options.compress,escape:this.options.escape,runtime:this.options.runtime,debug:u})).code)}catch(t){s=t}if(!s&&p&&(c++,r=this._setMetadata(r,{debug:u,version:c,md5:f}),!1!==this.options.output)){try{this._fsMkdir(path.dirname(o)),fs.writeFileSync(o,r,this.options.charset)}catch(t){n=t}l||n||(u&&this._beautify(o),!u&&this.options.minify&&this._minify(o))}var m={id:d,version:c,source:e,sourceFile:t,output:r,outputFile:o,modified:p,requires:a.requires||[]};return s&&!s.source?(s.debugFile=path.join(this.base,DEBUG_File),this.debuging=!0,this._debug(s,function(t){var e={name:s.name,message:t,debugFile:s.debugFile,temp:s.temp};for(var i in e)s[i]=e[i];this.emit("debug",s)}.bind(this))):(this.debuging&&(this._fsUnlink(path.join(this.base,DEBUG_File)),delete this.debuging),this._setCache(t,r)),this.emit("compile",s||n,m),s||n?(this.emit("debug",s||n),null):m}},_getByteLength:function(t){return t.replace(/[^\x00-\xff]/gi,"--").length},_getCache:function(t){return void 0===t?this._cache:this._cache[t]},_setCache:function(t,e){this._cache[t]=e},_removeCache:function(t){delete this._cache[t]},_initEngine:function(){var t,e=this.options;switch(String(e.syntax)){case"native":t=require("./syntax/native.js");break;case"simple":t=require("./syntax/simple.js");break;default:var i=path.resolve(this.base,e.syntax);if(fs.existsSync(i)){t=require("./syntax/native.js");var s=fs.readFileSync(i,"utf-8");vm.runInNewContext(s,{console:console,template:t})}else this.log("[red]Not found: "+i+"[/red]"),process.exit(1)}if(e.helpers){var n=path.resolve(this.base,e.helpers);fs.existsSync(n)?(this._helpersCode=fs.readFileSync(n,"utf-8"),vm.runInNewContext(this._helpersCode,{console:console,template:t})):(this.log("[red]Not found: "+n+"[/red]"),process.exit(1))}this.template=AOTcompile(t)},_clear:function(){this._fsUnlink(path.join(this.base,DEBUG_File)),this.options.combo||this._fsRmdir(path.join(this.output,CACHE_DIR))}},module.exports=Tmod;
//# sourceMappingURL=/sm/22ded5d78fc4d9e44bebd7dd1f2e0e8d7631835922edda4d9492d7e2a4cda980.map