Callback when starting Android emulator with shell command via node - node.js

I am opening an android emulator with node via a shell script:
var process = require('child_process');
process.exec('~/Library/Android/sdk/tools/emulator -avd Nexus_5_API_21_x86', processed);
function processed(data){
console.log('processed called', data, data.toString());
}
I need to be able to detect when the emulator has finished loading so I can initiate a screen unlock and then launch the browser to a specified url (~/Library/Android/sdk/platform-tools/adb shell input keyevent 82 and ~/Library/Android/sdk/platform-tools/adb shell am start -a android.intent.action.VIEW -d http://www.stackoverflow.com)
However, when I launch the emulator I don't appear to get anything back and the process stays engaged with the emulator. When shutting the process down (ctrl+c) the emulator is closed along with it. (This is the same behaviour as running the shell command directly in the terminal)
Is it possible to know when the emulator has opened and loaded?
How can I execute additional commands when the process continues to
run?

I solved it like a boss.
I was able to set a timer to check once a second if the bootanimation had stopped. If it has, we know the emulator is open and booted.
var process = require('child_process');
process.exec('~/Library/Android/sdk/tools/emulator -avd Nexus_5_API_21_x86');
function isEmulatorBooted(){
process.exec('~/Library/Android/sdk/platform-tools/adb shell getprop init.svc.bootanim', function(error, stdout, stderr){
if (stdout.toString().indexOf("stopped")>-1){
clearInterval(bootChecker);
emulatorIsBooted();
} else {
console.log('we are still loading');
}
});
}
function emulatorIsBooted(){
//unlock the device
process.exec('~/Library/Android/sdk/platform-tools/adb shell input keyevent 82');
//gotourl
process.exec('~/Library/Android/sdk/platform-tools/adb shell am start -a android.intent.action.VIEW -d http://192.168.10.126:9876/');
}
bootChecker = setInterval(function(){
isEmulatorBooted();
},1000);

In case anyone else is looking for this, made an alternate version of Fraser's script - which also starts the actual emulator in a background process.
#!/usr/bin/env node
const process = require('child_process');
/* Get last emulator in list of emulators */
process.exec("emulator -list-avds|sed '$!d'", (_, stdout) => {
console.log('[android emulator] Booting')
/* Start emulator */
process.exec(`nohup emulator -avd ${stdout.replace('\n', '')} >/dev/null 2>&1 &`)
/* Wait for emulator to boot */
const waitUntilBootedThen = completion => {
process.exec('adb shell getprop init.svc.bootanim', (_, stdout) => {
if (stdout.replace('\n', '') !== 'stopped') {
setTimeout(() => waitUntilBootedThen(completion), 250)
} else {
completion()
}
})
}
/* When emulator is booted up */
waitUntilBootedThen(() => {
console.log('[android emulator] Done')
})
})

Related

NodeJS - How do I detect other copies of my program?

I have written a NodeJS command-line program with two modes:
mode foo: runs forever until the user presses Ctrl+C
mode bar: runs once
If the user is already running the program in mode foo, then running it again in mode bar will cause errors. Thus, when the user invokes mode bar, I want to search for all other existing copies of my command-line program that are running and kill them (as a mechanism to prevent the errors before they happen).
Getting a list of processes in NodeJS is easy, but that doesn't help me much. If I simply kill all other node processes, then I might be killing other programs that are not mine. So, I need to know which specific node processes are the ones running my app. Is it even possible to interrogate a process to determine that information?
Another option is to have my program write a temporary file to disk, or write a value to the Windows registry, or something along those lines. And then, before my program exists, I could clean up the temporary value. However, this feels like a precarious solution, because if my program crashes, then the flag will never be unset and will remain orphaned forever.
What is the correct solution to this problem? How can I kill my own application?
I was able to solve this problem using PowerShell:
import { execSync } from "child_process";
const CWD = process.cwd();
function validateOtherCopiesNotRunning(verbose: boolean) {
if (process.platform !== "win32") {
return;
}
// From: https://securityboulevard.com/2020/01/get-process-list-with-command-line-arguments/
const stdout = execPowershell(
"Get-WmiObject Win32_Process -Filter \"name = 'node.exe'\" | Select-Object -ExpandProperty CommandLine",
verbose,
);
const lines = stdout.split("\r\n");
const otherCopiesOfMyProgram= lines.filter(
(line) =>
line.includes("node.exe") &&
line.includes("myProgram") &&
// Exclude the current invocation that is doing a 1-time publish
!line.includes("myProgram publish"),
);
if (otherCopiesOfMyProgram.length > 0) {
throw new Error("You must close other copies of this program before publishing.");
}
}
function execPowershell(
command: string,
verbose = false,
cwd = CWD,
): string {
if (verbose) {
console.log(`Executing PowerShell command: ${command}`);
}
let stdout: string;
try {
const buffer = execSync(command, {
shell: "powershell.exe",
cwd,
});
stdout = buffer.toString().trim();
} catch (err) {
throw new Error(`Failed to run PowerShell command "${command}":`, err);
}
if (verbose) {
console.log(`Executed PowerShell command: ${command}`);
}
return stdout;
}

Is it possible to monitor terminal for specific output?

I am using NodeJS to run webots by command line and have redirected the stdout too the node terminal. My problem is that I want to trigger an event based on a console log. I tried redirecting the stdout of the command to another file, but this didn't seem to work.
This is the console output
INFO: sumo_example_two: Starting controller: python.exe -u sumo_example_two.py
INFO: sumo_supervisor: Starting controller: python.exe -u sumo_supervisor.py
robot2
INFO: sumo_example_one: Terminating.
INFO: sumo_example_two: Terminating.
INFO: sumo_supervisor: Terminating.
stdout:
I want to extract 'robot2'.
I have just tested and the following snippet works fine for me:
const { spawn } = require('child_process');
const ls = spawn('webots', ['--stdout']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
// Process `data` as you prefer, something like
//
// if (data.includes('robot2')) {
// something()
// }
});

Open terminal and launch commands

In my electron/reactjs app, i'm trying to open a terminal and launch somes commands.
My code looks like this :
const terminal = 'x-terminal-emulator';
const { spawn } = require('child_process');
spawn(terminal);
My terminal opens but i don't know how to launch commands in this terminal like 'cd /my/custom/path && ls'
Can someone help me please ? :)
Node.js child_process.spawn command have an option to specify the shell you want to use.
So I would use the opposite logic and launch directly the command within a particular shell (for exemple bash):
const { spawn } = require('child_process');
const terminal = '/bin/bash';
let cmd = 'echo $SHELL';
spawn(cmd, { shell: terminal })
.stdout.on('data', (data) => {
console.log(`stdout: ${data}`); //-> stdout: /bin/bash
});

Node js app running inside container opens user's web browser

I need an option to open user's default web browser from node js app which is running inside docker container. I need to know that to implement OAuth flow.
I know that I can do it by opening new tab on the client side, but I don't have this option for other reasons.
I'm not familiar with docker container, but for just straight nodejs, this works for me:
// start browser
if (opSys == "Win64")
command = ("start http://localhost:8000/init"); // Win64
else if (opSys == "MacOS")
command = ("open http://localhost:8000/init"); // MacOS
else
command = ("xdg-open http://localhost:8000/init"); // Linux
exec (command, function (error, stdout, stderr)
{
if (error)
{
console.log ("command: ", command);
console.log ("error: ", stderr);
}
});

Node JS: Executing command lines and getting outputs asynchronously

How can I run a command line and get the outputs as soon as available to show them somewhere.
For example if a run ping command on a linux system, it will never stop, now is it possible to get the responses while the command is still processing ?
Or let's take apt-get install command, what if i want to show the progress of the installation as it is running ?
Actually i'm using this function to execute command line and get outputs, but the function will not return until the command line ends, so if i run a ping command it will never return!
var sys = require('sys'),
exec = require('child_process').exec;
function getOutput(command,callback){
exec(
command,
(
function(){
return function(err,data,stderr){
callback(data);
}
}
)(callback)
);
}
Try using spawn instead of exec, then you can tap into the stream and listen to the data and end events.
var process = require('child_process');
var cmd = process.spawn(command);
cmd.stdout.on('data', function(output){
console.log(output.toString()):
});
cmd.on('close', function(){
console.log('Finished');
});
//Error handling
cmd.stderr.on('data', function(err){
console.log(err);
});
See the Node.js documentation for spawn here: https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Resources