"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.upload=exports.SIGNATURE_FILE_NAME=void 0;const tslib_1=require("tslib"),cache_1=require("../../utils/cache"),locales_1=tslib_1.__importDefault(require("../../utils/locales/locales")),config_1=require("../../config/config"),getcompiler_1=require("../getcompiler"),tools_1=require("../utils/tools"),moment_1=tslib_1.__importDefault(require("moment")),url_config_1=require("../../config/url.config"),sign_1=require("../../utils/sign"),taskstatus_1=require("../../utils/taskstatus"),pack_1=require("../utils/pack"),log=tslib_1.__importStar(require("../../utils/log")),querystring_1=tslib_1.__importDefault(require("querystring")),zlib_1=tslib_1.__importDefault(require("zlib")),request_1=require("../../utils/request"),jsonParse_1=require("../../utils/jsonParse"),progressupdate_1=require("../utils/progressupdate");function getMiniappBuildQuery(e){var i,o,r,t,s,n;const a=(0,moment_1.default)(new Date).format("YYYY-MM-DD-HH-mm-ss"),p={gzip:1,cross_platform_app:1,"user-version":encodeURIComponent(e.version||"miniapp-release-d-"+a),"user-desc":encodeURIComponent(e.desc||"miniapp-release-d-"+a),os_type:"mini-android"===e.targetPlatform?2:1,debug_extra_info:"miniappPkgType-"+(null===(i=e.miniappOptions)||void 0===i?void 0:i.miniappPkgType)};return(null===(o=e.miniappOptions)||void 0===o?void 0:o.uploadId)&&(p.upload_id=null===(r=e.miniappOptions)||void 0===r?void 0:r.uploadId),(null===(t=e.miniappOptions)||void 0===t?void 0:t.useCloudSync)&&(p.use_cloud_sync=1),(null===(s=e.miniappOptions)||void 0===s?void 0:s.archVersion)&&(p.arch_version=null===(n=e.miniappOptions)||void 0===n?void 0:n.archVersion),p}async function upload(e){const{project:i,setting:o={useProjectConfig:!1},robot:r="1",threads:t=0}=e,s=(0,progressupdate_1.transProgressUpdate)(e.onProgressUpdate||(e=>{if("object"==typeof e)try{const i=JSON.stringify(e);console.log(i)}catch(e){}console.log(""+e)}));process.env.COMPILE_THREADS=t.toString(),cache_1.cacheManager.clean();const n=await(0,getcompiler_1.getCompiler)(i,o,{targetPlatform:e.targetPlatform}),a=(0,tools_1.formatCISetting)(i,o),p=await n.compile({setting:a,onProgressUpdate:s,resultType:"prod",disableSpreadingUsingComponents:!0}),u=config_1.CI_VERSION,l=encodeURIComponent(JSON.stringify(getMiniappBuildQuery(e))),c={appid:i.appid,robot:r,miniapparchivequery:l};try{const e=new taskstatus_1.TaskStatus(locales_1.default.config.UPLOAD.toString());s(e);const o=`${url_config_1.miniappUpload}?${querystring_1.default.stringify(c)}`,r=await(0,sign_1.getSignature)(i.privateKey,i.appid);p[exports.SIGNATURE_FILE_NAME]=JSON.stringify({signature:r,version:u});const t=(0,pack_1.pack)(p),n=zlib_1.default.gzipSync(t.buffer);log.info("request url:",o);const a=(await(0,request_1.request)({url:o,method:"post",body:n})).body.toString(),l=(0,jsonParse_1.jsonRespParse)(a,o);if(0!==l.errCode)throw new Error(a);e.done(),s(e);const _={respBody:l.body};if(Array.isArray(l.body.subpackage_info)){const e=l.body.subpackage_info;_.subPackageInfo=e,_.useSubPkg=e.length>1}if(Array.isArray(l.body.ext_plugin_info)){const e=l.body.ext_plugin_info;_.pluginInfo=e.map(e=>({pluginProviderAppid:e.provider,version:e.version,size:e.size}))}return _}catch(e){throw e}}exports.SIGNATURE_FILE_NAME="ci.signature",exports.upload=upload;