"use strict";var __assign=this&&this.__assign||function(){return __assign=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var a in r=arguments[t])Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a]);return e},__assign.apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,r,t,n){return new(t||(t=Promise))((function(a,o){function i(e){try{c(n.next(e))}catch(e){o(e)}}function s(e){try{c(n.throw(e))}catch(e){o(e)}}function c(e){var r;e.done?a(e.value):(r=e.value,r instanceof t?r:new t((function(e){e(r)}))).then(i,s)}c((n=n.apply(e,r||[])).next())}))},__generator=this&&this.__generator||function(e,r){var t,n,a,o,i={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(t)throw new TypeError("Generator is already executing.");for(;i;)try{if(t=1,n&&(a=2&o[0]?n.return:o[0]?n.throw||((a=n.return)&&a.call(n),0):n.next)&&!(a=a.call(n,o[1])).done)return a;switch(n=0,a&&(o=[2&o[0],a.value]),o[0]){case 0:case 1:a=o;break;case 4:return i.label++,{value:o[1],done:!1};case 5:i.label++,n=o[1],o=[0];continue;case 7:o=i.ops.pop(),i.trys.pop();continue;default:if(!(a=i.trys,(a=a.length>0&&a[a.length-1])||6!==o[0]&&2!==o[0])){i=0;continue}if(3===o[0]&&(!a||o[1]>a[0]&&o[1]<a[3])){i.label=o[1];break}if(6===o[0]&&i.label<a[1]){i.label=a[1],a=o;break}if(a&&i.label<a[2]){i.label=a[2],i.ops.push(o);break}a[2]&&i.ops.pop(),i.trys.pop();continue}o=r.call(e,i)}catch(e){o=[6,e],n=0}finally{t=a=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};exports.__esModule=!0,exports.stopDevServer=void 0;var devSever,webpack_1=__importDefault(require("webpack")),webpack_dev_server_1=__importDefault(require("webpack-dev-server")),chalk_1=__importDefault(require("chalk")),testPort_1=require("../../utils/testPort"),createDevServerOptions_1=__importDefault(require("./createDevServerOptions")),component_dev_config_1=__importDefault(require("../config/component.dev.config")),createEntryService_1=__importDefault(require("../createEntryService")),mergeWebpackConfig_1=__importDefault(require("../../utils/mergeWebpackConfig")),fs=require("fs-extra"),path=require("path").posix,_a=require("../../utils/paths"),frameworkEntryPath=_a.frameworkEntryPath,ComEntryDir=_a.ComEntryDir,stopDevServer=function(){return __awaiter(void 0,void 0,void 0,(function(){var e,r;return __generator(this,(function(t){switch(t.label){case 0:return devSever?(e=devSever.options.port,[4,devSever.stop()]):[2];case 1:return t.sent(),r=path.join(ComEntryDir,"entry-".concat(e,".js")),fs.removeSync(r),[2,e]}}))}))};exports.stopDevServer=stopDevServer;var createDevServer=function(e,r){return __awaiter(void 0,void 0,void 0,(function(){var t,n,a,o,i,s,c,u,l,_;return __generator(this,(function(f){switch(f.label){case 0:return f.trys.push([0,5,,6]),r.length?[4,(0,exports.stopDevServer)()]:[2];case 1:return f.sent(),[4,(0,testPort_1.findAvaliablePort)()];case 2:return(t=f.sent())?[4,(0,createEntryService_1.default)(t,r)]:(console.log(chalk_1.default.red("Failed to find an avaliable port")),[2,!1]);case 3:return n=f.sent(),a=n.entryFile,o=n.comInfos,i=(0,createDevServerOptions_1.default)({port:t}),s=(0,component_dev_config_1.default)(frameworkEntryPath,e),c=s.resolve,s.resolve=__assign(__assign({},c),{alias:{"@coms":a}}),[4,(0,mergeWebpackConfig_1.default)(o,s)];case 4:return u=f.sent(),l=(0,webpack_1.default)(u),new webpack_dev_server_1.default(i,l).start(),process.on("exit",(function(){var e=path.join(ComEntryDir,"entry-".concat(t,".js"));fs.removeSync(e)})),[2,t];case 5:return _=f.sent(),console.log(chalk_1.default.red(_.message)),[3,6];case 6:return[2]}}))}))};exports.default=createDevServer;