xhs-mp-core 1.2.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("xhs-mp-compiler-cli"),t=require("xhs-mp-project"),s=require("xhs-mp-utils"),r=require("events"),o=require("axios"),i=require("path"),a=require("os"),n=require("fs-extra"),l=require("fs"),p=require("rxjs"),c=require("lodash"),h=require("dayjs"),u=require("rxjs/operators"),d=require("qrcode-terminal"),g=require("jimp"),m=require("jsqr");function S(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(s){if("default"!==s){var r=Object.getOwnPropertyDescriptor(e,s);Object.defineProperty(t,s,r.get?r:{enumerable:!0,get:function(){return e[s]}})}})),t.default=e,Object.freeze(t)}var f=S(i),v=S(l);const w=()=>({isSit:"false"===process.env.XHS_IS_PROD,isProd:"true"===process.env.XHS_IS_PROD}),k=require("process").env.XHS_BUSINESS_SIT_HOST||"http://business-cli.sl.beta.xiaohongshu.com",x=require("process").env.XHS_CUSTOMER_SIT_HOST||"https://customer.sit.xiaohongshu.com",y={BUSINESS_HOST:w().isSit?k:"https://business.xiaohongshu.com",CUSTOMER_HOST:w().isSit?x:"https://customer.xiaohongshu.com",CUSTOMER_SERVICE_ID:w().isSit?x.replace(/https?:\/\/customer/,"file://miniapp-open"):"file://miniapp-open.xiaohongshu.com"},_={generateQrCode:`${y.CUSTOMER_HOST}/api/cas/generateQrCode`,queryQrCodeStatus:`${y.CUSTOMER_HOST}/api/cas/queryQrCodeStatus`,session:`${y.BUSINESS_HOST}/api/eros/mp/red/code/session`,getUserInfo:`${y.BUSINESS_HOST}/api/eros/mp/red/code/user/info`},C=require("crypto");require("net");const I=e=>{if(!e)return"";const t=1024;return e<t?`${e}B`:e<1048576?`${(e/t).toFixed(2)}KB`:e<t**3?`${(e/1048576).toFixed(2)}MB`:e<t**4?`${(e/t**3).toFixed(2)}GB`:`${(e/t**4).toFixed(2)}T`};function b(e){return e.replace(/\\/g,"/")}const P=b(i.join(a.homedir(),".xhs-mp-cli"));n.ensureDirSync(P);const T=b(i.join(P,"xhs-storage"));n.ensureDirSync(T);const j=require("tough-cookie");var q=new class{constructor(){this.cookiePath=i.join(T,"cookie.json");const e=this.getSerializeData();Object.keys(e).length?this.cookieJar=j.CookieJar.deserializeSync(e):this.cookieJar=new j.CookieJar}getSerializeData(){let e={};return n.existsSync(this.cookiePath)&&(e=n.readJSONSync(this.cookiePath)),e}save(){const e=this.cookieJar.serializeSync();n.writeJSONSync(this.cookiePath,e)}clear(){n.removeSync(this.cookiePath)}set(e){const t=e.headers["set-cookie"]||[],s=e.config.url;t.forEach((e=>{const t=j.Cookie.parse(e);if(t.domain){const e=new j.Cookie(t);this.cookieJar.setCookieSync(e,s)}})),this.save()}get(e){return this.cookieJar.getCookieStringSync(e)}};let E;o.defaults.baseURL=y.BUSINESS_HOST,o.interceptors.request.use((e=>{const{url:t,data:s,baseURL:r=""}=e;let i=o.getUri(e);if(/^https?:/.test(i))try{const e=new URL(t);i=i.replace(e.origin,"")}catch(e){i=t}const a=q.get(t);return e.headers={...e.headers,Cookie:a},e.headers["User-Agent"]="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) @xhs/mp-ide/2.0.0 Chrome/116.0.5845.228 Electron/26.4.0 Safari/537.36 discover/2.0.0 xhsminiapp/latest",e})),o.interceptors.response.use((e=>(q.set(e),e))),o.interceptors.response.use((e=>{var t;const{config:s,data:r}=e;if((null===(t=s.url)||void 0===t?void 0:t.startsWith(`${y.BUSINESS_HOST}/api/eros/mp/red`))&&!r.success)throw new Error(`${r.msg}`);return e}),(async e=>{const{response:t={}}=e;if(401===t.status){if(t.data&&-240309===t.data.code)return null==E||E.logout({from:"axios"}),Promise.reject(new Error("登陆失效,请重新登陆或采用 token 免密登陆:",t.data.msg));if(t.data&&t.data.msg)return null==E||E.logout({from:"axios"}),Promise.reject(new Error(`未登录,请先登录或采用 token 免密登陆。code:${t.data.code},msg: ${t.data.msg}`))}return Promise.reject(e)}));class M extends r.EventEmitter{constructor(e){super(),this.core=e.core}get axios(){return e=this.core,E=e,o;var e}getProxyConfig(){const{proxy:e}=this.core.getGlobalConfig();if(e){const t=new URL(e);return{host:t.hostname,port:t.port,protocol:t.protocol}}return!1}async request(e){const{method:t,url:s,data:r,config:o={}}=e;return await this.axios[t](s,r,{...o,proxy:this.getProxyConfig()})}handleHttpStatus(e){if(200!==e.status)throw new Error(`request fail, code: ${e.status}, errMsg: ${e.status}`);return e}handleStatusCode(e){const{data:t}=e;if(200!==t.statusCode)throw new Error(`request fail, code: ${t.statusCode}, errMsg: ${t.errorMsg}`);return t.data}handleCode(e){const{data:t}=e;if(0!==t.code&&0!==t.error_code)throw new Error(`request fail, code: ${t.code||t.error_code}, errMsg: ${t.msg}`);return t.data}}const U=require("path"),D=require("fs-extra"),O=e=>{if(!D.existsSync(e))return"";let t="";try{t=JSON.stringify(D.readJSONSync(e))}catch(t){console.warn(`readJson error: ${e}`)}return t};class $ extends M{constructor(e){super(e),this.getUploadToken=async e=>{const t=await this.request({method:"get",url:`${y.BUSINESS_HOST}/api/eros/mp/red/code/upload/permit`,data:{params:e,withCredentials:!0}});return this.handleCode(this.handleHttpStatus(t))},this.project=e.project}async checkUploadAuthV1({version:e}){const t=this.project,s=t.entryJsonPath,r=U.resolve(t.extJsonPath),o=O(s),i=O(r),a=t.projectAppId,n=`${y.BUSINESS_HOST}/api/eros/mp/red/code/${a}/validate/version/${e}`,l=await this.request({method:"post",url:n,data:{app_json:o,ext_json:i}});return this.handleCode(this.handleHttpStatus(l))}async checkUploadAuthV2(e){const{version:t,appId:s,cliToken:r,nonce:o}=e,i=this.project,a=i.entryJsonPath,n=U.resolve(i.extJsonPath),l=O(a),p=O(n),c=`${y.BUSINESS_HOST}/api/eros/mp/red/code/v2/validate/version?app_id=${s}&cli_token=${r}&nonce=${o}&version=${t}`,h=await this.request({method:"post",url:c,data:{app_json:l,ext_json:p}});return this.handleCode(this.handleHttpStatus(h))}async createUploadTaskV1(e){const{version:t,fileUrl:s,userVersion:r,userDesc:o,appId:i,packageMap:a,packageMapV2:n}=e,l=`${y.BUSINESS_HOST}/api/eros/mp/red/code/${i}/upload/version/${t}`,p=await this.request({method:"post",url:l,data:{file_url:s,user_version:r,user_desc:o,package_map:a,package_map_v2:n,cloud_type:4}});return this.handleCode(this.handleHttpStatus(p))}async createUploadTaskV2(e){const{version:t,fileUrl:s,userVersion:r,userDesc:o,appId:i,packageMap:a,packageMapV2:n,cliToken:l,nonce:p}=e,c=`${y.BUSINESS_HOST}/api/eros/mp/red/code/v2/upload/version?app_id=${i}&cli_token=${l}&nonce=${p}&version=${t}`,h=await this.request({method:"post",url:c,data:{file_url:s,user_version:r,user_desc:o,package_map:a,package_map_v2:n,cloud_type:4}});return this.handleCode(this.handleHttpStatus(h))}async queryTaskDetailV1(e){const{taskId:t}=e,s=await this.request({method:"get",url:`${y.BUSINESS_HOST}/api/eros/mp/red/code/task/detail`,data:{params:{task_id:t}}});return this.handleCode(this.handleHttpStatus(s))}async queryTaskDetailV2(e){const{taskId:t,appId:s,cliToken:r,nonce:o}=e,i=`${y.BUSINESS_HOST}/api/eros/mp/red/code/v2/task/detail`,a=await this.request({method:"get",url:i,data:{params:{task_id:t,app_id:s,cli_token:r,nonce:o}}});return this.handleCode(this.handleHttpStatus(a))}}const A=(e,t)=>e.reduce(((e,s)=>{let r={};return s.type===t&&(r={[s.root]:s.fileId}),{...e,...r}}),{});class H extends r{constructor(e){super(),this.uploadFile=(e,t,r,o)=>{const{zipPath:i,type:a,root:n}=t,{appId:l,cliToken:c,nonce:h}=this.getTokenParams(),u=new s.Uploader({bizName:"fe",scene:"fe-platform",getToken:t=>e.getUploadToken({...t,app_id:l,nonce:h,cli_token:c})});return new p.Observable((e=>{setTimeout((()=>{const t=`${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(e=>{const t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)}))}-xhs-example.zip`,s=v.readFileSync(f.resolve(i)),r=v.statSync(f.resolve(i));function l(){return new Promise(((e,l)=>{setTimeout((()=>{u.post({Body:s,fileInfo:{name:t,type:"application/octet-stream",size:r.size},FilePath:f.resolve(i)}).then((t=>{var s;return 0!==t.code?l(new Error(`${i}上传失败:${t.msg}`)):(null===(s=null==t?void 0:t.data)||void 0===s?void 0:s.fileId)?(e({fileId:t.data.fileId,type:a,root:n}),void(null==o||o())):l(new Error(`${i}上传失败`))}))}),500)}))}l().catch(l).catch(l).catch(l).catch(l).then((t=>{e.next(t),e.complete()})).catch((t=>{e.error(t)}))}),500)}))},this.submitTask=e=>this.isTokenAuth()?this.submitTaskV2(e):this.submitTaskV1(e),this.submitTaskV1=e=>{const{uploadApi:t,uploadRes:s,appId:r,version:o,userDesc:i,userVersion:a}=e,n=s.filter((e=>"v0"===e.type))[0].fileId,l=A(s,"v1"),h=A(s,"v2");return p.from(t.createUploadTaskV1({version:o,fileUrl:n,appId:r,packageMap:l,packageMapV2:c.isEmpty(h)?void 0:h,userDesc:i,userVersion:a})).pipe(p.switchMap((e=>{const{task_id:s}=e;return p.interval(1500).pipe(p.switchMap((()=>p.from(t.queryTaskDetailV1({taskId:s})).pipe(p.map((e=>{var t;if(5===e.status)throw new Error(null===(t=null==e?void 0:e.result)||void 0===t?void 0:t.msg);return e}))))),p.first((e=>3===e.status)))})))},this.submitTaskV2=e=>{const{uploadApi:t,uploadRes:s,version:r,userDesc:o,userVersion:i}=e,a=s.filter((e=>"v0"===e.type))[0].fileId,{appId:n,cliToken:l,nonce:h}=this.getTokenParams(),u=A(s,"v1"),d=A(s,"v2");return p.from(t.createUploadTaskV2({version:r,fileUrl:a,appId:n,packageMap:u,packageMapV2:c.isEmpty(d)?void 0:d,userDesc:o,userVersion:i,cliToken:l,nonce:h})).pipe(p.switchMap((e=>{const{task_id:s}=e;return p.interval(1500).pipe(p.switchMap((()=>p.from(t.queryTaskDetailV2({taskId:s,appId:n,cliToken:l,nonce:h})).pipe(p.map((e=>{var t;if(5===e.status)throw new Error(null===(t=null==e?void 0:e.result)||void 0===t?void 0:t.msg);return e}))))),p.first((e=>3===e.status)))})))},this.core=e.core,this.project=e.project,this.compiler=e.compiler,this.logger=e.logger}isTokenAuth(){const e=this.project.projectAppId;return!!this.core.getAppConfig(e).token}getTokenParams(){const e=this.project.projectAppId,t=this.core.getAppConfig(e).token,s="xxxxxxxxxxxxxxxx".replace(/[xy]/g,(e=>{const t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})),r=function(e){const t=C.createHash("md5");return t.update(e),t.digest("hex")}(e+"#"+t+"#"+s+"#"+h().format("YYYYMMDDHHmm"));return{cliToken:r,nonce:s,appId:e}}getCheckUploadAuthFn(e,t){let s;const r=this.isTokenAuth();if(this.logger.log("Check appid auth..."),r){const{appId:r,cliToken:o,nonce:i}=this.getTokenParams();s=e.checkUploadAuthV2({version:t,appId:r,cliToken:o,nonce:i})}else s=e.checkUploadAuthV1({version:t});return s}}class L extends H{constructor(t){super(t),this.doUpload=t=>{const{progressCallback:s,userVersion:r,userDesc:o}=t;if(!(null==r?void 0:r.trim()))throw new Error("请填写上传版本号");if(!(null==o?void 0:o.trim()))throw new Error("请填写版本描述");const i=()=>{var t,s;this.compiler.kill(e.COMPILE_ENTRY.upload),null===(t=this.taskSubscription)||void 0===t||t.unsubscribe(),this.taskSubscription=null,null===(s=this.uploadPromise)||void 0===s||s.resolve(null),this.uploadPromise=null,this.logger.log("Upload Done!!!")},a=t=>{var r,o;null==s||s(-1),null===(r=this.taskSubscription)||void 0===r||r.unsubscribe(),this.taskSubscription=null,this.compiler.kill(e.COMPILE_ENTRY.upload),this.logger.log("Upload Error:",t.toString()),null===(o=this.uploadPromise)||void 0===o||o.reject(t),this.uploadPromise=null};try{const t=this.project.projectAppId;this.taskSubscription=p.from(this.getCheckUploadAuthFn(this.uploadApi,"develop")).pipe(p.switchMap((t=>{let o=t.can_upload,i=t.can_upload_ext_json,a=t.upload_app_id;if(!o)throw new Error(t.show_msg);if(!a)throw new Error("请检查是否该appid无权限");return null==s||s(5),p.from(this.compiler.compileAndZip({entryType:e.COMPILE_ENTRY.upload,can_upload_ext_json:i,upload_app_id:a,pkgInfo:{version:r}}))})),p.switchMap((e=>{null==s||s(30),this.emit("compile-and-zip-status",{status:"uploading"}),this.logger.log("Upload packages to cos...");const t=e.map(((t,r)=>this.uploadFile(this.uploadApi,{zipPath:t.zipPath,type:t.type,root:t.root},r,(()=>{null==s||s(30+45/e.length*(r+1))}))));return p.forkJoin(t)})),p.switchMap((e=>{null==s||s(80),this.logger.log("Upload packages info...");const i={uploadApi:this.uploadApi,uploadRes:e,appId:t,version:"develop",userVersion:r,userDesc:o};return this.submitTask(i)}))).subscribe({next:e=>{null==s||s(100),i()},error:e=>{a(e)}})}catch(e){a(e)}},this.uploadApi=new $(t)}upload(e){return new Promise(((t,s)=>{this.uploadPromise={resolve:t,reject:s},this.doUpload(e)}))}}class V extends M{async generateQrCode(){const e=await this.request({method:"post",url:_.generateQrCode,data:{subsystem:"business"}});return this.handleStatusCode(this.handleHttpStatus(e))}async queryQrCodeStatus(e){const t=await this.request({method:"get",url:_.queryQrCodeStatus,data:{params:{qrId:e,service:y.CUSTOMER_SERVICE_ID}}});return this.handleStatusCode(this.handleHttpStatus(t))}async login(e){const t=await this.request({method:"post",url:_.session,data:{ticket:e,clientId:y.CUSTOMER_SERVICE_ID}});return this.handleHttpStatus(t).data}async fetchUserInfo(){const e=await this.request({method:"get",url:_.getUserInfo});return this.handleCode(this.handleHttpStatus(e))}}var z=new class{constructor(){if(this.data={},this.jsonPath=i.join(T,"storage.json"),n.existsSync(this.jsonPath))try{this.data=n.readJSONSync(this.jsonPath)}catch(e){this.data={}}}get(e){return this.data[e]}set(e,t){this.data[e]=t,this.refresh()}refresh(){n.writeJSONSync(this.jsonPath,this.data)}};class N{get isLogin(){return this._isLogin}set isLogin(e){this._isLogin=e}get userInfo(){return this._userInfo}set userInfo(e){this._userInfo=e,z.set("xhs-cli-userInfo",e)}constructor(e){var t;this.qrCodeStatusData={},this.startPollingSub=null,this.loginPromise={},this.transformUserInfo=e=>{const{nick:t,avatar:s,desc:r,gender:o}=(i=e,["nick","avatar","desc","gender"].reduce(((e,t)=>({...e,[t]:i[t]})),{}));var i;return{nickName:t,avatar:s,userId:"",desc:r,gender:o}},this.queryQrCodeStatus=e=>p.from(this.userApi.queryQrCodeStatus(e)),this.doLogin=async e=>{const{resolve:t,reject:s}=this.loginPromise;this.loginPromise=null;try{const r=await this.userApi.login(e);if(!r.success)return void s(new Error(r.msg));const o=await this.userApi.fetchUserInfo(),i=this.transformUserInfo(o);i.userInfoData=o,t(i)}catch(e){return void s(new Error(e.message))}},this.startPolling=()=>(this.qrCodeStatusData.isLoading=!0,this.qrCodeStatusData.expired=!1,p.from(p.defer((()=>this.userApi.generateQrCode()))).pipe(u.tap((e=>{this.qrUrl=e.qrUrl,this.qrCodeStatusData.isLoading=!1,this.showQrCode(),setTimeout((()=>{this.qrCodeStatusData.expired=!0,this.stopPolling()}),6e4)})),u.exhaustMap((({qrUrl:e,qrId:t})=>p.interval(2e3).pipe(u.switchMap((()=>this.queryQrCodeStatus(t))),u.tap((e=>{if(4===e.status)throw new Error("retry login");1===e.status&&(this.stopPolling(),this.doLogin(e.ticket))}))))),u.takeWhile((e=>1!==e.status)),u.retry(5))),this.stopPolling=()=>{this.startPollingSub&&(this.startPollingSub.unsubscribe(),this.startPollingSub=null)},this.logger=e.logger,this.userApi=new V(e),this.qrCodeStatusData={isLoading:!1,expired:!1},this._userInfo=z.get("userInfo")||z.get("xhs-cli-userInfo")||{},this.isLogin=!!(null===(t=this.userInfo)||void 0===t?void 0:t.nickName)}showQrCode(){d.generate(this.qrUrl,{small:!0})}async login(e="qrcode"){if(this.isLogin)this.logger.log("Had Login!!!");else{this.isLogin=!1,this.logger.log("Login...");try{if("qrcode"!==e)throw new Error("暂不支持扫码之外的其他登陆方式");{const e=await this.loginByQrcode();this.userInfo=e}this.isLogin=!0,this.logger.log("Login success")}catch(e){throw this.logger.log("Login fail:",e.message),this.isLogin=!1,this.userInfo=null,e}}}loginByQrcode(){return new Promise(((e,t)=>{this.loginPromise={resolve:e,reject:t};try{this.startPollingSub=this.startPolling().subscribe()}catch(e){console.error(e)}}))}logout(e){"axios"!==e.from&&this.logger.log("Logout..."),this.isLogin=!1,this.userInfo={},q.clear(),"axios"!==e.from&&this.logger.log("Logout Success!!!")}}const R=async(e,t={},s,r)=>{let o=e.getCompileEntryPage();o={...o,...t};const{path:i,query:a,launchMode:n}=o;try{let e=await(async e=>{const t=Buffer.from(e.replace(/^data:image\/[a-z]+;base64,/,""),"base64"),s=await g.read(t),r={data:new Uint8ClampedArray(s.bitmap.data),width:s.bitmap.width,height:s.bitmap.height},o=m(r.data,r.width,r.height);if(!o)throw new Error("QR code not found in the image.");return o.data})(s);return i&&(e=e.replace("_develop",`_develop${i.startsWith("/")?"":"/"}${i}`)),a&&(e+=`&${a}`),"halfPageNativeFunctionalized"===n&&(e+="&runtime_mode=2"),e}catch(e){r.error("二维码解码失败:",e)}};class B extends H{constructor(e){super(e),this.progressData={},this.previewData={},this.calcSize=e=>{var t,s;const r=this.project,o=(null===(t=null==r?void 0:r.flags)||void 0===t?void 0:t.enableV2Preivew)?"v2":"v1",i=(null===(s=null==e?void 0:e.filter)||void 0===s?void 0:s.call(e,(e=>e.type===o)).sort(((e,t)=>t.zipSize-e.zipSize)))||[];this.previewData.subPkgs=i.map(((e,t)=>({key:t,path:e.root,size:I(e.zipSize)})));const a=i.reduce(((e,t)=>t.zipSize+e),0)||0;this.previewData.zipSize=I(a)},this.uploadApi=new $(e),this.progressData={},this.previewData={}}preview(e){return new Promise(((t,s)=>{this.previewPromise={resolve:t,reject:s},this.doPreview(e)}))}doPreview(t){this.progressData.previewIsLoading=!0;const s=this.project.projectAppId;this.isTokenAuth();const r=Date.now(),o=async s=>{var o,i;const a=JSON.parse(s.result.extra_json);this.previewData.qrcodeUrl=await R(this.project,t.entry,a.qrcode,this.logger),this.showQrCode(this.previewData.qrcodeUrl),this.previewData.previewTotalTime=+((Date.now()-r)/1e3).toFixed(1),this.progressData.previewIsLoading=!1,this.compiler.kill(e.COMPILE_ENTRY.preview),null===(o=this.taskSubscription)||void 0===o||o.unsubscribe(),this.taskSubscription=null,null===(i=this.previewPromise)||void 0===i||i.resolve({qrcodeUrl:this.previewData.qrcodeUrl}),this.previewPromise=null,this.logger.log("Preview Done!!!")},i=t=>{var s,r;this.compiler.kill(e.COMPILE_ENTRY.preview),null===(s=this.taskSubscription)||void 0===s||s.unsubscribe(),this.taskSubscription=null,null===(r=this.previewPromise)||void 0===r||r.reject(t),this.previewPromise=null,this.logger.log("Preview Error:",t.toString())};this.taskSubscription=p.from(this.getCheckUploadAuthFn(this.uploadApi,"debug")).pipe(p.switchMap((t=>{let s=t.can_upload_ext_json,r=t.upload_app_id;if(!r)throw new Error("请检查是否该appid无权限");return p.from(this.compiler.compileAndZip({entryType:e.COMPILE_ENTRY.preview,can_upload_ext_json:s,upload_app_id:r}))})),p.tap((e=>{this.calcSize(e)})),p.switchMap((e=>{this.emit("compile-and-zip-status",{status:"uploading"}),this.logger.log("Upload packages to cos...");const t=e.map(((e,t)=>this.uploadFile(this.uploadApi,{zipPath:e.zipPath,type:e.type,root:e.root},t)));return p.forkJoin(t)})),p.switchMap((e=>{this.logger.log("Upload packages info...");const t={uploadApi:this.uploadApi,uploadRes:e,appId:s,version:"debug"};return this.submitTask(t)}))).subscribe({next:e=>o(e),error:e=>i(e)})}showQrCode(e){d.generate(e,{small:!0})}}exports.Core=class{constructor(){this.globalConfig={},this.appConfigMap={},this.projectMap={},this.compilerMap={},this.logger=new s.Logger("[xhs-mp-cli]"),this.globalConfig=z.get("xhs-cli-global-config")||{},this.appConfigMap=z.get("xhs-cli-app-config")||{},globalThis.logger=this.logger,this.loginManager=new N({core:this,logger:this.logger})}setGlobalConfig(e={}){this.logger.log("Set global config..."),this.globalConfig={...this.globalConfig||{},...e},z.set("xhs-cli-global-config",e),this.logger.log("Set global config done")}getGlobalConfig(){return this.globalConfig||{}}setAppConfig(e={}){const{appId:t,config:s}=e;t?(this.appConfigMap[t]={...this.appConfigMap[t]||{},...s},z.set("xhs-cli-app-config",this.appConfigMap),this.logger.log("Set app config done")):this.logger.error("没有传递 appId 参数")}getAppConfig(e){return this.appConfigMap[e]||{}}getProject(e){const{projectPath:s}=e;if(!s)throw new Error("没有配置projectPath");return this.projectMap[s]||(this.projectMap[s]=new t.Project(e)),this.projectMap[s]}getCompiler(t,s){const{projectPath:r}=t;if(!this.compilerMap[r]){const o=this.getProject(t);this.compilerMap[r]=new e.Compiler({...s,project:o,logger:this.logger})}return this.compilerMap[r]}async login(e={}){e.verbose&&this.logger.setVerbose(!0);const{type:t="qrcode"}=e;await this.loginManager.login(t)}logout(e){this.loginManager.logout(e)}async preview(e){e.verbose&&this.logger.setVerbose(!0);const t=e.project||{},s=e.buildConfig||{},r=this.getProject(t),o=this.appConfigMap[r.projectAppId];(null==o?void 0:o.token)||this.loginManager.isLogin||await this.login();const i=this.getCompiler(t,s),a=new B({core:this,project:r,compiler:i,logger:this.logger});return await a.preview({entry:e.entry})}async upload(e){e.verbose&&this.logger.setVerbose(!0);const{project:t,buildConfig:s={},version:r,desc:o,progressCallback:i}=e,a=this.getProject(t),n=this.appConfigMap[a.projectAppId];(null==n?void 0:n.token)||this.loginManager.isLogin||await this.login();const l=this.getCompiler(t,s),p=new L({core:this,project:a,compiler:l,logger:this.logger});return await p.upload({userVersion:r,userDesc:o,progressCallback:i})}};
1
+ "use strict";var e=require("xhs-mp-compiler-cli"),t=require("xhs-mp-project"),s=require("xhs-mp-utils"),r=require("events"),o=require("axios"),i=require("path"),a=require("os"),n=require("fs-extra"),l=require("fs"),p=require("rxjs"),c=require("lodash"),h=require("dayjs"),u=require("rxjs/operators"),d=require("qrcode-terminal"),g=require("jimp"),m=require("jsqr"),S=require("process");function f(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(s){if("default"!==s){var r=Object.getOwnPropertyDescriptor(e,s);Object.defineProperty(t,s,r.get?r:{enumerable:!0,get:function(){return e[s]}})}})),t.default=e,Object.freeze(t)}var v=f(i),w=f(l);const k=()=>({isSit:"false"===process.env.XHS_IS_PROD,isProd:"true"===process.env.XHS_IS_PROD}),x=require("process").env.XHS_BUSINESS_SIT_HOST||"http://business-cli.sl.beta.xiaohongshu.com",y=require("process").env.XHS_CUSTOMER_SIT_HOST||"https://customer.sit.xiaohongshu.com",_={BUSINESS_HOST:k().isSit?x:"https://business.xiaohongshu.com",CUSTOMER_HOST:k().isSit?y:"https://customer.xiaohongshu.com",CUSTOMER_SERVICE_ID:k().isSit?y.replace(/https?:\/\/customer/,"file://miniapp-open"):"file://miniapp-open.xiaohongshu.com"},C={generateQrCode:`${_.CUSTOMER_HOST}/api/cas/generateQrCode`,queryQrCodeStatus:`${_.CUSTOMER_HOST}/api/cas/queryQrCodeStatus`,session:`${_.BUSINESS_HOST}/api/eros/mp/red/code/session`,getUserInfo:`${_.BUSINESS_HOST}/api/eros/mp/red/code/user/info`},b=require("crypto");require("net");const I=e=>{if(!e)return"";const t=1024;return e<t?`${e}B`:e<1048576?`${(e/t).toFixed(2)}KB`:e<t**3?`${(e/1048576).toFixed(2)}MB`:e<t**4?`${(e/t**3).toFixed(2)}GB`:`${(e/t**4).toFixed(2)}T`};function P(e){return e.replace(/\\/g,"/")}const j=P(i.join(a.homedir(),".xhs-mp-cli"));n.ensureDirSync(j);const T=P(i.join(j,"xhs-storage"));n.ensureDirSync(T);const q=require("tough-cookie");var E=new class{constructor(){this.cookiePath=i.join(T,"cookie.json");const e=this.getSerializeData();Object.keys(e).length?this.cookieJar=q.CookieJar.deserializeSync(e):this.cookieJar=new q.CookieJar}getSerializeData(){let e={};return n.existsSync(this.cookiePath)&&(e=n.readJSONSync(this.cookiePath)),e}save(){const e=this.cookieJar.serializeSync();n.writeJSONSync(this.cookiePath,e)}clear(){n.removeSync(this.cookiePath)}set(e){const t=e.headers["set-cookie"]||[],s=e.config.url;t.forEach((e=>{const t=q.Cookie.parse(e);if(t.domain){const e=new q.Cookie(t);this.cookieJar.setCookieSync(e,s)}})),this.save()}get(e){return this.cookieJar.getCookieStringSync(e)}};let M;o.defaults.baseURL=_.BUSINESS_HOST,o.interceptors.request.use((e=>{const{url:t,data:s,baseURL:r=""}=e;let i=o.getUri(e);if(/^https?:/.test(i))try{const e=new URL(t);i=i.replace(e.origin,"")}catch(e){i=t}const a=E.get(t);return e.headers={...e.headers,Cookie:a},e.headers["User-Agent"]="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) @xhs/mp-ide/2.0.0 Chrome/116.0.5845.228 Electron/26.4.0 Safari/537.36 discover/2.0.0 xhsminiapp/latest",e})),o.interceptors.response.use((e=>(E.set(e),e))),o.interceptors.response.use((e=>{var t;const{config:s,data:r}=e;if((null===(t=s.url)||void 0===t?void 0:t.startsWith(`${_.BUSINESS_HOST}/api/eros/mp/red`))&&!r.success)throw new Error(`${r.msg}`);return e}),(async e=>{const{response:t={}}=e;if(401===t.status){if(t.data&&-240309===t.data.code)return null==M||M.logout({from:"axios"}),Promise.reject(new Error("登陆失效,请重新登陆或采用 token 免密登陆:",t.data.msg));if(t.data&&t.data.msg)return null==M||M.logout({from:"axios"}),Promise.reject(new Error(`未登录,请先登录或采用 token 免密登陆。code:${t.data.code},msg: ${t.data.msg}`))}return Promise.reject(e)}));class U extends r.EventEmitter{constructor(e){super(),this.core=e.core}get axios(){return e=this.core,M=e,o;var e}getProxyConfig(){const{proxy:e}=this.core.getGlobalConfig();if(e){const t=new URL(e);return{host:t.hostname,port:t.port,protocol:t.protocol}}return!1}async request(e){const{method:t,url:s,data:r,config:o={}}=e;return await this.axios[t](s,r,{...o,proxy:this.getProxyConfig()})}handleHttpStatus(e){if(200!==e.status)throw new Error(`request fail, code: ${e.status}, errMsg: ${e.status}`);return e}handleStatusCode(e){const{data:t}=e;if(200!==t.statusCode)throw new Error(`request fail, code: ${t.statusCode}, errMsg: ${t.errorMsg}`);return t.data}handleCode(e){const{data:t}=e;if(0!==t.code&&0!==t.error_code)throw new Error(`request fail, code: ${t.code||t.error_code}, errMsg: ${t.msg}`);return t.data}}const D=require("path"),O=require("fs-extra"),A=e=>{if(!O.existsSync(e))return"";let t="";try{t=JSON.stringify(O.readJSONSync(e))}catch(t){console.warn(`readJson error: ${e}`)}return t};class $ extends U{constructor(e){super(e),this.getUploadToken=async e=>{const t=await this.request({method:"get",url:`${_.BUSINESS_HOST}/api/eros/mp/red/code/upload/permit`,data:{params:e,withCredentials:!0}});return this.handleCode(this.handleHttpStatus(t))},this.project=e.project}async checkUploadAuthV1({version:e}){const t=this.project,s=t.entryJsonPath,r=D.resolve(t.extJsonPath),o=A(s),i=A(r),a=t.projectAppId,n=`${_.BUSINESS_HOST}/api/eros/mp/red/code/${a}/validate/version/${e}`,l=await this.request({method:"post",url:n,data:{app_json:o,ext_json:i}});return this.handleCode(this.handleHttpStatus(l))}async checkUploadAuthV2(e){const{version:t,appId:s,cliToken:r,nonce:o}=e,i=this.project,a=i.entryJsonPath,n=D.resolve(i.extJsonPath),l=A(a),p=A(n),c=`${_.BUSINESS_HOST}/api/eros/mp/red/code/v2/validate/version?app_id=${s}&cli_token=${r}&nonce=${o}&version=${t}`,h=await this.request({method:"post",url:c,data:{app_json:l,ext_json:p}});return this.handleCode(this.handleHttpStatus(h))}async createUploadTaskV1(e){const{version:t,fileUrl:s,userVersion:r,userDesc:o,appId:i,packageMap:a,packageMapV2:n}=e,l=`${_.BUSINESS_HOST}/api/eros/mp/red/code/${i}/upload/version/${t}`,p=await this.request({method:"post",url:l,data:{file_url:s,user_version:r,user_desc:o,package_map:a,package_map_v2:n,cloud_type:4}});return this.handleCode(this.handleHttpStatus(p))}async createUploadTaskV2(e){const{version:t,fileUrl:s,userVersion:r,userDesc:o,appId:i,packageMap:a,packageMapV2:n,cliToken:l,nonce:p}=e,c=`${_.BUSINESS_HOST}/api/eros/mp/red/code/v2/upload/version?app_id=${i}&cli_token=${l}&nonce=${p}&version=${t}`,h=await this.request({method:"post",url:c,data:{file_url:s,user_version:r,user_desc:o,package_map:a,package_map_v2:n,cloud_type:4}});return this.handleCode(this.handleHttpStatus(h))}async queryTaskDetailV1(e){const{taskId:t}=e,s=await this.request({method:"get",url:`${_.BUSINESS_HOST}/api/eros/mp/red/code/task/detail`,data:{params:{task_id:t}}});return this.handleCode(this.handleHttpStatus(s))}async queryTaskDetailV2(e){const{taskId:t,appId:s,cliToken:r,nonce:o}=e,i=`${_.BUSINESS_HOST}/api/eros/mp/red/code/v2/task/detail`,a=await this.request({method:"get",url:i,data:{params:{task_id:t,app_id:s,cli_token:r,nonce:o}}});return this.handleCode(this.handleHttpStatus(a))}}const H=(e,t)=>e.reduce(((e,s)=>{let r={};return s.type===t&&(r={[s.root]:s.fileId}),{...e,...r}}),{});class L extends r{constructor(e){super(),this.uploadFile=(e,t,r,o)=>{const{zipPath:i,type:a,root:n}=t,{appId:l,cliToken:c,nonce:h}=this.getTokenParams(),u=new s.Uploader({bizName:"fe",scene:"fe-platform",getToken:t=>e.getUploadToken({...t,app_id:l,nonce:h,cli_token:c})});return new p.Observable((e=>{setTimeout((()=>{const t=`${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(e=>{const t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)}))}-xhs-example.zip`,s=w.readFileSync(v.resolve(i)),r=w.statSync(v.resolve(i));function l(){return new Promise(((e,l)=>{setTimeout((()=>{u.post({Body:s,fileInfo:{name:t,type:"application/octet-stream",size:r.size},FilePath:v.resolve(i)}).then((t=>{var s;return 0!==t.code?l(new Error(`${i}上传失败:${t.msg}`)):(null===(s=null==t?void 0:t.data)||void 0===s?void 0:s.fileId)?(e({fileId:t.data.fileId,type:a,root:n}),void(null==o||o())):l(new Error(`${i}上传失败`))}))}),500)}))}l().catch(l).catch(l).catch(l).catch(l).then((t=>{e.next(t),e.complete()})).catch((t=>{e.error(t)}))}),500)}))},this.submitTask=e=>this.isTokenAuth()?this.submitTaskV2(e):this.submitTaskV1(e),this.submitTaskV1=e=>{const{uploadApi:t,uploadRes:s,appId:r,version:o,userDesc:i,userVersion:a}=e,n=s.filter((e=>"v0"===e.type))[0].fileId,l=H(s,"v1"),h=H(s,"v2");return p.from(t.createUploadTaskV1({version:o,fileUrl:n,appId:r,packageMap:l,packageMapV2:c.isEmpty(h)?void 0:h,userDesc:i,userVersion:a})).pipe(p.switchMap((e=>{const{task_id:s}=e;return p.interval(1500).pipe(p.switchMap((()=>p.from(t.queryTaskDetailV1({taskId:s})).pipe(p.map((e=>{var t;if(5===e.status)throw new Error(null===(t=null==e?void 0:e.result)||void 0===t?void 0:t.msg);return e}))))),p.first((e=>3===e.status)))})))},this.submitTaskV2=e=>{const{uploadApi:t,uploadRes:s,version:r,userDesc:o,userVersion:i}=e,a=s.filter((e=>"v0"===e.type))[0].fileId,{appId:n,cliToken:l,nonce:h}=this.getTokenParams(),u=H(s,"v1"),d=H(s,"v2");return p.from(t.createUploadTaskV2({version:r,fileUrl:a,appId:n,packageMap:u,packageMapV2:c.isEmpty(d)?void 0:d,userDesc:o,userVersion:i,cliToken:l,nonce:h})).pipe(p.switchMap((e=>{const{task_id:s}=e;return p.interval(1500).pipe(p.switchMap((()=>p.from(t.queryTaskDetailV2({taskId:s,appId:n,cliToken:l,nonce:h})).pipe(p.map((e=>{var t;if(5===e.status)throw new Error(null===(t=null==e?void 0:e.result)||void 0===t?void 0:t.msg);return e}))))),p.first((e=>3===e.status)))})))},this.core=e.core,this.project=e.project,this.compiler=e.compiler,this.logger=e.logger}isTokenAuth(){const e=this.project.projectAppId;return!!this.core.getAppConfig(e).token}getTokenParams(){const e=this.project.projectAppId,t=this.core.getAppConfig(e).token,s="xxxxxxxxxxxxxxxx".replace(/[xy]/g,(e=>{const t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})),r=function(e){const t=b.createHash("md5");return t.update(e),t.digest("hex")}(e+"#"+t+"#"+s+"#"+h().format("YYYYMMDDHHmm"));return{cliToken:r,nonce:s,appId:e}}getCheckUploadAuthFn(e,t){let s;const r=this.isTokenAuth();if(this.logger.log("Check appid auth..."),r){const{appId:r,cliToken:o,nonce:i}=this.getTokenParams();s=e.checkUploadAuthV2({version:t,appId:r,cliToken:o,nonce:i})}else s=e.checkUploadAuthV1({version:t});return s}}class V extends L{constructor(t){super(t),this.doUpload=t=>{const{progressCallback:s,userVersion:r,userDesc:o}=t;if(!(null==r?void 0:r.trim()))throw new Error("请填写上传版本号");if(!(null==o?void 0:o.trim()))throw new Error("请填写版本描述");const i=()=>{var t,s;this.compiler.kill(e.COMPILE_ENTRY.upload),null===(t=this.taskSubscription)||void 0===t||t.unsubscribe(),this.taskSubscription=null,null===(s=this.uploadPromise)||void 0===s||s.resolve(null),this.uploadPromise=null,this.logger.log("Upload Done!!!")},a=t=>{var r,o;null==s||s(-1),null===(r=this.taskSubscription)||void 0===r||r.unsubscribe(),this.taskSubscription=null,this.compiler.kill(e.COMPILE_ENTRY.upload),this.logger.log("Upload Error:",t.toString()),null===(o=this.uploadPromise)||void 0===o||o.reject(t),this.uploadPromise=null};try{const t=this.project.projectAppId;this.taskSubscription=p.from(this.getCheckUploadAuthFn(this.uploadApi,"develop")).pipe(p.switchMap((t=>{let o=t.can_upload,i=t.can_upload_ext_json,a=t.upload_app_id;if(!o)throw new Error(t.show_msg);if(!a)throw new Error("请检查是否该appid无权限");return null==s||s(5),p.from(this.compiler.compileAndZip({entryType:e.COMPILE_ENTRY.upload,can_upload_ext_json:i,upload_app_id:a,pkgInfo:{version:r}}))})),p.switchMap((e=>{null==s||s(30),this.emit("compile-and-zip-status",{status:"uploading"}),this.logger.log("Upload packages to cos...");const t=e.map(((t,r)=>this.uploadFile(this.uploadApi,{zipPath:t.zipPath,type:t.type,root:t.root},r,(()=>{null==s||s(30+45/e.length*(r+1))}))));return p.forkJoin(t)})),p.switchMap((e=>{null==s||s(80),this.logger.log("Upload packages info...");const i={uploadApi:this.uploadApi,uploadRes:e,appId:t,version:"develop",userVersion:r,userDesc:o};return this.submitTask(i)}))).subscribe({next:e=>{null==s||s(100),i()},error:e=>{a(e)}})}catch(e){a(e)}},this.uploadApi=new $(t)}upload(e){return new Promise(((t,s)=>{this.uploadPromise={resolve:t,reject:s},this.doUpload(e)}))}}class z extends U{async generateQrCode(){const e=await this.request({method:"post",url:C.generateQrCode,data:{subsystem:"business"}});return this.handleStatusCode(this.handleHttpStatus(e))}async queryQrCodeStatus(e){const t=await this.request({method:"get",url:C.queryQrCodeStatus,data:{params:{qrId:e,service:_.CUSTOMER_SERVICE_ID}}});return this.handleStatusCode(this.handleHttpStatus(t))}async login(e){const t=await this.request({method:"post",url:C.session,data:{ticket:e,clientId:_.CUSTOMER_SERVICE_ID}});return this.handleHttpStatus(t).data}async fetchUserInfo(){const e=await this.request({method:"get",url:C.getUserInfo});return this.handleCode(this.handleHttpStatus(e))}}var N=new class{constructor(){if(this.data={},this.jsonPath=i.join(T,"storage.json"),n.existsSync(this.jsonPath))try{this.data=n.readJSONSync(this.jsonPath)}catch(e){this.data={}}}get(e){return this.data[e]}set(e,t){this.data[e]=t,this.refresh()}refresh(){n.writeJSONSync(this.jsonPath,this.data)}};class R{get isLogin(){return this._isLogin}set isLogin(e){this._isLogin=e}get userInfo(){return this._userInfo}set userInfo(e){this._userInfo=e,N.set("xhs-cli-userInfo",e)}constructor(e){var t;this.qrCodeStatusData={},this.startPollingSub=null,this.loginPromise={},this.transformUserInfo=e=>{const{nick:t,avatar:s,desc:r,gender:o}=(i=e,["nick","avatar","desc","gender"].reduce(((e,t)=>({...e,[t]:i[t]})),{}));var i;return{nickName:t,avatar:s,userId:"",desc:r,gender:o}},this.queryQrCodeStatus=e=>p.from(this.userApi.queryQrCodeStatus(e)),this.doLogin=async e=>{const{resolve:t,reject:s}=this.loginPromise;this.loginPromise=null;try{const r=await this.userApi.login(e);if(!r.success)return void s(new Error(r.msg));const o=await this.userApi.fetchUserInfo(),i=this.transformUserInfo(o);i.userInfoData=o,t(i)}catch(e){return void s(new Error(e.message))}},this.startPolling=()=>(this.qrCodeStatusData.isLoading=!0,this.qrCodeStatusData.expired=!1,p.from(p.defer((()=>this.userApi.generateQrCode()))).pipe(u.tap((e=>{this.qrUrl=e.qrUrl,this.qrCodeStatusData.isLoading=!1,this.showQrCode(),setTimeout((()=>{this.qrCodeStatusData.expired=!0,this.stopPolling()}),6e4)})),u.exhaustMap((({qrUrl:e,qrId:t})=>p.interval(2e3).pipe(u.switchMap((()=>this.queryQrCodeStatus(t))),u.tap((e=>{if(4===e.status)throw new Error("retry login");1===e.status&&(this.stopPolling(),this.doLogin(e.ticket))}))))),u.takeWhile((e=>1!==e.status)),u.retry(5))),this.stopPolling=()=>{this.startPollingSub&&(this.startPollingSub.unsubscribe(),this.startPollingSub=null)},this.logger=e.logger,this.userApi=new z(e),this.qrCodeStatusData={isLoading:!1,expired:!1},this._userInfo=N.get("userInfo")||N.get("xhs-cli-userInfo")||{},this.isLogin=!!(null===(t=this.userInfo)||void 0===t?void 0:t.nickName)}showQrCode(){d.generate(this.qrUrl,{small:!0})}async login(e="qrcode"){if(this.isLogin)this.logger.log("Had Login!!!");else{this.isLogin=!1,this.logger.log("Login...");try{if("qrcode"!==e)throw new Error("暂不支持扫码之外的其他登陆方式");{const e=await this.loginByQrcode();this.userInfo=e}this.isLogin=!0,this.logger.log("Login success")}catch(e){throw this.logger.log("Login fail:",e.message),this.isLogin=!1,this.userInfo=null,e}}}loginByQrcode(){return new Promise(((e,t)=>{this.loginPromise={resolve:e,reject:t};try{this.startPollingSub=this.startPolling().subscribe()}catch(e){console.error(e)}}))}logout(e){"axios"!==e.from&&this.logger.log("Logout..."),this.isLogin=!1,this.userInfo={},E.clear(),"axios"!==e.from&&this.logger.log("Logout Success!!!")}}const B=async(e,t={},s,r)=>{let o=e.getCompileEntryPage();o={...o,...t};const{path:i,query:a,launchMode:n}=o;try{let e=await(async e=>{const t=Buffer.from(e.replace(/^data:image\/[a-z]+;base64,/,""),"base64"),s=await g.read(t),r={data:new Uint8ClampedArray(s.bitmap.data),width:s.bitmap.width,height:s.bitmap.height},o=m(r.data,r.width,r.height);if(!o)throw new Error("QR code not found in the image.");return o.data})(s);return i&&(e=e.replace("_develop",`_develop${i.startsWith("/")?"":"/"}${i}`)),a&&(e+=`&${a}`),"halfPageNativeFunctionalized"===n&&(e+="&runtime_mode=2"),e}catch(e){r.error("二维码解码失败:",e)}};class J extends L{constructor(e){super(e),this.progressData={},this.previewData={},this.calcSize=e=>{var t,s;const r=this.project,o=(null===(t=null==r?void 0:r.flags)||void 0===t?void 0:t.enableV2Preivew)?"v2":"v1",i=(null===(s=null==e?void 0:e.filter)||void 0===s?void 0:s.call(e,(e=>e.type===o)).sort(((e,t)=>t.zipSize-e.zipSize)))||[];this.previewData.subPkgs=i.map(((e,t)=>({key:t,path:e.root,size:I(e.zipSize)})));const a=i.reduce(((e,t)=>t.zipSize+e),0)||0;this.previewData.zipSize=I(a)},this.uploadApi=new $(e),this.progressData={},this.previewData={}}preview(e){return new Promise(((t,s)=>{this.previewPromise={resolve:t,reject:s},this.doPreview(e)}))}doPreview(t){this.progressData.previewIsLoading=!0;const s=this.project.projectAppId;this.isTokenAuth();const r=Date.now(),o=async s=>{var o,i;const a=JSON.parse(s.result.extra_json);this.previewData.qrcodeUrl=await B(this.project,t.entry,a.qrcode,this.logger),this.showQrCode(this.previewData.qrcodeUrl),this.previewData.previewTotalTime=+((Date.now()-r)/1e3).toFixed(1),this.progressData.previewIsLoading=!1,this.compiler.kill(e.COMPILE_ENTRY.preview),null===(o=this.taskSubscription)||void 0===o||o.unsubscribe(),this.taskSubscription=null,null===(i=this.previewPromise)||void 0===i||i.resolve({qrcodeUrl:this.previewData.qrcodeUrl}),this.previewPromise=null,this.logger.log("Preview Done!!!")},i=t=>{var s,r;this.compiler.kill(e.COMPILE_ENTRY.preview),null===(s=this.taskSubscription)||void 0===s||s.unsubscribe(),this.taskSubscription=null,null===(r=this.previewPromise)||void 0===r||r.reject(t),this.previewPromise=null,this.logger.log("Preview Error:",t.toString())};this.taskSubscription=p.from(this.getCheckUploadAuthFn(this.uploadApi,"debug")).pipe(p.switchMap((t=>{let s=t.can_upload_ext_json,r=t.upload_app_id;if(!r)throw new Error("请检查是否该appid无权限");return p.from(this.compiler.compileAndZip({entryType:e.COMPILE_ENTRY.preview,can_upload_ext_json:s,upload_app_id:r}))})),p.tap((e=>{this.calcSize(e)})),p.switchMap((e=>{this.emit("compile-and-zip-status",{status:"uploading"}),this.logger.log("Upload packages to cos...");const t=e.map(((e,t)=>this.uploadFile(this.uploadApi,{zipPath:e.zipPath,type:e.type,root:e.root},t)));return p.forkJoin(t)})),p.switchMap((e=>{this.logger.log("Upload packages info...");const t={uploadApi:this.uploadApi,uploadRes:e,appId:s,version:"debug"};return this.submitTask(t)}))).subscribe({next:e=>o(e),error:e=>i(e)})}showQrCode(e){d.generate(e,{small:!0})}}exports.Core=class{constructor(){this.globalConfig={},this.appConfigMap={},this.projectMap={},this.compilerMap={},this.logger=new s.Logger("[xhs-mp-cli]"),this.globalConfig=N.get("xhs-cli-global-config")||{},this.appConfigMap=N.get("xhs-cli-app-config")||{},globalThis.logger=this.logger,this.loginManager=new R({core:this,logger:this.logger})}setGlobalConfig(e={}){this.logger.log("Set global config..."),this.globalConfig={...this.globalConfig||{},...e},N.set("xhs-cli-global-config",e),this.logger.log("Set global config done")}getGlobalConfig(){return this.globalConfig||{}}setAppConfig(e={}){const{appId:t,config:s}=e;t?(this.appConfigMap[t]={...this.appConfigMap[t]||{},...s},N.set("xhs-cli-app-config",this.appConfigMap),this.logger.log("Set app config done")):this.logger.error("没有传递 appId 参数")}getAppConfig(e){return this.appConfigMap[e]||{}}getProject(e){let s=e.projectPath;if(!s)throw new Error("没有配置projectPath");return i.isAbsolute(s)||(s=i.join(S.cwd(),s)),this.projectMap[s]||(this.projectMap[s]=new t.Project(e)),this.projectMap[s]}getCompiler(t,s){let r=t.projectPath;if(i.isAbsolute(r)||(r=i.join(S.cwd(),r)),!this.compilerMap[r]){const o=this.getProject(t);this.compilerMap[r]=new e.Compiler({...s,project:o,logger:this.logger})}return this.compilerMap[r]}async login(e={}){e.verbose&&this.logger.setVerbose(!0);const{type:t="qrcode"}=e;await this.loginManager.login(t)}logout(e){this.loginManager.logout(e)}async preview(e){e.verbose&&this.logger.setVerbose(!0);const t=e.project||{},s=e.buildConfig||{},r=this.getProject(t),o=this.appConfigMap[r.projectAppId];(null==o?void 0:o.token)||this.loginManager.isLogin||await this.login();const i=this.getCompiler(t,s),a=new J({core:this,project:r,compiler:i,logger:this.logger});return await a.preview({entry:e.entry})}async upload(e){e.verbose&&this.logger.setVerbose(!0);const{project:t,buildConfig:s={},version:r,desc:o,progressCallback:i}=e,a=this.getProject(t),n=this.appConfigMap[a.projectAppId];(null==n?void 0:n.token)||this.loginManager.isLogin||await this.login();const l=this.getCompiler(t,s),p=new V({core:this,project:a,compiler:l,logger:this.logger});return await p.upload({userVersion:r,userDesc:o,progressCallback:i})}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xhs-mp-core",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -28,9 +28,9 @@
28
28
  "querystring": "^0.2.1",
29
29
  "rxjs": "^7.8.1",
30
30
  "tough-cookie": "^4.1.3",
31
- "xhs-mp-compiler-cli": "^1.3.1",
32
- "xhs-mp-project": "^1.2.1",
33
- "xhs-mp-utils": "^1.3.1"
31
+ "xhs-mp-compiler-cli": "^1.3.2",
32
+ "xhs-mp-project": "^1.2.2",
33
+ "xhs-mp-utils": "^1.3.2"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@rollup/plugin-commonjs": "^25.0.7",