UNPKG

5.45 kBJavaScriptView Raw
1const path = require('path');
2const chalk = require('chalk');
3const fs = require('fs');
4const child_process = require('child_process');
5const inquirer = require('inquirer');
6const utils = require('../utils');
7
8function runAndroid(options) {
9 utils.buildJS()
10 .then(() => { return {options} })
11 .then(prepareAndroid)
12 .then(findAndroidDevice)
13 .then(chooseDevice)
14 .then(reverseDevice)
15 .then(buildDebugApp)
16 .then(installApp)
17 .then(runApp)
18 .catch((err) => {
19 if (err) {
20 console.log(chalk.red('Error:', err));
21 }
22 });
23}
24
25function prepareAndroid({options}) {
26 return new Promise((resolve, reject) => {
27 const rootPath = process.cwd();
28
29 console.log();
30 console.log(` => ${chalk.blue.bold('Will start Android app')}`);
31
32 process.chdir(path.join(rootPath, 'platforms/android/eeuiApp'));
33
34 try {
35 child_process.execSync(`adb start-server`, {encoding: 'utf8'})
36 } catch (e) {
37 reject()
38 }
39 try {
40 child_process.execSync(`adb devices`, {encoding: 'utf8'})
41 } catch (e) {
42 reject()
43 }
44 resolve({options, rootPath})
45 })
46}
47
48function findAndroidDevice({options}) {
49 return new Promise((resolve, reject) => {
50 let devicesInfo = '';
51 try {
52 devicesInfo = child_process.execSync(`adb devices`, {encoding: 'utf8'})
53 } catch (e) {
54 console.log(chalk.red(`adb devices failed, please make sure you have adb in your PATH.`));
55 console.log(`See ${chalk.cyan('http://stackoverflow.com/questions/27301960/errorunable-to-locate-adb-within-sdk-in-android-studio')}`);
56 reject()
57 }
58 let devicesList = utils.parseDevicesResult(devicesInfo);
59 resolve({devicesList, options})
60 })
61}
62
63function chooseDevice({devicesList, options}) {
64 return new Promise((resolve, reject) => {
65 if (devicesList && devicesList.length > 1) {
66 const listNames = [new inquirer.Separator(' = devices = ')];
67 for (const device of devicesList) {
68 listNames.push(
69 {
70 name: `${device}`,
71 value: device
72 }
73 )
74 }
75 inquirer.prompt([
76 {
77 type: 'list',
78 message: 'Choose one of the following devices',
79 name: 'chooseDevice',
80 choices: listNames
81 }
82 ]).then((answers) => {
83 const device = answers.chooseDevice;
84 resolve({device, options})
85 })
86 } else if (devicesList.length === 1) {
87 resolve({device: devicesList[0], options})
88 } else {
89 reject('No android devices found.')
90 }
91 });
92}
93
94function reverseDevice({device, options}) {
95 return new Promise((resolve, reject) => {
96 try {
97 let s = child_process.execSync(`adb -s ${device} reverse tcp:8080 tcp:8080`, {encoding: 'utf8'})
98 } catch (e) {
99 console.error('reverse error[ignored]');
100 resolve({device, options})
101 }
102
103 resolve({device, options})
104 })
105}
106
107function buildDebugApp({device, options}) {
108 return new Promise((resolve, reject) => {
109 console.log(` => ${chalk.blue.bold('Building app ...')}`);
110 const rootPath = process.cwd();
111 console.log('build='+rootPath);
112 let clean = options.clean ? ' clean' : '';
113 try {
114 child_process.execSync(process.platform === 'win32' ? `call gradlew.bat${clean} assembleDebug` : `./gradlew${clean} assembleDebug`, {
115 encoding: 'utf8',
116 stdio: [0, 1, 2]
117 })
118 } catch (e) {
119 reject()
120 }
121 resolve({device, options})
122 })
123}
124
125function installApp({device, options}) {
126 return new Promise((resolve, reject) => {
127 console.log(` => ${chalk.blue.bold('Install app ...')}`);
128 const rootPath = process.cwd();
129 const apkName = rootPath + '/app/build/outputs/apk/debug/app-debug.apk';
130 console.log(chalk.green('=============================================='));
131 console.log(chalk.green('=============================================='));
132 console.log(chalk.green("apk输出目录:" + rootPath + '/app/build/outputs/apk'));
133 console.log(chalk.green('=============================================='));
134 console.log(chalk.green('=============================================='));
135 try {
136 child_process.execSync(`adb -s ${device} install -r ${apkName}`, {encoding: 'utf8'})
137 } catch (e) {
138 reject()
139 }
140 resolve({device, options})
141 })
142}
143
144function runApp({device, options}) {
145 return new Promise((resolve, reject) => {
146 console.log(` => ${chalk.blue.bold('Running app ...')}`);
147 const rootPath = process.cwd();
148 console.log(rootPath);
149 const packageName = fs.readFileSync('build.gradle', 'utf8').match(/applicationId\s*=\s*(["'])(.+?)\1/)[2];
150 try {
151 child_process.execSync(`adb -s ${device} shell am start -n ${packageName}/${packageName}.WelcomeActivity`, {encoding: 'utf8'})
152 } catch (e) {
153 reject(e)
154 }
155 resolve()
156 })
157}
158
159
160module.exports = {runAndroid};