2 lines
8.4 KiB
JavaScript
2 lines
8.4 KiB
JavaScript
|
|
"use strict";var e=require("debug"),t=require("licia/isWindows"),o=require("licia/getPort"),s=require("qrcode-reader"),n=require("fs"),r=require("child_process"),a=require("licia/sleep"),i=require("licia/toStr"),c=require("licia/waitUntil"),l=require("licia/concat"),u=require("licia/dateFormat"),d=require("ws"),h=require("events"),p=require("licia/uuid"),m=require("licia/stringify"),f=require("os");function w(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var g=w(e),v=w(t),y=w(o),b=w(s),E=w(n),C=w(r),M=w(a),A=w(i),q=w(c),P=w(l),H=w(u),x=w(d),T=w(p),$=w(m),j=w(f);/^win/.test(process.platform);class k extends h.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,t){this.ws.send(e,t)}close(){this.ws.close()}}const S=new Map,R=new Map,_="Connection closed";class D extends h.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.exConnectionClosed=!1,this.debug=g.default("automator:protocol:"+this.namespace),this.onMessage=e=>{var t,o,s;if(this.isAlive=!0,"true"===process.env.UNI_APP_X&&'"pong"'===e)return;const{id:n,method:r,error:a,result:i,params:c}=JSON.parse(e);if((null===(t=null==i?void 0:i.data)||void 0===t?void 0:t.length)>10240){let e=JSON.stringify({id:n,method:r,error:a,result:Object.assign(Object.assign({},i),{data:i.data.substring(0,30)+"...<more>"}),params:c});this.debug(`${H.default("yyyy-mm-dd HH:MM:ss:l")} ◀ RECV ${e}`)}else this.debug(`${H.default("yyyy-mm-dd HH:MM:ss:l")} ◀ RECV ${e}`);if(null===(o=null==i?void 0:i.method)||void 0===o?void 0:o.startsWith("on"))return void((e,t)=>{const o=S.get(e.method);(null==o?void 0:o.has(t))&&o.get(t)(e.data)})(i,n);if(null===(s=null==i?void 0:i.method)||void 0===s?void 0:s.startsWith("Socket.")){return void((e,t,o)=>{const s=R.get(t);(null==s?void 0:s.has(e))&&s.get(e)(o)})(i.method.replace("Socket.",""),i.id,i.data)}if(!n)return this.puppet.emit(r,c);const{callbacks:l}=this;if(n&&l.has(n)){const e=l.get(n);l.delete(n),a?e.reject(Error(a.message||a.detailMessage||a.errMsg)):e.resolve(i)}},this.onClose=()=>{this.callbacks.forEach((e=>{e.reject(Error(_))}))},this.transport.on("message",this.onMessage),this.transport.on("close",this.onClose)}isBroken(){return!!this.exConnectionClosed}send(e,t={},o=!0){if(o&&this.puppet.adapter.has(e))return this.puppet.adapter.send(this,e,t);if(this.exConnectionClosed)throw this.exConnectionClosed;const s=T.default(),n=$.default({id:s,method:e,params:t});return"ping"!==e&&this.debug(`${H.default("yyyy-mm-dd HH:MM:ss:l")} SEND ► ${n}`),new Promise(((e,t)=>{try{this.transport.send(n,(e=>{e&&t(Error(_))}))}catch(e){t(Error(_))}this.callbacks.set(s,{resolve:e,reject:t})})).catch((e=>{throw e.message===_&&(this.exConnectionClosed=e),e}))}dispose(){this.callbacks.clear(),this.transport.close()}startHeartbeat(){"true"===process.env.UNI_APP_X&&("android"===process.env.UNI_APP_PLATFORM?this.startXAndroidHeartbeat():"ios"===process.env.UNI_APP_PLATFORM&&this.startXIosHeartbeat())}startXAndroidHeartbeat(){const e=new Map,t=function(e){try{return require(e)}catch(t){return require(require.resolve(e,{paths:[process.cwd()]}))}}("adbkit"),o=j.default.platform();let s="",n="";"darwin"===o?(s='ps | grep "u0_a"',n="logcat -b crash | grep -C 10 io.dcloud.uniappx"):"win32"===o&&(s='dumpsys activity | findstr "Run"',n="logcat | findstr UncaughtExceptionHandler"),e.set(this.id,setInterval((async()=>{if(!this.isAlive){const r=t.createClient(),a=await r.listDevices();if(!a.length)throw Error("Device not found");const i=a[0].id,c=await r.getProperties(i);return("1"===c["ro.kernel.qemu"]||"goldfish"===c["ro.hardware"])&&"win32"===o&&(n="logcat | grep UncaughtExceptionHandler"),r.shell(i,s).then((function(e){let t,o="";e.on("data",(function(e){o+=e.toString(),t&&clearTimeout(t),t=setTimeout((()=>{o.includes("io.dcloud.uniapp")||(r.shell(i,"screencap -p /sdcard/uni-automator-screenshot-when-uniapp-un-active.png"),console
|