UNPKG

@appshuttle.io/bell

Version:

Device and Emulator Library used in Shuttle

120 lines (102 loc) 4.85 kB
var shell = require('shelljs') var nodePath = process.execPath ? process.execPath : (which('node') || which('nodejs')) shell.config.execPath = nodePath.stdout const ANDROID_DEVICES_COMMAND = 'adb devices' const ANDROID_AVD_STRING_ID_HOLDER = '%AVD_STRING_ID%' const ANDROID_RUN_EMULATOR_COMMAND = 'emulator.exe -avd "' + ANDROID_AVD_STRING_ID_HOLDER + '" -gpu on -memory 2048' const ADNROID_CREATE_EMULATOR_COMMAND = 'avdmanager create avd --name "' + ANDROID_AVD_STRING_ID_HOLDER + '" -k "system-images;android-26; google_apis; x86"' const ANDROID_BUILDPATH_HOLDER = '%BUILDPATH%' const ANDROID_INSTALL_COMMAND = 'adb -e install ' + ANDROID_BUILDPATH_HOLDER const ANDROID_ADB_STARTAPP_BUNDLEID_HOLDER = '%APP_BUNDLE_ID%' const ANDROID_ADB_START_APP = 'adb shell am start - a android.intent.action.MAIN -n '+ ANDROID_ADB_STARTAPP_BUNDLEID_HOLDER +'/.MainActivity' class AndroidManager { constructor (params) { if (!params) throw new Error('No parameters specified') if (!params.buildPath) throw new Error('No Android build path specified') if (!params.appBundleId) throw new Error('No Android app bundle id specified') if (!params.deviceStringID) throw new Error('No Android device id specified') this.params = params this.AndroidSDKLocation = '/AppData/Local/Android/sdk/tools' this.checkAndroidEnvironment = function () { //Validate Android SDK if (!shell.which('adb')) { shell.echo('Sorry, this script requires android sdk. Download at https://developer.android.com/studio/index.html') return false } var adbDev = shell.exec(ANDROID_DEVICES_COMMAND) if (adbDev.code === 0) { var exResult = adbDev.stdout.split('\r\n\r\n') if(exResult.length <= 1) { shell.echo('Sorry, no emulator running. Follow our config guide at https://developer.android.com/studio/index.html') return false } } else { return false } return true } this.run = function () { const context = this return new Promise((resolve, reject) => { console.log('will exec adb: ' + params.buildPath) shell.cd(context.getAndroidSdkPath()) context.openDevice(context.params.deviceStringID).then(function (resultOpenEmulator) { context.installAppInDevice(context.params.buildPath).then(function (resultInstall) { context.openAppInDevice(context.params.appBundleId).then(function (resultOpenApp) { resolve() }).catch(function (error) { reject(error) }) }).catch(function (error) { reject(error) }) }).catch(function (error) { reject(error) }) }) } this.openDevice = function(deviceStringID) { return new Promise((resolve, reject) => { shell.exec(ANDROID_RUN_EMULATOR_COMMAND.replace(ANDROID_AVD_STRING_ID_HOLDER, deviceStringID), function(code, stdout, stderr) { if (code === 0) { resolve(stdout) } else { reject(stderr) } }) }) } this.installAppInDevice = function(appBuildPath) { return new Promise((resolve, reject) => { shell.exec(ANDROID_INSTALL_COMMAND.replace(ANDROID_BUILDPATH_HOLDER, appBuildPath), function(code, stdout, stderr) { if (code === 0) { resolve(stdout) } else { reject(stderr) } }) }) } this.openAppInDevice = function (appBundleId) { return new Promise((resolve, reject) => { shell.exec(ANDROID_ADB_START_APP.replace(ANDROID_ADB_STARTAPP_BUNDLEID_HOLDER, appBundleId), function(code, stdout, stderr) { if (code === 0) { resolve(stdout) } else { reject(stderr) } }) }) } this.getAndroidSdkPath = function () { //TODO get user folder with code return 'C:/Users/danie'+ this.AndroidSDKLocation } this.installDependencies = function () { //Install android sdk and adb //Create emulator } } } module.exports = AndroidManager;