"use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var e=t(require("fs")),i=t(require("debug")),s=t(require("postcss-selector-parser")),a=require("path"),r=require("util"),n=t(require("licia/dateFormat"));function c(t){t.walk(t=>{if("tag"===t.type){const e=t.value;t.value="page"===e?"body":"uni-"+e}})}const o=["Page.getElement","Page.getElements","Element.getElement","Element.getElements"];const l=require("adbkit"),h=i("automator:adb"),d=r.promisify(e.readdir),u=r.promisify(e.stat);class p{constructor(t){this.device={id:""},this.device.id=t.id,this.apk=t.executablePath,this.appid=t.appid||"HBuilder",this.package=t.package||"io.dcloud.HBuilder",this.client=l.createClient()}async init(){if(!this.device.id){const t=await this.client.listDevices();if(!t.length)throw Error("Device id is not provided");this.device.id=t[0].id}this.sdcard=(await this.shell(this.COMMAND_EXTERNAL)).trim(),h(`${n("yyyy-mm-dd HH:MM:ss:l")} init ${this.device.id} ${this.sdcard}`)}version(){return this.shell(this.COMMAND_VERSION).then(t=>{const e=t.match(/versionName=(.*)/);return e&&e.length>1?e[1]:""})}install(){return h(`${n("yyyy-mm-dd HH:MM:ss:l")} install ${this.apk}`),this.client.install(this.device.id,this.apk).then(()=>this.init())}push(t){return async function t(e){const i=await d(e);return(await Promise.all(i.map(async i=>{const s=a.resolve(e,i);return(await u(s)).isDirectory()?t(s):s}))).reduce((t,e)=>t.concat(e),[])}(t).then(e=>{const i=e.map(e=>{const i=a.join(this.DIR_WWW,a.relative(t,e));return h(`${n("yyyy-mm-dd HH:MM:ss:l")} push ${e} ${i}`),this.client.push(this.device.id,e,i)});return Promise.all(i)}).then(t=>!0)}start(){return this.exit().then(()=>this.shell(this.COMMAND_START))}exit(){return this.shell(this.COMMAND_STOP)}captureScreenshot(){return this.client.screencap(this.device.id).then(t=>new Promise(e=>{const i=[];t.on("data",(function(t){i.push(t)})),t.on("end",(function(){e(Buffer.concat(i).toString("base64"))}))}))}shouldPush(){return this.client.stat(this.device.id,this.FILE_APP_SERVICE).then(()=>!1).catch(()=>!0)}shell(t){return h(`${n("yyyy-mm-dd HH:MM:ss:l")} SEND ► ${t}`),this.client.shell(this.device.id,t).then(l.util.readAll).then(t=>{const e=t.toString();return h(`${n("yyyy-mm-dd HH:MM:ss:l")} ◀ RECV ${e}`),e})}get DIR_WWW(){return`${this.sdcard}/Android/data/${this.package}/apps/${this.appid}/www/`}get FILE_APP_SERVICE(){return`${this.sdcard}/Android/data/${this.package}/apps/${this.appid}/www/app-service.js`}get COMMAND_EXTERNAL(){return"echo $EXTERNAL_STORAGE"}get COMMAND_VERSION(){return"dumpsys package "+this.package}get COMMAND_STOP(){return"am force-stop "+this.package}get COMMAND_START(){return`am start -n ${this.package}/io.dcloud.PandoraEntry --es ${this.appid} --ez needUpdateApp false --ez reload true`}}const m=i("automator:devtool");let y,f=!1;const g={"Tool.close":{reflect:async()=>{}},"App.exit":{reflect:async()=>y.exit()},"App.enableLog":{reflect:()=>Promise.resolve()},"App.captureScreenshot":{reflect:async(t,e)=>{const i=await y.captureScreenshot(e);return m("App.captureScreenshot "+i.length),{data:i}}}};!function(t){o.forEach(e=>{t[e]=function(t){return{reflect:async(e,i)=>e(t,i,!1),params:t=>(t.selector&&(t.selector=s(c).processSync(t.selector)),t)}}(e)})}(g);const v={devtools:{name:"App",paths:[],required:["manifest.json","app-service.js"],validate:async function(t,i){if(t.platform=(t.platform||process.env.UNI_OS_NAME).toLocaleLowerCase(),Object.assign(t,t[t.platform]),y=function(t,e){return new p(e)}(t.platform,t),await y.init(),!await y.version()){if(!t.executablePath)throw Error(`app-plus->${t.platform}->executablePath is not provided`);if(!e.existsSync(t.executablePath))throw Error(t.executablePath+" not exists");f=!0}return t},create:async function(t,e,i){f&&await y.install(),(i.compiled||await y.shouldPush())&&await y.push(t),await y.start()}},adapter:g};module.exports=v;