Open MacOS Terminal window and execute command - node.js

I am working on an internal tool built with Electron that runs various NodeJS and shell scripts.
I want to open a default MacOS Terminal window and run a command. I am aware of and using NodeJS child_process functions like spawn(), fork(), and exec() for other purposes, but in some cases I would much prefer to open a desktop OS Terminal window because the command output is complex, includes escape codes, tails logs, and will be used by developers who may want to control the terminal after being launched by the app.
I tried using exec() and spawn() with open -a Terminal as described here and it simply did not open a Terminal window.
I currently have a solution based on this article that looks like this:
import {exec} from "child_process";
import os = require("os");
export function openTerminal(cmd: string) {
if (os.platform() !== 'darwin') throw new Error('Not supported');
const command = [
`osascript -e 'tell application "Terminal" to activate'`,
`-e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'`,
`-e 'tell application "Terminal" to do script "${cmd}" in selected tab of the front window'`
].join(" ");
const child = exec(command, (error, stdout, stderr) => {
if (error) {
console.error(error);
alert("Unable to open Terminal window, see dev console for error.");
}
});
child.on("exit", (code) => console.log("Open terminal exit"));
}
This works, but has some issues:
Initially the Electron app (built with electron-packager) is not allowed to run this command, and the user is prompted with "This app wants to control your computer". If the user accepts the dialog the command and subsequent commands still just fail to open a Terminal window. They have to go to OS Security/Accessibility settings, unlock admin privileges, set the app to allow control, close the app, then re-open. Then it might work.
Under working conditions (see above) it takes a painfully long time for the window to show up.
If Terminal is already open (which is usually the case) it often (but not always) fails to run the command in the new window, rather it will duplicate an existing tab and run the command in the original tab, which may not work if that tab was already running a command. Even if it does work this is not desirable, it should run the command in the new tab not an existing one.
Is there a way to open a terminal window in MacOS from Electron that avoids these problems?

Related

Terminal in React-Electron app

My requirement is to embed a terminal in my React-Electron app wherein all commands which I can run from bash can be run in the embedded terminal too.
Suppose I want to 'npm install' I want it to be possible through my embedded terminal too. Could anyone suggest possible solutions ?
I'm not exactly sure, but I bet you can create a interface with an text input, get the content from it, and use some function of NodeJS to run that content (witch should be a command). Then, just print the result on the screen.
You can use the exec function from "child_process" dependencie, like this.
const { exec } = require("child_process");
exec("ls");
For more details, you can check here: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Can I make Node.js opn open the browser with maximized window when Node.js itself is started in a minimized console window ( Windows 10 )?

My problem is that it seems that on Windows 10 a desktop link (.lnk) that opens a console application in a minimized window makes all windows opened by this console application to be also minimized.
I would like to start Node.js in a minimized console window, but at the same time make Node.js opn open the browser in a maximized window ( this makes sense, since I'm not interested in the console output, rather I want the output in the browser window ).
opn("http://localhost:9000") // but hey, in maximized window!
Is there any way I can achieve this either by Node.js or by some Windows manipulation?
I made it. It is a Windows trick.
Based on this answer:
How do I minimize the command prompt from my bat file
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
... script logic here ...
exit
This a trick for a script to run itself in a minimized window. But somehow the property inherited from the .lnk remains maximized, so the browser window opened by the script running in its self minimized window opens the browser maximized.
There is a workaround, at least in my used case. But this just minimized the node console only. It will still appear in the taskbar.
I just created a shortcut with
Target: C:\WINDOWS\system32\cmd.exe /c start /min node < Your Application >
Start in: < Path to your application >
"Start in" is not required if you specify the full path to your application
Examples:
Target: C:\WINDOWS\system32\cmd.exe /c start /min node "C:\Users\jjyong\Documents\Programming\Node\PriceTracking\app.js"
Or
Target: C:\WINDOWS\system32\cmd.exe /c start /min node app.js
Start in: C:\Users\jjyong\Documents\Programming\Node\PriceTracking
Note:
I have only tested using NodeJS version 12.13.1 with PuppeteerJS running chromium with "headless: false"

Can i open a new shell window or tab with node.js

When i use shelljs, How can i execute script by opening another shell window or tab ?
I need to start api server and build app both. like
var shell = require('shelljs');
...
shell.exec('node server.js');
shell.exec('npm run build')

Warning x error baddrawable when I run the deepmind DQN on GNOME (ubuntu 12)

It was scuccessful to run dqn with no image, but by trying using qlua (some tutorial) to see the netwrok playing the rom in real time, it gets a
Warning x error baddrawable
It opens an image window but it is grey/blank and in terminal I get a " warning x error baddrawable (...)" error.
UPDATE: I have solved the issue I was facing. Since I am running the DeepMind in one of my instances of Google Cloud V's, via a VNC GUI server, the proceedings should work in any.
I´ve opened the script file (run_cpu) and put the line "export QT_GRAPHICSSYSTEM=native" right before the line that runs qlua. Let me share you my editings:
(UBUNTU 12.* using latest vncserver + GNOME GUI)
//terminal and where the dqn dir is:
$ vim run_cpu
//that opens the file 'run_cpu'. Press "i" to edit. The new edit should look like this (copy and paste at line 45):
export QT_GRAPHICSSYSTEM=native
//Press "ESC" then type ":wq" then enter.
$ reboot
//To make sure the effects will take change after restarting all processes.
Open a new SSH terminal via Google Cloud console. Before running the vncserver, also run the export QT_GRAPHICSSYSTEM=native before:
$ export QT_GRAPHICSSYSTEM=native
$ vncserver
Then, when connected thru a VNC client, open the terminal there and run ./run_cpu.
$ sudo bash ./run_cpu
//In case no root access.
UPDATE 2: I've also did a very small change at the dqn-graphics.sh. At line 4, leave nothing but the line with a 'then'. Like this ( :
//$ vim dqn-graphics.sh. Scroll at line 4. Press Enter. Put:
then
//ESC :wq

Can I install an app as certified on my own phone? (Firefox OS)

Is there any way that I can develop an app for Firefox OS that uses a certified API and install it on my own phone?
Our specific need is for telephony data such as signal strength. We can do this on Android without any difficulty (indeed, we could put the app on Google Play if we wanted to). But we just need to install it on a small number of in-house phones.
It seems that Firefox OS considers it to be in some way a security risk to find out the signal strength of the cell tower, and similar telephony data. Am I correct in assuming that there is no way that Firefox OS will let a developer app read this data?
If you are using Firefox OS 1.2 > you should be able to push a certified app to a unlocked phone for testing purposes. I put in code like:
testconn.onclick = function () {
var cnx = navigator.mozMobileConnection;
if (cnx.voice.connected) {
console.log("The signal has a strength of " + (+cnx.voice.relSignalStrength) + "%");
} else {
console.log("The state of the connection is: " + cnx.voice.state);
}
};
and entered the following permission in the manifest:
"permissions": {
"mobileconnection":{}
},
"type": "certified"
You should be able to install your certified app on a real Firefox OS phone after performing these steps from MDN:
On your computer, enter the following command in Terminal/console to enter your device's filesystem via the shell:
adb shell
Your prompt should change to root#android.
Next, stop B2G running using the following command:
stop b2g
Navigate to the following directory:
cd /data/b2g/mozilla/*.default/
Here, update the prefs.js file with the following line:
echo 'user_pref("devtools.debugger.forbid-certified-apps", false);' >> prefs.js
After you've finished editing and saving the file, start B2G again using the following command:
start b2g
Exit the android filesystem using the exit command; this will return you to your normal terminal prompt.

Resources