@mornya/vessel
Version:
Creates/publishes a Node.js module with no build configuration.
1 lines • 4.11 kB
JavaScript
var c=this&&this.__assign||function(){return(c=Object.assign||function(t){for(var e,i=1,o=arguments.length;i<o;i++)for(var s in e=arguments[i])Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s]);return t}).apply(this,arguments)},t=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}},l=(Object.defineProperty(exports,"__esModule",{value:!0}),require("@mornya/cli-libs")),u=t(require("node-watch")),e=t(require("./clean")),i=require("../cli"),o=require("../env"),h=require("../paths");function BuildScript(t,e){this.option=t,this.isExistPublic=l.Files.isExist(h.paths.appPublic),this.isExistDotEnv=l.Files.isExist(h.paths.appDotEnv);t=null!=(t=null==(t=i.CLI.appPackageJson.vessel)?void 0:t.minify)?t:"default";this.isMinify="default"===t||!("none"===t)}BuildScript.prototype.run=function(){this.cleanProgress(),this.copyExtraAssetsProgress(),this.loadDotEnvProgress(),this.option.watch?this.watchProgress():this.buildProgress()},BuildScript.prototype.compile=function(){var t,e;return l.Log.info(),l.Command.run(h.commands.tsc,["--project",h.paths.appPath,"--pretty",!this.option.watch&&"--diagnostics","--extendedDiagnostics",this.option.watch?"false":"true","--preserveWatchOutput","true","--listEmittedFiles","false"].filter(Boolean))?(l.Command.run(h.commands.tscAlias,["-p","".concat(h.paths.appPath,"/tsconfig.json")]),!this.option.watch&&this.isMinify&&(l.Log.info(),(e=l.Minify.run(h.paths.appBuild)).failed||(t=e.succeed,e=e.totalFiles,l.Log.info("Minify completed ".concat(t," / ").concat(e," file").concat(1<t?"s":"",".")))),this.option.esm&&l.Command.run(h.commands.tsc,["--project",h.paths.appPath,"--pretty","--preserveWatchOutput","true","--listEmittedFiles","false","--module","esnext","--outDir","./esm"].filter(Boolean))&&l.Command.run(h.commands.tscAlias,["--project","".concat(h.paths.appPath,"/tsconfig.json"),"--outDir","./esm"]),this.option.watch?l.Log.watch(l.chalk.green("Build succeed.")):l.Log.success("Build succeed without errors."),!0):(e="Failed to compile!",this.option.watch?l.Log.watch(l.chalk.red(e)):l.Log.error(e),!1)},BuildScript.watchConsole=function(t,e){l.Log.watch(t,e.replace("".concat(h.paths.appPath,"/"),""))},BuildScript.prototype.cleanProgress=function(){l.Log.progress("Clean build and cache directories..."),new e.default({debug:!1},[]).run([this.option.esm?"esm":"dist"])},BuildScript.prototype.copyExtraAssetsProgress=function(){this.isExistPublic&&(l.Log.progress("Copy public resources to build directory..."),l.Files.copyDir(h.paths.appPublic,this.option.esm?h.paths.appBuildESM:h.paths.appBuild))},BuildScript.prototype.loadDotEnvProgress=function(){this.isExistDotEnv&&(l.Log.progress("Load .env variables into environment..."),(0,o.loadEnv)((0,h.basename)(h.paths.appDotEnv)))},BuildScript.prototype.watchProgress=function(){var t=this,e=(l.Log.progress("Build and watch your project..."),{recursive:!0,persistent:!0,delay:300}),i=c(c({},e),{filter:function(t){return/\.([t|j]sx?|mjs|json)$/.test(t)}}),o=!1,s=[{path:"*",done:!1}],n=(l.Log.linefeed(),l.Log.watch("started"),null),e=(this.isExistPublic&&(n=(0,u.default)(h.paths.appPublic,e,function(t,e){"update"===t&&(BuildScript.watchConsole(t,e),o=!0)})),[h.paths.appSrc,h.paths.appTypes,"".concat(h.paths.appPath,"/package.json"),"".concat(h.paths.appPath,"/tsconfig.json")]),r=(0,u.default)(e,i,function(t,e){"update"===t&&(BuildScript.watchConsole(t,e),s.push({path:e,done:!1}))}),a=setInterval(function(){o&&(l.Files.copyDir(h.paths.appPublic,h.paths.appBuild),o=!1)},300),p=setInterval(function(){s.length&&s.filter(function(t){return!t.done}).length&&(s=s.filter(function(t){return!t.done}),t.compile(),s.map(function(t){return t.done=!0}))},300);["beforeExit","unhandledRejection","uncaughtException","SIGINT","SIGTERM"].forEach(function(t){process.on(t,function(){a&&clearInterval(a),p&&clearInterval(p),null!=n&&n.close(),null!=r&&r.close(),l.Log.watch("terminated."),l.Log.linefeed(),process.exit(0)})})},BuildScript.prototype.buildProgress=function(){l.Log.progress("Start build your project..."),this.compile()||process.exit(1)},exports.default=BuildScript;