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