2 lines
8.1 KiB
JavaScript
2 lines
8.1 KiB
JavaScript
|
"use strict";var t=require("fs"),e=require("debug"),s=require("licia/sleep"),i=require("postcss-selector-parser"),a=require("fs-extra"),r=require("licia/dateFormat"),o=require("path"),n=require("util"),l=require("jimp");function c(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var d=c(t),u=c(e),h=c(s),p=c(i),y=c(a),m=c(r),f=c(l);function v(t){t.walk((t=>{if("tag"===t.type){const e=t.value;t.value="page"===e?"body":"uni-"+e}}))}u.default("automator:devtool");const M=["Page.getElement","Page.getElements","Element.getElement","Element.getElements"];const $=/^win/.test(process.platform);function g(t){try{return require(t)}catch(e){return require(require.resolve(t,{paths:[process.cwd()]}))}}const w=u.default("automator:launcher"),P=n.promisify(d.default.readdir),A=n.promisify(d.default.stat);async function E(t){const e=await P(t);return(await Promise.all(e.map((async e=>{const s=o.resolve(t,e);return(await A(s)).isDirectory()?E(s):s})))).reduce(((t,e)=>t.concat(e)),[])}class H{constructor(t){this.isX=!1,"true"===process.env.UNI_APP_X&&(this.isX=!0),this.id=t.id,this.app=t.executablePath,this.appid=t.appid||process.env.UNI_APP_ID||"HBuilder",this.package=t.package||(this.isX?"io.dcloud.uniappx":"io.dcloud.HBuilder"),this.activity=t.activity||(this.isX?"io.dcloud.uniapp.UniAppActivity":"io.dcloud.PandoraEntry")}shouldPush(){return this.exists(this.FILE_APP_SERVICE).then((()=>(w(`${m.default("yyyy-mm-dd HH:MM:ss:l")} ${this.FILE_APP_SERVICE} exists`),!1))).catch((()=>(w(`${m.default("yyyy-mm-dd HH:MM:ss:l")} ${this.FILE_APP_SERVICE} not exists`),!0)))}push(t){return E(t).then((e=>{const s=e.map((e=>{const s=(t=>$?t.replace(/\\/g,"/"):t)(o.join(this.DIR_WWW,o.relative(t,e)));return w(`${m.default("yyyy-mm-dd HH:MM:ss:l")} push ${e} ${s}`),this.pushFile(e,s)}));return Promise.all(s)})).then((t=>!0))}get FILE_APP_SERVICE(){return`${this.DIR_WWW}/app-service.js`}}const S=u.default("automator:simctl");function _(t){const e=parseInt(t);return e>9?String(e):"0"+e}class x extends H{constructor(){super(...arguments),this.bundleVersion=""}async init(){const t=g("node-simctl").Simctl;this.tool=new t({udid:this.id});try{await this.tool.bootDevice()}catch(t){}await this.initSDCard(),S(`${m.default("yyyy-mm-dd HH:MM:ss:l")} init ${this.id}`)}async initSDCard(){const t=await this.tool.appInfo(this.package);S(`${m.default("yyyy-mm-dd HH:MM:ss:l")} appInfo ${t}`);const e=t.match(/DataContainer\s+=\s+"(.*)"/);if(!e)return Promise.resolve("");const s=t.match(/CFBundleVersion\s+=\s+(.*);/);if(!s)return Promise.resolve("");this.sdcard=e[1].replace("file:",""),this.bundleVersion=s[1],S(`${m.default("yyyy-mm-dd HH:MM:ss:l")} install ${this.sdcard}`)}async version(){return Promise.resolve(this.bundleVersion)}formatVersion(t){const e=t.split(".");return 3!==e.length?t:e[0]+_(e[1])+_(e[2])}async install(){return S(`${m.default("yyyy-mm-dd HH:MM:ss:l")} install ${this.app}`),await this.tool.installApp(this.app),await this.tool.grantPermission(this.package,"all"),await this.initSDCard(),Promise.resolve(!0)}async start(){try{await this.tool.terminateApp(this.package)}catch(t){}try{await this.tool.launchApp(this.package)}catch(t){console.error(t)}return Promise.resolve(!0)}async exit(){return await this.tool.terminateApp(this.package),await this.tool.shutdownDevice(),Promise.resolve(!0)}async captureScreenshot(){return Promise.resolve(await this.tool.getScreenshot())}exists(t){return y.default.existsSync(t)?Promise.resolve(!0):Promise.reject(Error(`${t} not exists`))}pushFile(t,e){return Promise.resolve(y.default.copySync(t,e))}get DIR_WWW(){return`${this.sdcard}/Documents/Pandora/apps/${this.appid}/www/`}}const D=g("adbkit"),b=u.default("automator:adb");class I extends H{async init(){if(this.tool=D.createClient(),b(`${m.default("yyyy-mm-dd HH:MM:ss:l")} init ${await this.tool.version()}`),!this.id){const t=await this.tool.listDevices();if(!t.length)throw Error("Device not found");this.id=t[0].id}this.sdcard=(await this.shell(this.COMMAND_EXTERNAL)).trim(),b(`${m.default("yyyy-mm-dd HH:MM:ss:l")} init ${this.id} ${this.sdcard}`)}root(){return
|