"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var ansi_colors_1=require("ansi-colors"),express=require("express"),path_1=require("path"),Request=require("request"),utils_1=require("../../lib/utils"),host=process.argv[2],port=Number(process.argv[3]),resconfigPath=process.argv[4],workspaceRoot=utils_1.getEnvironment().workspaceRoot;if(!workspaceRoot)throw Error("请在 NgCortex 工程目录中操作！");process.chdir(workspaceRoot);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,resconfig=require(workspaceRoot+"/mock/resconfig.json"),_loop_1=function(r){var e=resconfig[r],t=e.origin;t.remoteOnly?e.origin=t.remote:(e.origin="http://"+host+":"+port+"/"+r,utils_1.src("./**/*.js",{base:path_1.join("./mock",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)).pipe(r))})}))};for(name_1 in resconfig)_loop_1(name_1);server.get(resconfigPath,function(e,r){return r.json(resconfig)}),server.listen(port,host,function(){console.log("模拟服务器运行中，资源配置地址："+ansi_colors_1.cyan("http://"+host+":"+port+resconfigPath))});
