Skip to content

Instantly share code, notes, and snippets.

@techierishi
Forked from millermedeiros/example.js
Created August 9, 2021 18:00
Show Gist options
  • Select an option

  • Save techierishi/76c054a5f95248bec78b12245a5881e5 to your computer and use it in GitHub Desktop.

Select an option

Save techierishi/76c054a5f95248bec78b12245a5881e5 to your computer and use it in GitHub Desktop.

Revisions

  1. @millermedeiros millermedeiros revised this gist Feb 6, 2013. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  2. @millermedeiros millermedeiros created this gist Feb 6, 2013.
    40 changes: 40 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    // 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();
    };
    19 changes: 19 additions & 0 deletions gistfile2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    // 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');
    });