提交 04833949 编写于 作者: fxy060608's avatar fxy060608

feat(app-plus): automator

上级 542702fd
"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;
"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;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册