"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.innerUpload=exports.upload=exports.SIGNATURE_FILE_NAME=void 0;const tslib_1=require("tslib"),log=tslib_1.__importStar(require("../utils/log")),request_1=require("../utils/request"),pack_1=require("./utils/pack"),zlib_1=tslib_1.__importDefault(require("zlib")),sign_1=require("../utils/sign"),tools_1=require("../utils/tools"),config_1=require("../config/config"),taskstatus_1=require("../utils/taskstatus"),error_1=require("../utils/error"),locales_1=tslib_1.__importDefault(require("../utils/locales/locales")),querystring_1=tslib_1.__importDefault(require("querystring")),progressupdate_1=require("./utils/progressupdate"),url_config_1=require("../config/url.config"),jsonParse_1=require("../utils/jsonParse"),cache_1=require("../utils/cache"),cos_upload_1=require("./cos-upload"),getcompiler_1=require("./getcompiler"),tools_2=require("./utils/tools");exports.SIGNATURE_FILE_NAME="ci.signature";const MIN_COS_UPLOAD_SIZE=5242880;async function upload(e){var o;const r=await innerUpload(e);return(null===(o=r.respBody)||void 0===o?void 0:o.dev_plugin_id)&&(log.log("Development Version Plugin ID: "+r.respBody.dev_plugin_id),r.devPluginId=r.respBody.dev_plugin_id),{subPackageInfo:r.subPackageInfo,pluginInfo:r.pluginInfo,devPluginId:r.devPluginId}}async function innerUpload(e){const{project:o,setting:r={useProjectConfig:!1},desc:t=`robot ${e.robot||"1"} use miniprogram-ci to upload at ${(0,tools_1.formatTime)(new Date)}`,version:i="",robot:s="1",test:a,pagePath:n,searchQuery:l,threads:u=0,bigPackageSizeSupport:p}=e,c=(0,progressupdate_1.transProgressUpdate)(e.onProgressUpdate||(e=>{if("object"==typeof e)try{const o=JSON.stringify(e);console.log(o)}catch(e){}console.log(""+e)}));let{useCOS:d}=e;if(process.env.COMPILE_THREADS=u.toString(),!i)throw new error_1.CodeError(locales_1.default.config.PARAM_ERROR.format("upload","version"),config_1.PARAM_ERROR);if(!o)throw new error_1.CodeError(locales_1.default.config.PARAM_ERROR.format("upload","project"),config_1.PARAM_ERROR);cache_1.cacheManager.clean();const _=await(0,getcompiler_1.getCompiler)(o,r),g=(0,tools_2.formatCISetting)(o,r),f=await _.compile({setting:g,onProgressUpdate:c,resultType:"prod",disableSpreadingUsingComponents:!0}),b=config_1.CI_VERSION,y={codeprotect:r.codeProtect?1:0,type:o.type,appid:o.appid,version:i,desc:t,robot:s},S={scene:e.scene||1011};let P;n&&(S.path=n,y.path=n),l&&(S.query=querystring_1.default.parse(l)),y.debugLaunchInfo=JSON.stringify(S),n&&l&&(y.path+="?"+l),a&&p&&(y.bigPackageSizeSupport=1);let q={};try{P=await o.getFile(o.miniprogramRoot,"ext.json"),q=JSON.parse(P.toString("utf-8"))}catch(e){}if(q&&(q.extEnable&&(y.extAppId=q.extAppid),q.directCommit)){let e="";e=q.extEnable?"The code will be uploaded into the waiting list of extAppid.":"The code will be uploaded into the draft box of the third-party platform.",log.warn(e)}try{const e=new taskstatus_1.TaskStatus(locales_1.default.config.UPLOAD.toString());c(e);const r=`${a?url_config_1.TEST_SOURCE_URL:url_config_1.UPLOAD_URL}?${querystring_1.default.stringify(y)}`;let t,i=!1,n=0;const l=(0,pack_1.pack)(f),u=zlib_1.default.gzipSync(l.buffer);if(log.info("useCOS parameter: ",d),log.info("upload zip buffer size: ",u.length),void 0===d&&(d=u.length>5242880),d){log.info("upload by COS: ",d);const e=await(0,cos_upload_1.uploadByCos)(u,r,o,s);e.fallback?(i=e.fallback,log.info("upload by COS failed, fallback to http way")):(t={body:e.body},n=e.uploadCOSCostTime)}if(!d||i){const e=await(0,sign_1.getSignature)(o.privateKey,o.appid);f[exports.SIGNATURE_FILE_NAME]=JSON.stringify({signature:e,version:b});const i=(0,pack_1.pack)(f),s=zlib_1.default.gzipSync(i.buffer);log.info("request url:",r);const a=(await(0,request_1.request)({url:r,method:"post",body:s})).body.toString();if(t=(0,jsonParse_1.jsonRespParse)(a,r),0!==t.errCode)throw new Error(a)}e.done(),c(e);const p={respBody:t.body};if(Array.isArray(t.body.subpackage_info)){const e=t.body.subpackage_info;p.subPackageInfo=e}if(Array.isArray(t.body.ext_plugin_info)){const e=t.body.ext_plugin_info;p.pluginInfo=e.map(e=>({pluginProviderAppid:e.provider,version:e.version,size:e.size}))}return p}catch(e){throw new error_1.CodeError(e.toString(),config_1.UPLOAD_CGI_ERR)}}exports.upload=upload,exports.innerUpload=innerUpload;