Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
execute multiple shell commands in series on node.js
// USAGE ------
// ============
var shell = require('./shellHelper');
// execute a single shell command
shell.exec('npm test --coverage', function(err){
console.log('executed test');
}});
// execute multiple commands in series
shell.series([
'node build release'
'git add -A',
'git commit --verbose'
], function(err){
console.log('executed many commands in a row');
});
// spawn a child process and execute shell command
// borrowed from https://github.com/mout/mout/ build script
// author Miller Medeiros
// released under MIT License
// version: 0.1.0 (2013/02/01)
// execute a single shell command where "cmd" is a string
exports.exec = function(cmd, cb){
// this would be way easier on a shell/bash script :P
var child_process = require('child_process');
var parts = cmd.split(/\s+/g);
var p = child_process.spawn(parts[0], parts.slice(1), {stdio: 'inherit'});
p.on('exit', function(code){
var err = null;
if (code) {
err = new Error('command "'+ cmd +'" exited with wrong status code "'+ code +'"');
err.code = code;
err.cmd = cmd;
}
if (cb) cb(err);
});
};
// execute multiple commands in series
// this could be replaced by any flow control lib
exports.series = function(cmds, cb){
var execNext = function(){
exports.exec(cmds.shift(), function(err){
if (err) {
cb(err);
} else {
if (cmds.length) execNext();
else cb(null);
}
});
};
execNext();
};
@rafaelrinaldi

This comment has been minimized.

Copy link

commented Jul 15, 2013

Loved this idea, going to implement something like thins on my default build system.
Found a good library to deal with shell commands, don't know if you already use it: shelljs

@shurane

This comment has been minimized.

Copy link

commented Nov 15, 2014

There's always async for control flow, too.

@bitsofinfo

This comment has been minimized.

Copy link

commented Jan 19, 2015

Cool lib, if you want to exec all the commands over the same shell instance you could try this too: https://github.com/bitsofinfo/stateful-process-command-proxy

@cagataycali

This comment has been minimized.

Copy link

commented Sep 13, 2016

maybe you can use g3l.
https://www.npmjs.com/package/g3l

@gsundin

This comment has been minimized.

Copy link

commented Apr 10, 2017

Is there a way to log the terminal output from running commands with this?

@cagcak

This comment has been minimized.

Copy link

commented Apr 22, 2017

the shell.series results that no way to pass a shell command containing whitespaces inside a string

@jay16

This comment has been minimized.

Copy link

commented Nov 16, 2018

thanks!

var chalk = require('chalk')
const { exec } = require('child_process');

var version = require('../version')
var type = process.argv[2],
    moduler = process.argv[3],
    message = process.argv.slice(4, process.argv.length).join(' '),
    commands = [
      "git add .",
      "git commit -m \"" + type + "@" + version.pro_version + "(" + moduler + "): " + message + "\"",
      "git push origin $(git symbolic-ref --short -q HEAD)"
    ]

var exec_commands = (commands) => {
  var command = commands.shift()
  exec(command, (error, stdout, stderr) => {
    console.log(chalk.gray("$ ") + chalk.yellow(command))
    console.log("")
    if (error) {
      console.error(chalk.red(error));
      process.exit(1)
      return;
    }
    if(stdout) console.log(chalk.gray(stdout));
    if(stderr) console.log(chalk.gray(stderr));

    if(commands.length) exec_commands(commands)
  });
}
exec_commands(commands)
@ParadeTo

This comment has been minimized.

Copy link

commented Sep 3, 2019

It cannot support this situation: cd to somewhere and then execute the command under that path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.