"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.uploadJsServer=uploadJsServer;const tslib_1=require("tslib"),path_1=tslib_1.__importDefault(require("path")),request_1=require("../utils/request"),packfile_1=require("./utils/packfile"),zlib_1=tslib_1.__importDefault(require("zlib")),sign_1=require("../utils/sign"),config_1=require("../config/config"),log=tslib_1.__importStar(require("../utils/log")),error_1=require("../utils/error"),locales_1=tslib_1.__importDefault(require("../utils/locales/locales")),querystring_1=tslib_1.__importDefault(require("querystring")),url_config_1=require("../config/url.config"),jsonParse_1=require("../utils/jsonParse"),projectconfig_1=require("../modules/corecompiler/original/json/projectconfig");async function uploadJsServer(r){var e;const{project:o,env:t}=r;if(!t||"test"!==t&&"release"!==t)throw new error_1.CodeError(locales_1.default.config.PARAM_ERROR.format("uploadJsServer","env"),config_1.PARAM_ERROR);if(!o)throw new error_1.CodeError(locales_1.default.config.PARAM_ERROR.format("uploadJsServer","project"),config_1.PARAM_ERROR);const i=(0,projectconfig_1.getProjectConfigJSON)(o);if("string"!=typeof i.jsserverRoot||!(null===(e=o.stat("",i.jsserverRoot))||void 0===e?void 0:e.isDirectory))throw new error_1.CodeError("please set a correct jsserverRoot value in project.config.json",config_1.PARAM_ERROR);const s=await(0,packfile_1.packFiledir)(path_1.default.posix.join(o.projectPath,i.jsserverRoot)),l=await(0,sign_1.getSignature)(o.privateKey,o.appid),_={type:"release"===t?0:1,appid:o.appid,signature:l};try{const r=`${url_config_1.UPLOAD_JS_SERVER}?${querystring_1.default.stringify(_)}`;log.info("request url:",r);const e=zlib_1.default.gzipSync(s.data),o=(await(0,request_1.request)({url:r,method:"post",body:e})).body.toString();if(0!==(0,jsonParse_1.jsonRespParse)(o,r).errCode)throw new Error(o);return!0}catch(r){throw new error_1.CodeError(r.toString(),config_1.UPLOAD_JS_SERVER_CGI_ERR)}}