Skip to content

Commit 508f4e0

Browse files
committed
refactor of the module
1 parent 07a8cee commit 508f4e0

File tree

8 files changed

+5611
-221
lines changed

8 files changed

+5611
-221
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
*.log
33
.idea/
4+
build/

README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,3 @@ Advice is to install is globally with `npm install -g start-android-emulator`.
1616

1717
## Usage
1818
After installing it globally it can be used with the following command `start-android-emulator`.
19-
20-
## FAQ
21-
### No emulators are found
22-
If no emulators are found open Android Studio and add one. See Google how to do this
23-
24-
### I'm getting an error that my `ANDROID_HOME` has not been set correct
25-
When trying to start my emulator I get this log
26-
27-
```bash
28-
===========================================================
29-
/Users/wswebcreation/Library/Android/sdk/tools/emulator
30-
does not exist. Did you set your 'ANDROID_HOME' correct?
31-
===========================================================
32-
```
33-
34-
Just check the `ANDROID_HOME` variable and check if it has been set to the correct folder where the emulators would be found.

babel.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
presets: [
3+
[ '@babel/preset-env', {
4+
targets: {
5+
node: 8
6+
}
7+
} ]
8+
],
9+
};

bin/start-android-emulator.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const parentPath = resolve(cwd, '..');
1010
const dir = __dirname;
1111
const folder = cwd.replace(parentPath, '').substring(1);
1212

13-
const isProjectVersion = folder === nodeModuleName;
1413
let isLocalVersion = false;
1514

1615
let printCyan, printMagenta, printGreen;

index.js

Lines changed: 0 additions & 77 deletions
This file was deleted.

lib/index.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)