-
Notifications
You must be signed in to change notification settings - Fork 919
feat: launch emulator during run-android #676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thymikee
merged 10 commits into
react-native-community:master
from
jayu:jaysbytes/launch_emulator
Sep 11, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8914975
feat: launch android emulator in run-android
jayu 0f0b69a
test: fix tests due to changes
jayu 0b3e574
refactor: address CR comments & improvements
jayu 06e0129
fix: refetch devices info after emualtor launch
jayu bb5d5d3
fix: remove redundant async/await
jayu 53a8907
fix: run emulator from android home path
jayu d759f1a
refactor: use interval to check if emulator is booted
jayu 7019639
fix emulator path
thymikee 20bdb48
adjust wording
thymikee aae4e6b
moar adjustments
thymikee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
packages/platform-android/src/commands/runAndroid/__mocks__/tryLaunchEmulator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default async () => true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/platform-android/src/commands/runAndroid/tryLaunchEmulator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import execa from 'execa'; | ||
import Adb from './adb'; | ||
|
||
const emulatorCommand = process.env.ANDROID_HOME | ||
? `${process.env.ANDROID_HOME}/emulator/emulator` | ||
: 'emulator'; | ||
|
||
const getEmulators = () => { | ||
try { | ||
const emulatorsOutput = execa.sync(emulatorCommand, ['-list-avds']).stdout; | ||
return emulatorsOutput.split('\n').filter(name => name !== ''); | ||
} catch { | ||
return []; | ||
} | ||
}; | ||
|
||
const launchEmulator = async (emulatorName: string, adbPath: string) => { | ||
return new Promise((resolve, reject) => { | ||
const cp = execa(emulatorCommand, [`@${emulatorName}`], { | ||
detached: true, | ||
stdio: 'ignore', | ||
}); | ||
cp.unref(); | ||
const timeout = 30; | ||
|
||
// Reject command after timeout | ||
const rejectTimeout = setTimeout(() => { | ||
cleanup(); | ||
reject(`Could not start emulator within ${timeout} seconds.`); | ||
}, timeout * 1000); | ||
|
||
const bootCheckInterval = setInterval(() => { | ||
if (Adb.getDevices(adbPath).length > 0) { | ||
cleanup(); | ||
resolve(); | ||
} | ||
}, 1000); | ||
|
||
const cleanup = () => { | ||
clearTimeout(rejectTimeout); | ||
clearInterval(bootCheckInterval); | ||
}; | ||
|
||
cp.on('exit', () => { | ||
cleanup(); | ||
reject('Emulator exited before boot.'); | ||
}); | ||
|
||
cp.on('error', error => { | ||
cleanup(); | ||
reject(error.message); | ||
}); | ||
}); | ||
}; | ||
|
||
export default async function tryLaunchEmulator( | ||
adbPath: string, | ||
): Promise<{success: boolean; error?: string}> { | ||
const emulators = getEmulators(); | ||
if (emulators.length > 0) { | ||
try { | ||
await launchEmulator(emulators[0], adbPath); | ||
return {success: true}; | ||
} catch (error) { | ||
return {success: false, error}; | ||
} | ||
} | ||
return { | ||
success: false, | ||
error: 'No emulators found as an output of `emulator -list-avds`', | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.