uni-ticket-system/node_modules/@dcloudio/uni-mp-baidu/lib/uni.automator.js

2 lines
8.2 KiB
JavaScript
Raw Normal View History

2023-12-05 02:11:10 +00:00
"use strict";var e=require("os"),t=require("path"),s=require("debug"),a=require("licia/isWindows"),r=require("fs"),o=require("child_process"),n=require("licia/sleep"),i=require("licia/toStr"),c=require("licia/waitUntil"),l=require("licia/concat"),u=require("licia/getPort"),p=require("licia/dateFormat"),d=require("ws"),m=require("events"),f=require("licia/uuid"),h=require("licia/stringify");function g(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var w=g(e),y=g(t),E=g(s),v=g(a),b=g(r),P=g(o),j=g(n),M=g(i),O=g(c),$=g(l),q=g(u),C=g(p),S=g(d),A=g(f),T=g(h);const L=/(^[a-z][a-z0-9-]*)/i,H=/^navigator/i,k=/^swan-nav$/i;var R;!function(e){e.SELECTOR="selector",e.TAGNAME="tagName"}(R||(R={}));const N={[R.SELECTOR]:[{test:H,processor:e=>e.replace(H,"nav")},{test:L,processor:e=>`swan-${e}`}],[R.TAGNAME]:[{test:k,processor:e=>e.replace(k,"swan-navigator")},{test:L,processor:e=>e.toLocaleLowerCase().replace("swan-","")}]},x=e=>t=>{const s=(N[e]||[]).filter((e=>e.test.test(t)));for(const e of s)t=e.processor(t);return t},D=x(R.SELECTOR),F=x(R.TAGNAME),I=e=>Object.assign({},e,{type:"id",info:{id:e.elementId}});/^win/.test(process.platform);class W extends m.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 B=new Map,G=new Map,z="Connection closed";class J extends m.EventEmitter{constructor(e,t,s){super(),this.puppet=t,this.namespace=s,this.callbacks=new Map,this.transport=e,this.debug=E.default("automator:protocol:"+this.namespace),this.onMessage=e=>{var t,s;this.debug(`${C.default("yyyy-mm-dd HH:MM:ss:l")} ◀ RECV ${e}`);const{id:a,method:r,error:o,result:n,params:i}=JSON.parse(e);if(null===(t=null==n?void 0:n.method)||void 0===t?void 0:t.startsWith("on"))return void((e,t)=>{const s=B.get(e.method);(null==s?void 0:s.has(t))&&s.get(t)(e.data)})(n,a);if(null===(s=null==n?void 0:n.method)||void 0===s?void 0:s.startsWith("Socket.")){return void((e,t,s)=>{const a=G.get(t);(null==a?void 0:a.has(e))&&a.get(e)(s)})(n.method.replace("Socket.",""),n.id,n.data)}if(!a)return this.puppet.emit(r,i);const{callbacks:c}=this;if(a&&c.has(a)){const e=c.get(a);c.delete(a),o?e.reject(Error(o.message||o.detailMessage||o.errMsg)):e.resolve(n)}},this.onClose=()=>{this.callbacks.forEach((e=>{e.reject(Error(z))}))},this.transport.on("message",this.onMessage),this.transport.on("close",this.onClose)}send(e,t={},s=!0){if(s&&this.puppet.adapter.has(e))return this.puppet.adapter.send(this,e,t);const a=A.default(),r=T.default({id:a,method:e,params:t});return 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(z))}this.callbacks.set(a,{resolve:e,reject:t})}))}dispose(){this.transport.close()}static createDevtoolConnection(e,t){return new Promise(((s,a)=>{const r=new S.default(e);r.addEventListener("open",(()=>{s(new J(new W(r),t,"devtool"))})),r.addEventListener("error",a)}))}static createRuntimeConnection(e,t,s){return new Promise(((a,r)=>{E.default("automator:runtime")(`${C.default("yyyy-mm-dd HH:MM:ss:l")} port=${e}`);const o=new S.default.Server({port:e});O.default((async()=>{if(t.runtimeConnection)return!0}),s,1e3).catch((()=>{o.close(),r("Failed to connect to runtime, please make sure the project is running")})),o.on("connection",(function(e){E.default("automator:runtime")(`${C.default("yyyy-mm-dd HH:MM:ss:l")} connected`);const s=new J(new W(e),t,"runtime");t.setRuntimeConnection(s),a(s)})),t.setRuntimeServer(o)}))}}const U=E.default("automator:devtool");async function V(e,t,s){const{port:a,cliPath:r,timeout:o,cwd:n="",account:i="",args:c=[],launch:l=!0}=t;let u=!1,p=!1;if(!1!==l){const t={stdio:"ignore"};n&&(t.cwd=n);let s=$.default(c,[]);s=$.default(s,["--auto"]),s=$.default(s,[e,"--auto-port",M.default(a)]),i&&(s=$.default(s,["--auto-account",i]));try{U("%s %o %o",r,s,t);const e=P.default.spawn(r,s,t);e.on("error",(e=>{u=!0})),e.on("exit",(()=>{setTimeout((()=>{p=!0}),15e3)})),e.unr