Skip to content

Commit

Permalink
code: removed double error logging, extra join and split, cleaner met…
Browse files Browse the repository at this point in the history
…hod chaining
  • Loading branch information
noomorph committed May 2, 2018
1 parent 8855370 commit 8f93eea
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions detox/src/devices/android/Emulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,51 @@ class Emulator {
}

async boot(emulatorName) {
const cmd = _.compact([
const emulatorArgs = _.compact([
'-verbose',
'-gpu', 'auto',
'-no-audio',
argparse.getArgValue('headless') ? '-no-window' : '',
`@${emulatorName}`
]).join(' ');
]);

log.verbose(this.emulatorBin, cmd);
const tempLog = `./${emulatorName}.log`;
const stdout = fs.openSync(tempLog, 'a');
const stderr = fs.openSync(tempLog, 'a');
const tail = new Tail(tempLog);
const promise = spawn(this.emulatorBin, _.split(cmd, ' '), {detached: true, stdio: ['ignore', stdout, stderr]});

const childProcess = promise.childProcess;
childProcess.unref();

tail.on("line", function(line) {
const tail = new Tail(tempLog).on("line", (line) => {
if (line.includes('Adb connected, start proxing data')) {
detach();
promise._cpResolve();
childProcessPromise._cpResolve();
}
});

function detach() {
tail.unwatch();
fs.closeSync(stdout);
fs.closeSync(stderr);
fs.unlink(tempLog, () => {});
fs.unlink(tempLog, _.noop);
}

return promise.catch(function(err) {
log.verbose(this.emulatorBin, ...emulatorArgs);
const childProcessPromise = spawn(this.emulatorBin, emulatorArgs, {
detached: true,
stdio: ['ignore', stdout, stderr]
}).then(() => null, (err) => {
const output = fs.readFileSync(tempLog, 'utf8');

if (output.includes(`There's another emulator instance running with the current AVD`)) {
log.verbose('stdout', '%s', output);
return;
}

if (log.level === 'verbose') {
log.error('ChildProcessError', '%j', err);
} else {
log.error('ChildProcessError', '%s', err.message);
return null;
}

log.error('ChildProcessError', '%s', err.message);
log.error('stderr', '%s', output);
detach();
throw err;
});

childProcessPromise.childProcess.unref();
return childProcessPromise;
}
}

Expand Down

0 comments on commit 8f93eea

Please sign in to comment.