Skip to content

Instantly share code, notes, and snippets.

@gorhom
Created August 14, 2019 14:26
Show Gist options
  • Save gorhom/62e43fa1c3c2207f13eda07623e7bbc5 to your computer and use it in GitHub Desktop.
Save gorhom/62e43fa1c3c2207f13eda07623e7bbc5 to your computer and use it in GitHub Desktop.
Android Devices/Emulators JS Helper
const { exec } = require('@tunnckocore/execa')
/**
* Get connected Android devices / emulators list.
* @returns {Promise<Array>} Promise<devices[]>
*/
const getConnectedAndroidDevices = () =>
exec(['adb devices'], {
preferLocal: true,
}).then(output => {
var devices = []
const regex = /(?<=\n)[0-9A-Za-z\-]+/gm
while ((m = regex.exec(output[0].stdout)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++
}
m.forEach(match => {
devices.push(match)
})
}
return Promise.resolve(devices)
})
/**
* Get created emulators list.
* @returns {Promise<Array>} Promise<emulators[]>
*/
const getAndroidEmulators = () =>
exec(['emulator -list-avds'], {
preferLocal: true,
}).then(output => {
var devices = []
const regex = /(?<=\n)[0-9A-Za-z-_]+/gm
while ((m = regex.exec(output[0].stdout)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++
}
m.forEach(match => {
devices.push(match)
})
}
return Promise.resolve(devices)
})
/**
* Start Android emulator.
* @param {string} name emulator name
* @returns {Promise<void>} Promise<void>
*/
const startAndroidEmulator = name => {
// exec(`emulator -avd ${name}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment