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
Related
I need to open interactive login shell from my electron application, after reading some other topic I found that we can set options.stdio to inherit from spawn method to open interactive shell
https://nodejs.org/api/child_process.html#optionsstdio
but I can't found anything about login shell. I also tried adding options.shell like this (I'm just trying)
{ shell: 'C:\\msys64\\usr\\bin\\bash.exe --login' }
Is this possible to do?
I'm building a node.js app which allows people to run code on my server and I'm using Docker to containerise the user's code so that it can't steal data or in general do something they shouldn't. I have a Docker image template that is copied into the user's personal app directory and I want to build the image using this function I've written:
const util = require("util");
const exec = util.promisify(require("child_process").exec);
async function buildContainer(path, dockerUser) {
return await exec(`sudo docker build -t user_app_${dockerUser} ${path}`);
}
However when I go to use it, it requires me to enter my sudo password as if I was executing it manually in a terminal window.
Is there anyway I can run this function without having to include the sudo keyword?
Thanks in advance.
you can use podman instead of docker.
There you donĀ“t need sudo.
You have the most commands like docker.
example:
podman build
podman run
and so on...
hope that helps :)
Regards
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?
I'm running two commands
indexer idx_name --rotate
indexer idx_name --buildstops dict_file 10
Everything is fine when I run these commands from command line. However, when I pass these two commands through my node application using exec, first command works successfully and for second command the dict_file is not getting generated.
I tried some combinations with sudo, but it didn't help. I checked the stdout from both these ways(node and shell) and it looked same.
Here is my node js code:
var exec = require('child_process').exec;
var cmd = 'indexer idx_name --rotate && indexer idx_name --buildstops dict_file 10';
exec(cmd, function(err, stdout, stderr) {
console.log(stdout);
});
Is there something I'm missing ?
Which ever user is running node, will need permission to write dict_file.
Might even find it easier to delete the file, and let it be created by the right user via node (assuming that user can write the the folder)
Sudo could also work, but will need to make sure the user running node, has sudo permissions. Sorting that is definitly outside the remit of stackoverflow.
... do also check you looking in the right place. In your example you dont show a path, so the file dict_file will just be created in the current working directory (not sure how node configures that)
I got my first hubot up and running, and wrote my first few scripts based on the existing examples. My existing workflow, which I would like to integrate with hubot, is essentially based on several shell scripts, each one of them performing one task. The task can be relatively complex (git/svn checkout, compiling code with gcc, and running it). How can I execute a bash script with hubot? I have seen this question, but it only addresses simple commands such as ls. I tried
build = spawn 'source', ['test.sh']
build.stdout.on 'data', (data) -> msg.send data.toString()
build.stderr.on 'data', (data) -> msg.send data.toString()
without any luck:
Hubot> execvp(): Permission denied
I checked the obvious things (-rwxr-xr-x permissions), and export HUBOT_LOG_LEVEL="debug".
I am running hubot with the same user that owns the bash scripts.
Thanks.
For reference: the answer was
build = spawn '/bin/bash', ['test.sh']
Dah
npm install hubot-script-shellcmd
is your doorway to the shell.