Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
itsspriyansh committed Aug 18, 2024
1 parent a5fe8fa commit 8f0f896
Showing 1 changed file with 11 additions and 33 deletions.
44 changes: 11 additions & 33 deletions src/commands/android/dotcommands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import colors from 'ansi-colors';
import {spawn} from 'child_process';
import {spawnSync} from 'child_process';
import * as dotenv from 'dotenv';
import path from 'path';

Expand Down Expand Up @@ -34,6 +34,7 @@ export class AndroidDotCommand {
Logger.log(colors.cyan('npx @nightwatch/mobile-helper <DOTCMD> [options|args]\n'));

Logger.log(`Available Dot Commands: ${colors.magenta(ANDROID_DOTCOMMANDS.join(', '))}\n`);
Logger.log('Example command: npx @nightwatch/mobile-helper android.emulator @nightwatch-android-11\n');

return false;
}
Expand All @@ -55,27 +56,14 @@ export class AndroidDotCommand {
}
this.sdkRoot = sdkRootEnv;

return await this.executeDotCommand();
return this.executeDotCommand();
}

loadEnvFromDotEnv(): void {
this.androidHomeInGlobalEnv = 'ANDROID_HOME' in process.env;
dotenv.config({path: path.join(this.rootDir, '.env')});
}

async executeDotCommand(): Promise<boolean> {
try {
const cmd = this.buildCommand();
await this.runCommandStream(cmd);

return true;
} catch (err) {
console.error(err);

return false;
}
}

buildCommand(): string {
const binaryName = this.dotcmd.split('.')[1] as SdkBinary;
const binaryLocation = getBinaryLocation(this.sdkRoot, this.platform, binaryName, true);
Expand All @@ -93,27 +81,17 @@ export class AndroidDotCommand {
return cmd;
}

async runCommandStream(cmd: string) {
return new Promise((resolve, reject) => {
const stream = spawn(cmd, this.args);

stream.stdout.on('data', (data) => {
process.stdout.write(data.toString());
});
executeDotCommand(): boolean {
const cmd = this.buildCommand();
const result = spawnSync(cmd, this.args, {stdio: 'inherit'});

stream.stderr.on('data', (data) => {
process.stderr.write(data.toString());
});
if (result.error) {
console.error(result.error);

stream.on('close', (code) => {
resolve(code);
});
return false;
}

stream.on('error', (err) => {
console.error(err);
reject(err);
});
});
return result.status === 0;
}
}

0 comments on commit 8f0f896

Please sign in to comment.