Skip to content

Commit b163b3b

Browse files
committed
Merge branch 'main' into disconnect
2 parents 7a94c31 + d7d9a81 commit b163b3b

File tree

25 files changed

+2120
-1134
lines changed

25 files changed

+2120
-1134
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: 46 additions & 5 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,16 +35,56 @@ 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'
4142
}
4243
]
4344
},
44-
disconnect: {
45-
description: 'Disconnect a real device or emulator',
46-
options: []
45+
list: {
46+
description: 'List connected devices or installed AVDs',
47+
flags: [{
48+
name: 'device',
49+
description: 'List connected devices (real devices and AVDs)'
50+
},
51+
{
52+
name: 'avd',
53+
description: 'List installed AVDs'
54+
}]
55+
},
56+
install: {
57+
description: 'Install APK or AVD on a device',
58+
flags: [
59+
{
60+
name: 'avd',
61+
description: 'Create an Android Virtual Device'
62+
},
63+
{
64+
name: 'app',
65+
description: 'Install an APK on the device',
66+
cliConfigs: [
67+
{
68+
name: 'path',
69+
alias: ['p'],
70+
description: 'Path to the APK file',
71+
usageHelp: 'path_to_apk'
72+
},
73+
{
74+
name: 'deviceId',
75+
alias: ['s'],
76+
description: 'Id of the device to install the APK',
77+
usageHelp: 'device_id'
78+
}
79+
]
80+
}
81+
]
82+
},
83+
uninstall: {
84+
description: 'todo item',
85+
flags: [
86+
{name: 'avd', description: 'todo item'}
87+
]
4788
}
4889
};
4990

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)