"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.upload=upload;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"),asyncTask_1=require("../../utils/asyncTask");function getMiniappBuildQuery(e){var i,t,r,o,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===(t=e.miniappOptions)||void 0===t?void 0:t.uploadId)&&(p.upload_id=null===(r=e.miniappOptions)||void 0===r?void 0:r.uploadId),(null===(o=e.miniappOptions)||void 0===o?void 0:o.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:t={useProjectConfig:!1},robot:r="1",threads:o=0,onProgressUpdate:s=(()=>{})}=e,n=(0,progressupdate_1.transProgressUpdate)(s);process.env.COMPILE_THREADS=o.toString(),cache_1.cacheManager.clean();const a=(0,tools_1.formatCISetting)(i,t);i.setting=a;const p=e.targetPlatform;i.setting.targetPlatform=p;const u=await(0,getcompiler_1.getBuilder)(i,{targetPlatform:p}),l=await u.getCompiler(),c=await l.compile({setting:a,onProgressUpdate:n,resultType:"prod",disableSpreadingUsingComponents:!0});u.destroy();const d=config_1.CI_VERSION,_=encodeURIComponent(JSON.stringify(getMiniappBuildQuery(e))),g={appid:i.appid,robot:r,miniapparchivequery:_};try{const e=new taskstatus_1.TaskStatus(locales_1.default.config.UPLOAD.toString());n(e);const t=`${url_config_1.miniappUpload}?${querystring_1.default.stringify(g)}`,r=await(0,sign_1.getSignature)(i.privateKey,i.appid);c[asyncTask_1.SIGNATURE_FILE_NAME]=JSON.stringify({signature:r,version:d});const o=(0,pack_1.pack)(c),s=zlib_1.default.gzipSync(o.buffer);log.info("request url:",t);const a=(await(0,request_1.request)({url:t,method:"post",body:s})).body.toString(),p=(0,jsonParse_1.jsonRespParse)(a,t);if(0!==p.errCode)throw new Error(a);e.done(),n(e);const u={respBody:p.body};if(Array.isArray(p.body.subpackage_info)){const e=p.body.subpackage_info;u.subPackageInfo=e,u.useSubPkg=e.length>1}if(Array.isArray(p.body.ext_plugin_info)){const e=p.body.ext_plugin_info;u.pluginInfo=e.map(e=>({pluginProviderAppid:e.provider,version:e.version,size:e.size}))}return u}catch(e){throw e}}