|
| 1 | +import { spawnSync, execFileSync } from 'child_process'; |
| 2 | +import { existsSync } from 'fs'; |
| 3 | +import { cyan, red } from 'chalk'; |
| 4 | +import { prompt } from 'inquirer'; |
| 5 | + |
| 6 | +const MESSAGE_TYPES = { |
| 7 | + ERROR: 'error', |
| 8 | + NOTIFY: 'notify', |
| 9 | +}; |
| 10 | + |
| 11 | +(async () => { |
| 12 | + const emulators = getEmulators(); |
| 13 | + const questions = [ |
| 14 | + { |
| 15 | + type: 'list', |
| 16 | + name: 'emulator', |
| 17 | + message: 'Which emulator do you want to start?', |
| 18 | + choices: emulators, |
| 19 | + }, |
| 20 | + { |
| 21 | + type: 'list', |
| 22 | + name: 'wipeData', |
| 23 | + message: 'Do you want to wipe data (delete all user data and copy data from the initial data file)?', |
| 24 | + choices: [ 'No', 'Yes' ], |
| 25 | + }, |
| 26 | + ]; |
| 27 | + |
| 28 | + logMessage( |
| 29 | + MESSAGE_TYPES.NOTIFY, |
| 30 | + 'Android Emulator CLI Helper', |
| 31 | + ) |
| 32 | + |
| 33 | + if (emulators.length > 0) { |
| 34 | + const answers = await prompt(questions); |
| 35 | + startEmulator(answers); |
| 36 | + } else { |
| 37 | + logMessage( |
| 38 | + MESSAGE_TYPES.ERROR, |
| 39 | + 'NO ANDROID EMULATORS SPECIFIED', |
| 40 | + ); |
| 41 | + } |
| 42 | +})(); |
| 43 | + |
| 44 | +/** |
| 45 | + * Start the emulator based on all the answers |
| 46 | + * |
| 47 | + * @param answers {object} All the answers |
| 48 | + * @param answers.emulator {string} The emulator name |
| 49 | + * @param answers.wipeData {string} If the emulator needs to be cleaned yes or no |
| 50 | + */ |
| 51 | +function startEmulator(answers) { |
| 52 | + try { |
| 53 | + // Start logging that we start |
| 54 | + logMessage( |
| 55 | + MESSAGE_TYPES.NOTIFY, |
| 56 | + `${ answers.emulator } is being started. You can close the device with 'X'-button on the toolbar of the device.`, |
| 57 | + ); |
| 58 | + |
| 59 | + // Start the emulator |
| 60 | + const runOnEmulator = spawnSync( |
| 61 | + 'emulator', |
| 62 | + [ |
| 63 | + '-avd', |
| 64 | + answers.emulator, |
| 65 | + answers.wipeData === 'Yes' ? '-wipe-data' : '-no-snapshot', |
| 66 | + ], |
| 67 | + ); |
| 68 | + console.log(runOnEmulator.stdout.toString()); |
| 69 | + |
| 70 | + // Check if there is an error and throw it |
| 71 | + const stderr = runOnEmulator.stderr.toString(); |
| 72 | + if (stderr) { |
| 73 | + new Error(stderr); |
| 74 | + } |
| 75 | + process.exit(0); |
| 76 | + } catch (e) { |
| 77 | + let errorMessage; |
| 78 | + |
| 79 | + if (e.message.includes('Cannot read property \'toString\' of null')) { |
| 80 | + errorMessage = 'It looks like the `emulator` has not been set correct in your environment variables. ' + |
| 81 | + 'Please check this article on how to fix it.' + |
| 82 | + '\n\n https://bit.ly/2XD94gV'; |
| 83 | + } else if (!existsSync(process.env.ANDROID_HOME)) { |
| 84 | + errorMessage = 'The environment variable `ANDROID_HOME` is not found. Did you set your `ANDROID_HOME` correct?' |
| 85 | + } else { |
| 86 | + errorMessage = `The error\n\n${ e }\n\n occurred and the emulator couldn't be started. !Check Google what the problem could be!`; |
| 87 | + } |
| 88 | + |
| 89 | + // Now log the error message |
| 90 | + logMessage( |
| 91 | + MESSAGE_TYPES.ERROR, |
| 92 | + errorMessage, |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * GEt all the emulators that are installed |
| 99 | + * |
| 100 | + * @return {string[]} |
| 101 | + */ |
| 102 | +function getEmulators() { |
| 103 | + return execFileSync( |
| 104 | + 'emulator', |
| 105 | + [ '-list-avds' ], |
| 106 | + { encoding: 'utf8' }, |
| 107 | + ) |
| 108 | + .replace(/\n$/, '') |
| 109 | + .split('\n'); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Print the error message |
| 114 | + * |
| 115 | + * @param {string} type |
| 116 | + * @param {string} message |
| 117 | + */ |
| 118 | +function logMessage(type, message) { |
| 119 | + const messageType = type === MESSAGE_TYPES.NOTIFY ? cyan : red; |
| 120 | + console.log(messageType(` |
| 121 | +============================================================================================================================================ |
| 122 | + |
| 123 | + ${ message } |
| 124 | + |
| 125 | +============================================================================================================================================ |
| 126 | +`)); |
| 127 | +} |
0 commit comments