"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var child_process_1=require("child_process"),fs_1=require("fs"),inquirer=require("inquirer"),path_1=require("path"),operators_1=require("rxjs/operators"),utils_1=require("../../lib/utils");function default_1(e){var c=utils_1.getRuntime();if(!c.workspaceRoot)throw Error("请在 NgCortex 工程目录中操作！");var a=e.options||{};Promise.resolve().then(function(){if(c.portalRoot||c.frontendRoot)return Promise.resolve();var o=[{type:"list",message:"请选择项目:",name:"cwd",choices:[]}];if(o[0].choices.push({name:"portal",value:c.workspaceRoot+"/portal"}),fs_1.readdirSync(c.workspaceRoot+"/frontends").forEach(function(e){o[0].choices.push({name:e,value:c.workspaceRoot+"/frontends/"+e})}),0===o[0].choices.length)throw new Error("暂无项目可操作");return inquirer.prompt(o).then(function(e){c=utils_1.getRuntime(e.cwd)})}).then(function(){var s;if(a.useMock){var e=utils_1.getContentText(fs_1.readFileSync(c.workspaceRoot+"/environments/environment.mock.ts")),i=new URL(e.match(/(?<=resourceUrl\: *["'])[^"']+/g)[0]);if("http:"!==i.protocol)throw new Error("mock服务器不支持"+i.protocol+"协议");var n=function(){var e=path_1.join(__dirname,"./subprocess"),o=i.hostname,r=i.port||"80",t=i.pathname;(s=child_process_1.spawn("node",[e,o,r,t],{stdio:["pipe","inherit","inherit"]})).on("close",function(e){0===e&&n()})};n()}var o,r,t="win32"===process.platform?"ng.cmd":"ng",p=["serve"];a.host&&p.push("--host="+a.host),a.port&&p.push("--port="+a.port),a.open&&p.push("--open="+a.open),a.useMock&&p.push("--configuration=mock"),c.portalRoot?(o=child_process_1.spawn(t,p,{cwd:c.portalRoot,stdio:["pipe","inherit","inherit"]})).on("close",function(){s&&s.kill()}):(e={getPascalCase:utils_1.getPascalCase,getSeparatorCase:utils_1.getSeparatorCase,name:c.frontendName},r=c.frontendRoot+"/__dev-portal__",utils_1.src("./**/*",{base:path_1.join(__dirname,"./files")}).pipe(utils_1.ejs(e)).pipe(utils_1.dest(r)).subscribe(function(){(o=child_process_1.spawn(t,p,{cwd:r,stdio:["pipe","inherit","inherit"]})).on("close",function(){utils_1.src([r,r+"/**/*"],{read:!1}).pipe(operators_1.delay(100)).pipe(utils_1.clean()).subscribe(function(){s&&s.kill()})})})),process.on("SIGINT",function(){o&&o.kill()}),process.on("SIGHUP",function(){})})}exports.default=default_1;
