I am trying to simplify my workflow and for that I'd need to run from Node
a script that execute some commands
The ones that are no interactive, such as git add * and so on I can do with require('child_process').exec just fine
But the ones that require the user intervention such as lerna blah I cannot
I've tried something like
let { stdout, stderr } = await spawn('lerna', [lerna_option, '--no-push'], {
env: process.env,
stdio: 'inherit',
shell: true,
})
if (stdout) console.log('\n' + stdout)
if (stderr) console.log('\n' + stderr)
But it gives me one interaction and then goes on, not allowing me to ask all the questions and run as intended
Is there any simple way to accomplish this?
Related
I know this question has been asked many times but I have tried most of the methods and they just don't work for me.
So here is my problem, I have a simple bash script like this
#!/bin/bash
echo "Username: $1";
echo %DATABASE_URL%;
I want to run this script in a separate process. so if the parent process gets killed during my script being excused it still continues running.
Here is my nodejs code
const child = spawn('bash', [`script.sh`, 'test'], {
detached: true,
cwd: process.cwd(),
detached: true,
stdio: "inherit",
DATABASE_URL: 'test'
}, function (err, stdout, stderr) {
// Node.js does not invoke this
console.log(stdout);
stdout.on("data", data => {
console.log('Output of script execution');
});
stderr.on("data", data => {
console.log('an error with file system');
});
});
child.unref();
child.on('exit', (code) => {
console.log("Child exited");
});
So I know that my script returns some output and should run callback but it does not run it. It directly jumps to the on.('exit') callback which confuses me.
Also it worth mentioning that I am testing the code on windows and bash script.sh 'test' works if I run it on cmd.
Posts I have tried:
How to run shell script file using nodejs?
Execute script from Node in a separate process
Bash Script : what does #!/bin/bash mean?
and many of the existing weblogs that explains the same.
I want to execute a bash command - dotnet dev-certs https --trust in an interactive terminal through child_process library.
Since it asks for the user password, it has to be interactive terminal.
I've already tried using AppleScript for it, but the user experience is subpar since it tends to leave half-closed terminal windows around.
Edit - Added the code snippet that I am using to create a child_process.
import * as cp from 'child_process'
cp.spawn('dotnet dev-certs https --trust', {})
I've tried many many combinations of cp.spawn and cp.exec.
e.g. cp.spawn('...', { shell: true, stdio: 'ignore', detached: true }) etc.
cp.spawn actually creates a process, but it's not interactive and immediately terminates.
You have to filter the incoming data from stdout with stdout.on('data', data => {}) to find the shell request for user input. After you found that specific line you can simply send data to the shell via the stdin.write('any input \n')
import * as cp from 'child_process'
const ls = cp.spawn('dotnet dev-certs https --trust', {})
ls.stdout.on('data', function (data) {
const currentData = data.toString();
//check for password input notification:
if(currentData === "input credentials: ")
{
//send the password via stdin. \n does the trick over here.
ls.stdin.write('Password\n');
}
});
I need to run a shell script file using nodeJS that executes a set of Cassandra DB commands. Can anybody please help me on this.
inside db.sh file:
create keyspace dummy with replication = {'class':'SimpleStrategy','replication_factor':3}
create table dummy (userhandle text, email text primary key , name text,profilepic)
You could use "child process" module of nodejs to execute any shell commands or scripts with in nodejs. Let me show you with an example, I am running a shell script(hi.sh) with in nodejs.
hi.sh
echo "Hi There!"
node_program.js
const { exec } = require('child_process');
var yourscript = exec('sh hi.sh',
(error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
Here, when I run the nodejs file, it will execute the shell file and the output would be:
Run
node node_program.js
output
Hi There!
You can execute any script just by mentioning the shell command or shell script in exec callback.
You can execute any shell command using the shelljs module
const shell = require('shelljs')
shell.exec('./path_to_your_file')
you can go:
var cp = require('child_process');
and then:
cp.exec('./myScript.sh', function(err, stdout, stderr) {
// handle err, stdout, stderr
});
to run a command in your $SHELL.
Or go
cp.spawn('./myScript.sh', [args], function(err, stdout, stderr) {
// handle err, stdout, stderr
});
to run a file WITHOUT a shell.
Or go
cp.execFile();
which is the same as cp.exec() but doesn't look in the $PATH.
You can also go
cp.fork('myJS.js', function(err, stdout, stderr) {
// handle err, stdout, stderr
});
to run a javascript file with node.js, but in a child process (for big programs).
EDIT
You might also have to access stdin and stdout with event listeners. e.g.:
var child = cp.spawn('./myScript.sh', [args]);
child.stdout.on('data', function(data) {
// handle stdout as `data`
});
Also, you can use shelljs plugin.
It's easy and it's cross-platform.
Install command:
npm install [-g] shelljs
What is shellJS
ShellJS is a portable (Windows/Linux/OS X) implementation of Unix
shell commands on top of the Node.js API. You can use it to eliminate
your shell script's dependency on Unix while still keeping its
familiar and powerful commands. You can also install it globally so
you can run it from outside Node projects - say goodbye to those
gnarly Bash scripts!
An example of how it works:
var shell = require('shelljs');
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git');
shell.exit(1);
}
// Copy files to release dir
shell.rm('-rf', 'out/Release');
shell.cp('-R', 'stuff/', 'out/Release');
// Replace macros in each .js file
shell.cd('lib');
shell.ls('*.js').forEach(function (file) {
shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file);
shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file);
});
shell.cd('..');
// Run external tool synchronously
if (shell.exec('git commit -am "Auto-commit"').code !== 0) {
shell.echo('Error: Git commit failed');
shell.exit(1);
}
Also, you can use from the command line:
$ shx mkdir -p foo
$ shx touch foo/bar.txt
$ shx rm -rf foo
I'd like to use the execSync method which was added in NodeJS 0.12 but still have the output in the console window from which i ran the Node script.
E.g. if I run a NodeJS script which has the following line I'd like to see the full output of the rsync command "live" inside the console:
require('child_process').execSync('rsync -avAXz --info=progress2 "/src" "/dest"');
I understand that execSync returns the ouput of the command and that I could print that to the console after execution but this way I don't have "live" output...
You can pass the parent´s stdio to the child process if that´s what you want:
require('child_process').execSync(
'rsync -avAXz --info=progress2 "/src" "/dest"',
{stdio: 'inherit'}
);
You can simply use .toString().
var result = require('child_process').execSync('rsync -avAXz --info=progress2 "/src" "/dest"').toString();
console.log(result);
Edit: Looking back on this, I've realised that it doesn't actually answer the specific question because it doesn't show the output to you 'live' — only once the command has finished running.
However, I'm leaving this answer here because I know quite a few people come across this question just looking for how to print the result of the command after execution.
Unless you redirect stdout and stderr as the accepted answer suggests, this is not possible with execSync or spawnSync. Without redirecting stdout and stderr those commands only return stdout and stderr when the command is completed.
To do this without redirecting stdout and stderr, you are going to need to use spawn to do this but it's pretty straight forward:
var spawn = require('child_process').spawn;
//kick off process of listing files
var child = spawn('ls', ['-l', '/']);
//spit stdout to screen
child.stdout.on('data', function (data) { process.stdout.write(data.toString()); });
//spit stderr to screen
child.stderr.on('data', function (data) { process.stdout.write(data.toString()); });
child.on('close', function (code) {
console.log("Finished with code " + code);
});
I used an ls command that recursively lists files so that you can test it quickly. Spawn takes as first argument the executable name you are trying to run and as it's second argument it takes an array of strings representing each parameter you want to pass to that executable.
However, if you are set on using execSync and can't redirect stdout or stderr for some reason, you can open up another terminal like xterm and pass it a command like so:
var execSync = require('child_process').execSync;
execSync("xterm -title RecursiveFileListing -e ls -latkR /");
This will allow you to see what your command is doing in the new terminal but still have the synchronous call.
Simply:
try {
const cmd = 'git rev-parse --is-inside-work-tree';
execSync(cmd).toString();
} catch (error) {
console.log(`Status Code: ${error.status} with '${error.message}'`;
}
Ref: https://stackoverflow.com/a/43077917/104085
// nodejs
var execSync = require('child_process').execSync;
// typescript
const { execSync } = require("child_process");
try {
const cmd = 'git rev-parse --is-inside-work-tree';
execSync(cmd).toString();
} catch (error) {
error.status; // 0 : successful exit, but here in exception it has to be greater than 0
error.message; // Holds the message you typically want.
error.stderr; // Holds the stderr output. Use `.toString()`.
error.stdout; // Holds the stdout output. Use `.toString()`.
}
When command runs successful:
Add {"encoding": "utf8"} in options.
execSync(`pwd`, {
encoding: "utf8"
})
I'm trying to setup a boiler plate for new projects by creating a new yeoman generator, one thing that I need to have happen is a large Git repository set up as a submodule and then checked out at a certain tag. I have everything working, but I'd like to provide some progress feedback on the checkout.
When you run submodule add manually you get updated like so:
Receiving objects: 14% (22925/163744), 5.41 MiB | 1.30 MiB/s
I'd like to have that output show during my node script submodule add, but I can't seem to get it to show anything. Here's what I have:
MyGenerator.prototype.addSubmodule = function() {
var done = this.async();
console.log('Initializing submodule. This may take a minute.');
var git = spawn('git', ['submodule', 'add', 'git://github.com/PathTo/Submodule.git', 'submodule']);$
git.stdout.on('data', function(data){
console.log(data);
});
git.stderr.on('data', function(data){
console.log(data);
});
git.on('close', function(){$
process.chdir('submodule');$
console.log('Checking out %s branch of Submodule', this.submoduleVersion);
var checkout = spawn('git', ['checkout', this.submoduleVersion]);
checkout.stdout.on('data', function(data) {
console.log(data);
});
checkout.on('close', function() {
process.chdir('../');
done();
});
});
thanks in advance.
This is related to Running shell script in rails only outputs the 1 line, but is seems like the --progress option described there isn't supported by git submodule. You should be able to get the desired progress output by letting the Git process inherit the stderr file descriptor from the current process (might as well throw in stdout at the same time), which according to the child_process documentation can be done like this:
var git = spawn(
'git',
['submodule', 'add', 'git://github.com/PathTo/Submodule.git', 'submodule'],
{ stdio: ['pipe', process.stdout, process.stderr] );
If your code is run at the shell, then I believe using { stdio: 'inherit'} would fix it:
var git = spawn('git', ['submodule', 'add',
'git://github.com/PathTo/Submodule.git', 'submodule'],
{ stdio: 'inherit' });
Explanation: through tests in a bash shell I've found that if git thinks it is not communicating with a tty, it does not output any progress information. If your software is run from the shell and your tell spawn to just pass stdin, stdout, and stderr to the child process, then the child should see a tty and output progress information.