|
| 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