Skip to content

Commit 916b619

Browse files
committed
refactors
1 parent 7a94c31 commit 916b619

File tree

25 files changed

+3952
-1103
lines changed

25 files changed

+3952
-1103
lines changed

src/commands/android/androidSetup.ts

Lines changed: 1026 additions & 0 deletions
Large diffs are not rendered by default.

src/commands/android/constants.ts

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import inquirer from 'inquirer';
22
import os from 'os';
33
import path from 'path';
44

5-
import {AvailableOptions, AvailableSubcommands, SdkBinary} from './interfaces';
5+
import {AvailableOptions, SdkBinary} from './interfaces';
6+
import {AvailableSubcommands} from './subcommands/interfaces';
67

78
export const AVAILABLE_OPTIONS: AvailableOptions = {
89
help: {
@@ -34,7 +35,7 @@ export const AVAILABLE_OPTIONS: AvailableOptions = {
3435
export const AVAILABLE_SUBCOMMANDS: AvailableSubcommands = {
3536
connect: {
3637
description: 'Connect to a device',
37-
options: [
38+
flags: [
3839
{
3940
name: 'wireless',
4041
description: 'Connect a real device wirelessly'
@@ -43,7 +44,57 @@ export const AVAILABLE_SUBCOMMANDS: AvailableSubcommands = {
4344
},
4445
disconnect: {
4546
description: 'Disconnect a real device or emulator',
46-
options: []
47+
cliConfigs: [{
48+
name: 'deviceId',
49+
alias: ['s'],
50+
description: 'Id of the device to disconnect',
51+
usageHelp: 'device_id'
52+
}],
53+
flags: []
54+
},
55+
list: {
56+
description: 'List connected devices or installed AVDs',
57+
flags: [{
58+
name: 'device',
59+
description: 'List connected devices (real devices and AVDs)'
60+
},
61+
{
62+
name: 'avd',
63+
description: 'List installed AVDs'
64+
}]
65+
},
66+
install: {
67+
description: 'Install APK or AVD on a device',
68+
flags: [
69+
{
70+
name: 'avd',
71+
description: 'Create an Android Virtual Device'
72+
},
73+
{
74+
name: 'app',
75+
description: 'Install an APK on the device',
76+
cliConfigs: [
77+
{
78+
name: 'path',
79+
alias: ['p'],
80+
description: 'Path to the APK file',
81+
usageHelp: 'path_to_apk'
82+
},
83+
{
84+
name: 'deviceId',
85+
alias: ['s'],
86+
description: 'Id of the device to install the APK',
87+
usageHelp: 'device_id'
88+
}
89+
]
90+
}
91+
]
92+
},
93+
uninstall: {
94+
description: 'todo item',
95+
flags: [
96+
{name: 'avd', description: 'todo item'},
97+
]
4798
}
4899
};
49100

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import colors from 'ansi-colors';
2+
import {spawnSync} from 'child_process';
3+
import * as dotenv from 'dotenv';
4+
import path from 'path';
5+
6+
import {ANDROID_DOTCOMMANDS} from '../../constants';
7+
import Logger from '../../logger';
8+
import {getPlatformName} from '../../utils';
9+
import {Platform, SdkBinary} from './interfaces';
10+
import {checkJavaInstallation, getBinaryLocation, getBinaryNameForOS, getSdkRootFromEnv} from './utils/common';
11+
12+
export class AndroidDotCommand {
13+
dotcmd: string;
14+
args: string[];
15+
sdkRoot: string;
16+
rootDir: string;
17+
platform: Platform;
18+
androidHomeInGlobalEnv: boolean;
19+
20+
constructor(dotcmd: string, argv: string[], rootDir = process.cwd()) {
21+
this.dotcmd = dotcmd;
22+
this.args = argv.slice(1);
23+
this.sdkRoot = '';
24+
this.rootDir = rootDir;
25+
this.platform = getPlatformName();
26+
this.androidHomeInGlobalEnv = false;
27+
}
28+
29+
async run(): Promise<boolean> {
30+
if (!ANDROID_DOTCOMMANDS.includes(this.dotcmd)) {
31+
Logger.log(colors.red(`Unknown dot command passed: ${this.dotcmd}\n`));
32+
33+
Logger.log('Run Android SDK command line tools using the following command:');
34+
Logger.log(colors.cyan('npx @nightwatch/mobile-helper <DOTCMD> [options|args]\n'));
35+
36+
Logger.log(`Available Dot Commands: ${colors.magenta(ANDROID_DOTCOMMANDS.join(', '))}`);
37+
Logger.log(`(Example command: ${colors.gray('npx @nightwatch/mobile-helper android.emulator @nightwatch-android-11')})\n`);
38+
39+
return false;
40+
}
41+
42+
const javaInstalled = checkJavaInstallation(this.rootDir);
43+
if (!javaInstalled) {
44+
return false;
45+
}
46+
47+
this.loadEnvFromDotEnv();
48+
49+
const sdkRootEnv = getSdkRootFromEnv(this.rootDir, this.androidHomeInGlobalEnv);
50+
if (!sdkRootEnv) {
51+
Logger.log(`Run: ${colors.cyan('npx @nightwatch/mobile-helper android --standalone')} to fix this issue.`);
52+
Logger.log(`(Remove the ${colors.gray('--standalone')} flag from the above command if using the tool for testing.)\n`);
53+
54+
return false;
55+
}
56+
this.sdkRoot = sdkRootEnv;
57+
58+
return this.executeDotCommand();
59+
}
60+
61+
loadEnvFromDotEnv(): void {
62+
this.androidHomeInGlobalEnv = 'ANDROID_HOME' in process.env;
63+
dotenv.config({path: path.join(this.rootDir, '.env')});
64+
}
65+
66+
buildCommand(): string {
67+
const binaryName = this.dotcmd.split('.')[1] as SdkBinary;
68+
const binaryLocation = getBinaryLocation(this.sdkRoot, this.platform, binaryName, true);
69+
70+
let cmd: string;
71+
if (binaryLocation === 'PATH') {
72+
const binaryFullName = getBinaryNameForOS(this.platform, binaryName);
73+
cmd = `${binaryFullName}`;
74+
} else {
75+
const binaryFullName = path.basename(binaryLocation);
76+
const binaryDirPath = path.dirname(binaryLocation);
77+
cmd = path.join(binaryDirPath, binaryFullName);
78+
}
79+
80+
return cmd;
81+
}
82+
83+
executeDotCommand(): boolean {
84+
const cmd = this.buildCommand();
85+
const result = spawnSync(cmd, this.args, {stdio: 'inherit'});
86+
87+
if (result.error) {
88+
console.error(result.error);
89+
90+
return false;
91+
}
92+
93+
return result.status === 0;
94+
}
95+
}
96+

0 commit comments

Comments
 (0)