2 lines
5.9 KiB
JavaScript
2 lines
5.9 KiB
JavaScript
|
"use strict";var e=require("debug"),t=require("licia/isWindows"),o=require("licia/getPort"),a=require("qrcode-reader"),n=require("fs"),r=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"),m=require("licia/stringify");function f(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var w=f(e),v=f(t),y=f(o),g=f(a),b=f(n),E=f(r),M=f(s),q=f(i),j=f(c),$=f(l),P=f(u),C=f(d),x=f(h),k=f(m);/^win/.test(process.platform);class H 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 S=new Map,F=new Map,R="Connection closed";class D extends p.EventEmitter{constructor(e,t,o){super(),this.puppet=t,this.namespace=o,this.callbacks=new Map,this.transport=e,this.debug=w.default("automator:protocol:"+this.namespace),this.onMessage=e=>{var t,o;this.debug(`${P.default("yyyy-mm-dd HH:MM:ss:l")} ◀ RECV ${e}`);const{id:a,method:n,error:r,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=S.get(e.method);(null==o?void 0:o.has(t))&&o.get(t)(e.data)})(s,a);if(null===(o=null==s?void 0:s.method)||void 0===o?void 0:o.startsWith("Socket.")){return void((e,t,o)=>{const a=F.get(t);(null==a?void 0:a.has(e))&&a.get(e)(o)})(s.method.replace("Socket.",""),s.id,s.data)}if(!a)return this.puppet.emit(n,i);const{callbacks:c}=this;if(a&&c.has(a)){const e=c.get(a);c.delete(a),r?e.reject(Error(r.message||r.detailMessage||r.errMsg)):e.resolve(s)}},this.onClose=()=>{this.callbacks.forEach((e=>{e.reject(Error(R))}))},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 a=x.default(),n=k.default({id:a,method:e,params:t});return this.debug(`${P.default("yyyy-mm-dd HH:MM:ss:l")} SEND ► ${n}`),new Promise(((e,t)=>{try{this.transport.send(n)}catch(e){t(Error(R))}this.callbacks.set(a,{resolve:e,reject:t})}))}dispose(){this.transport.close()}static createDevtoolConnection(e,t){return new Promise(((o,a)=>{const n=new C.default(e);n.addEventListener("open",(()=>{o(new D(new H(n),t,"devtool"))})),n.addEventListener("error",a)}))}static createRuntimeConnection(e,t,o){return new Promise(((a,n)=>{w.default("automator:runtime")(`${P.default("yyyy-mm-dd HH:MM:ss:l")} port=${e}`);const r=new C.default.Server({port:e});j.default((async()=>{if(t.runtimeConnection)return!0}),o,1e3).catch((()=>{r.close(),n("Failed to connect to runtime, please make sure the project is running")})),r.on("connection",(function(e){w.default("automator:runtime")(`${P.default("yyyy-mm-dd HH:MM:ss:l")} connected`);const o=new D(new H(e),t,"runtime");t.setRuntimeConnection(o),a(o)})),t.setRuntimeServer(r)}))}}const T=w.default("automator:devtool");async function W(e,t,o){const{port:a,cliPath:n,timeout:r,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=$.default(c,[]);o=$.default(o,["auto","--project"]),o=$.default(o,[e,"--auto-port",q.default(a)]),i&&(o=$.default(o,["--auto-account",i]));try{T("%s %o %o",n,o,t);const e=E.default.spawn(n,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 j.default((async()=>{try{if(u||d)return!0;const e=await async function(e,t){let o;try{o=await D.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:${a}`},o);return e}catch(e){}}),r,1e3);if(u)throw Error(`Failed to launch ${o.devtools.name}, please make sure cliPath is correctly specified`);if(d)throw Error(`Failed to launc
|