"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var child_process_1=require("child_process"),fs_1=require("fs"),inquirer=require("inquirer"),js_beautify_1=require("js-beautify"),path_1=require("path"),rxjs_1=require("rxjs"),operators_1=require("rxjs/operators"),utils_1=require("../../lib/utils");function default_1(e){var a=utils_1.getRuntime();if(!a.workspaceRoot)throw Error("请在 NgCortex 工程目录中操作！");var r=e.params||{},o=e.options||{};Promise.resolve().then(function(){if(a.portalRoot||a.frontendRoot)return Promise.resolve();var t=[{type:"list",message:"请选择项目:",name:"cwd",choices:[]}];if(r.name||o.allLibrary||(t[0].choices.push({name:"portal",value:a.workspaceRoot+"/portal"}),t[0].choices.push({name:"mock",value:a.workspaceRoot+"/mock"})),fs_1.readdirSync(a.workspaceRoot+"/frontends").forEach(function(e){t[0].choices.push({name:e,value:a.workspaceRoot+"/frontends/"+e})}),0===t[0].choices.length)throw new Error("暂无项目可操作");return inquirer.prompt(t).then(function(e){a=utils_1.getRuntime(e.cwd)})}).then(function(){if(a.frontendRoot){if(r.name)return a=utils_1.getRuntime(a.frontendRoot+"/"+utils_1.getSeparatorCase(r.name)),Promise.resolve();if(!o.allLibrary&&!a.libraryRoot){var t=[{type:"list",message:"请选择库项目:",name:"cwd",choices:[]}];if(Object.keys(a.frontendConfig.projects).forEach(function(e){t[0].choices.push({name:e,value:a.frontendRoot+"/"+e})}),0===t[0].choices.length)throw new Error("暂无库项目可操作");return inquirer.prompt(t).then(function(e){a=utils_1.getRuntime(e.cwd)})}}return Promise.resolve()}).then(function(){if(a.portalRoot){try{var e="ng build --prod";o.baseHref&&(e+=" --base-href="+o.baseHref),child_process_1.execSync(e,{cwd:a.portalRoot,stdio:"inherit",windowsHide:!0})}catch(e){process.exit()}var t=path_1.join(a.portalRoot,a.portalConfig.projects.portal.architect.build.options.outputPath);utils_1.src("./**.*",{base:t}).pipe(utils_1.transform(function(e){var t;".js"!==e.extname&&".html"!==e.extname||(t=utils_1.removeComments(utils_1.getContentText(e.content)),".html"===e.extname&&(t=js_beautify_1.html_beautify(t,{indent_size:2,preserve_newlines:!1})),o.baseHref&&"/"!==o.baseHref&&(t=t.replace(/(?<!\.)\/assets(?![A-Za-z])/g,"./assets")),e.content=Buffer.from(t))})).pipe(utils_1.dest(t)).subscribe()}else if(a.libraryRoot){try{child_process_1.execSync("ng build "+a.libraryName,{cwd:a.frontendRoot,stdio:"inherit",windowsHide:!0})}catch(e){process.exit()}var r=path_1.join(a.libraryRoot,a.libraryConfig.dest);utils_1.src("./*.min.js",{base:path_1.join(r,"bundles")}).pipe(operators_1.mergeMap(function(e){return utils_1.src([r,path_1.join(r,"**/*")],{read:!1}).pipe(utils_1.clean()).pipe(operators_1.map(function(){return e}))})).pipe(utils_1.transform(function(e){e.basename=a.libraryName+".js",e.content=Buffer.from(utils_1.removeComments(utils_1.getContentText(e.content)))})).pipe(utils_1.dest(path_1.dirname(r))).subscribe()}else{if(!a.frontendRoot||!o.allLibrary)throw new Error("构建项目不存在！");var n=rxjs_1.of([]),s=Object.keys(a.frontendConfig.projects);s.forEach(function(r,e){var o=a.frontendRoot+"/"+r,i=require(o+"/ng-package.json");n=n.pipe(operators_1.mergeMap(function(){process.stdout.write(++e+"/"+s.length+" 正在构建："+r+"\n");try{child_process_1.execSync("ng build "+r,{cwd:a.frontendRoot,stdio:"inherit",windowsHide:!0})}catch(e){process.exit()}var t=path_1.join(o,i.dest);return utils_1.src("./*.min.js",{base:path_1.join(t,"bundles")}).pipe(operators_1.mergeMap(function(e){return utils_1.src([t,path_1.join(t,"**/*")],{read:!1}).pipe(utils_1.clean()).pipe(operators_1.map(function(){return e}))})).pipe(utils_1.transform(function(e){e.basename=r+".js",e.content=Buffer.from(utils_1.removeComments(utils_1.getContentText(e.content)))})).pipe(utils_1.dest(path_1.dirname(t))).pipe(operators_1.tap(function(){e<s.length&&process.stdout.write("\n")}))}))}),n.subscribe()}})}exports.default=default_1;
