mh-two-thousand-and-two
2024-04-12 7fc6dbf547b8899d949b67cdec36b96a7d1701c7
1
"use strict";var e=require("debug"),t=require("licia/isWindows"),o=require("licia/getPort"),n=require("qrcode-reader"),r=require("fs"),a=require("child_process"),s=require("licia/sleep"),i=require("licia/toStr"),c=require("licia/waitUntil"),l=require("licia/concat"),u=require("licia/dateFormat"),d=require("ws"),p=require("events"),h=require("licia/uuid"),f=require("licia/stringify"),m=require("os");function w(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var v=w(e),g=w(t),y=w(o),b=w(n),E=w(r),q=w(a),M=w(s),P=w(i),$=w(c),j=w(l),C=w(u),x=w(d),A=w(h),H=w(f),k=w(m);/^win/.test(process.platform);class S extends p.EventEmitter{constructor(e){super(),this.ws=e,this.ws.addEventListener("message",(e=>{this.emit("message",e.data)})),this.ws.addEventListener("close",(()=>{this.emit("close")}))}send(e){this.ws.send(e)}close(){this.ws.close()}}const T=new Map,R=new Map,D="Connection closed";class F extends p.EventEmitter{constructor(e,t,o){super(),this.puppet=t,this.namespace=o,this.callbacks=new Map,this.transport=e,this.isAlive=!0,this.id=Date.now(),this.debug=v.default("automator:protocol:"+this.namespace),this.onMessage=e=>{var t,o;if(this.isAlive=!0,"true"===process.env.UNI_APP_X&&'"pong"'===e)return;this.debug(`${C.default("yyyy-mm-dd HH:MM:ss:l")} ◀ RECV ${e}`);const{id:n,method:r,error:a,result:s,params:i}=JSON.parse(e);if(null===(t=null==s?void 0:s.method)||void 0===t?void 0:t.startsWith("on"))return void((e,t)=>{const o=T.get(e.method);(null==o?void 0:o.has(t))&&o.get(t)(e.data)})(s,n);if(null===(o=null==s?void 0:s.method)||void 0===o?void 0:o.startsWith("Socket.")){return void((e,t,o)=>{const n=R.get(t);(null==n?void 0:n.has(e))&&n.get(e)(o)})(s.method.replace("Socket.",""),s.id,s.data)}if(!n)return this.puppet.emit(r,i);const{callbacks:c}=this;if(n&&c.has(n)){const e=c.get(n);c.delete(n),a?e.reject(Error(a.message||a.detailMessage||a.errMsg)):e.resolve(s)}},this.onClose=()=>{this.callbacks.forEach((e=>{e.reject(Error(D))}))},this.transport.on("message",this.onMessage),this.transport.on("close",this.onClose)}send(e,t={},o=!0){if(o&&this.puppet.adapter.has(e))return this.puppet.adapter.send(this,e,t);const n=A.default(),r=H.default({id:n,method:e,params:t});return"ping"!==e&&this.debug(`${C.default("yyyy-mm-dd HH:MM:ss:l")} SEND ► ${r}`),new Promise(((e,t)=>{try{this.transport.send(r)}catch(e){t(Error(D))}this.callbacks.set(n,{resolve:e,reject:t})}))}dispose(){this.transport.close()}startHeartbeat(){if("true"===process.env.UNI_APP_X&&"android"===process.env.UNI_APP_PLATFORM){const e=new Map,t=function(e){try{return require(e)}catch(t){return require(require.resolve(e,{paths:[process.cwd()]}))}}("adbkit"),o=5e3,n=k.default.platform();let r="",a="";"darwin"===n?(r='dumpsys activity | grep "Run"',a=`logcat -b crash | grep -C 10 ${"io.dcloud.uniappx"}`):"win32"===n&&(r='dumpsys activity | findstr "Run"',a="logcat | findstr UncaughtExceptionHandler"),e.set(this.id,setInterval((async()=>{if(!this.isAlive){const o=t.createClient(),n=await o.listDevices();if(!n.length)throw Error("Device not found");const s=n[0].id;return o.shell(s,r).then((function(e){let t,o="";e.on("data",(function(e){o+=e.toString(),t&&clearTimeout(t),t=setTimeout((()=>{o.includes("io.dcloud.uniapp")||console.log("Stop the test process.")}),50)}))})),o.shell(s,a).then((e=>{let t,o="";e.on("data",(e=>{o+=e.toString(),t&&clearTimeout(t),t=setTimeout((()=>{console.log(`crash log: ${o}`)}),50)}))})),clearInterval(e.get(this.id)),e.delete(this.id),void this.dispose()}this.send("ping"),this.isAlive=!1}),o))}}static createDevtoolConnection(e,t){return new Promise(((o,n)=>{const r=new x.default(e);r.addEventListener("open",(()=>{o(new F(new S(r),t,"devtool"))})),r.addEventListener("error",n)}))}static createRuntimeConnection(e,t,o){return new Promise(((n,r)=>{v.default("automator:runtime")(`${C.default("yyyy-mm-dd HH:MM:ss:l")} port=${e}`);const a=new x.default.Server({port:e});$.default((async()=>{if(t.runtimeConnection)return!0}),o,1e3).catch((()=>{a.close(),r("Failed to connect to runtime, please make sure the project is running")})),a.on("connection",(function(e){v.default("automator:runtime")(`${C.default("yyyy-mm-dd HH:MM:ss:l")} connected`);const o=new F(new S(e),t,"runtime");t.setRuntimeConnection(o),o.startHeartbeat(),n(o)})),t.setRuntimeServer(a)}))}}const L=v.default("automator:devtool");async function O(e,t,o){const{port:n,cliPath:r,timeout:a,cwd:s="",account:i="",args:c=[],launch:l=!0}=t;let u=!1,d=!1;if(!1!==l){const t={stdio:"ignore",detached:!0};s&&(t.cwd=s);let o=j.default(c,[]);o=j.default(o,["auto","--project"]),o=j.default(o,[e,"--auto-port",P.default(n)]),i&&(o=j.default(o,["--auto-account",i]));try{L("%s %o %o",r,o,t);const e=q.default.spawn(r,o,t);e.on("error",(e=>{u=!0})),e.on("exit",(()=>{setTimeout((()=>{d=!0}),15e3)})),e.unref()}catch(e){u=!1}}else setTimeout((()=>{d=!0}),15e3);const p=await $.default((async()=>{try{if(u||d)return!0;const e=await async function(e,t){let o;try{o=await F.createDevtoolConnection(e.wsEndpoint,t)}catch(t){throw Error(`Failed connecting to ${e.wsEndpoint}, check if target project window is opened with automation enabled`)}return o}({wsEndpoint:`ws://127.0.0.1:${n}`},o);return e}catch(e){}}),a,1e3);if(u)throw Error(`Failed to launch ${o.devtools.name}, please make sure cliPath is correctly specified`);if(d)throw Error(`Failed to launch ${o.devtools.name} , please make sure http port is open`);return await M.default(5e3),L(`${C.default("yyyy-mm-dd HH:MM:ss:l")} connected`),p}const W={devtools:{name:"Wechat web devTools",remote:!0,automator:!0,paths:[g.default?"C:/Program Files (x86)/Tencent/微信web开发者工具/cli.bat":"/Applications/wechatwebdevtools.app/Contents/MacOS/cli"],required:["project.config.json","app.json","app.js"],defaultPort:9420,validate:async function(e,t){const o=function(e,t){const o=t.devtools.paths.slice(0);e&&o.unshift(e);for(const e of o)if(E.default.existsSync(e))return e;throw Error(`${t.devtools.name} not found, please specify executablePath option`)}(e.executablePath,t);let n=e.port||t.devtools.defaultPort;if(!1!==e.launch)try{n=await async function(e,t){const o=await y.default(e||t);if(e&&o!==e)throw Error(`Port ${e} is in use, please specify another port`);return o}(n)}catch(t){e.launch=!1}else{n===await y.default(n)&&(e.launch=!0)}return Object.assign(Object.assign({},e),{port:n,cliPath:o})},async create(e,t,o){const n=await O(e,t,o);return o.compiled?v.default("automator:devtool")("Waiting for runtime automator"):(v.default("automator:devtool")("initRuntimeAutomator"),n.send("App.callWxMethod",{method:"$$initRuntimeAutomator",args:[]})),n}},adapter:{"Tool.enableRemoteDebug":{reflect:async(e,t)=>{let{qrCode:o}=await e("Tool.enableRemoteDebug",t,!1);return o&&(o=await function(e){const t=new Buffer(e,"base64");return new Promise((async(e,o)=>{const n=await require("jimp").read(t),r=new b.default;r.callback=function(t,n){if(t)return o(t);e(n.result)},r.decode(n.bitmap)}))}(o)),{qrCode:o}}},"App.callFunction":{reflect:async(e,t)=>{return e("App.callFunction",Object.assign(Object.assign({},t),{functionDeclaration:(o=t.functionDeclaration,"}"===o[o.length-1]?o.replace("{","{\nvar uni = wx;\n"):o.replace("=>","=>{\nvar uni = wx;\nreturn ")+"}")}),!1);var o}},"Element.getHTML":{reflect:async(e,t)=>({html:(await e("Element.getWXML",t,!1)).wxml})}}};module.exports=W;