"use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var r,o=1,s=arguments.length;o<s;o++)for(var t in r=arguments[o])Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t]);return e}).apply(this,arguments)};Object.defineProperty(exports,"__esModule",{value:!0});var express=require("express"),fs_1=require("fs"),path_1=require("path"),Request=require("request"),utils_1=require("../../lib/utils"),host=process.argv[2],port=Number(process.argv[3]),resourcePath=process.argv[4],workspaceRoot=utils_1.getRuntime().workspaceRoot;if(!workspaceRoot)throw Error("请在 NgCortex 工程目录中操作！");var server=express();server.all("*",function(e,r,o){r.header("Access-Control-Allow-Origin","*"),r.header("Access-Control-Allow-Headers","*"),r.header("Access-Control-Allow-Methods","*"),o()});var name_1,resource=require(workspaceRoot+"/resources/resource.mock.json"),_loop_1=function(r){var e=resource[r],t=e.origin;t.remoteOnly?e.origin=t.remote:(e.origin="http://"+host+":"+port+"/"+r,utils_1.src("./**/*.js",{base:path_1.join(workspaceRoot+"/resources",t.local),read:!1}).subscribe(function(e){var s={};e.filter(function(e){return!e.stat.isDirectory()}).forEach(function(e){try{Object.assign(s,require(e.fullPath))}catch(e){}}),server.use("/"+r,function(e,r){var o=("OPTIONS"===e.method?e.headers["access-control-request-method"]:e.method)+" "+e.url.replace(/\?.*$/,""),o=s[o];o?"OPTIONS"===e.method?r.end():o(e,r):(o=t.remote.replace(/\/$/,"")+e.url,e.pipe(Request(o)).on("error",function(e){r.status(500).json(__assign(__assign({},e),{message:e.toString().replace(/^Error: |\n+(.|\n)*/g,"")}))}).pipe(r))})}))};for(name_1 in resource)_loop_1(name_1);server.get(resourcePath,function(e,r){return r.json(resource)}),server.listen(port,host,function(){function e(){clearTimeout(r),r=setTimeout(function(){return process.exit(0)},100)}var r;fs_1.watch(workspaceRoot+"/environments/environment.mock.ts",e),fs_1.watch(workspaceRoot+"/resources/resource.mock.json",e),fs_1.watch(workspaceRoot+"/mock",{recursive:!0},e)});
