index.js
function runMultiple(input, options) {
  return new Promise((resolve, reject) => {
    let commands = input;
    // set default options
    const defaultOpts = {
      cwd: process.cwd(),
      verbose: DEFAULT_VERBOSE,
      mode: SEQUENTIAL,
      logger: console.log,
    };

    // set global options
    const globalOpts = Object.assign({}, defaultOpts, options);

    // resolve string to proper input type
    if (typeof commands === 'string') {
      commands = [{ command: commands }];
    }

    // start execution
    if (commands && typeof commands === 'object') {
      if (Object.prototype.toString.call(commands) !== '[object Array]') {
        // not array
        commands = [commands];
      }
      else {
        // is array, check type of children
        commands = commands.map(cmd => typeof cmd === 'object' ? cmd : { command: cmd });
      }

      // run commands in parallel
      if (globalOpts.mode === PARALLEL) {
        const promises = commands.map(cmd => {
          const resolvedOpts = Object.assign({}, globalOpts, cmd);
          return run(resolvedOpts);
        });
        Promise.all(promises).then(resolve, reject);
      }
      else { // run commands in sequence (default)
        async(function* () {
          try {
            const results = [];
            for (const i in commands) {
              const cmd = commands[i];
              const resolvedOpts = Object.assign({}, globalOpts, cmd);
              const result = yield run(resolvedOpts);
              results.push(result);
            }
            if (results) {
              resolve(results);
            }
            else {
              reject('Falsy value in results');
            }
          }
          catch (e) {
            reject(e);
          }
        })();
      }
    }
    else {
      reject('Invalid input');
    }
  });
}
function runMultiple(input, options) {
  return new Promise((resolve, reject) => {
    let commands = input;
    // set default options
    const defaultOpts = {
      cwd: process.cwd(),
      verbose: DEFAULT_VERBOSE,
      mode: SEQUENTIAL,
      logger: console.log,
    };

    // set global options
    const globalOpts = Object.assign({}, defaultOpts, options);

    // resolve string to proper input type
    if (typeof commands === 'string') {
      commands = [{ command: commands }];
    }

    // start execution
    if (commands && typeof commands === 'object') {
      if (Object.prototype.toString.call(commands) !== '[object Array]') {
        // not array
        commands = [commands];
      }
      else {
        // is array, check type of children
        commands = commands.map(cmd => typeof cmd === 'object' ? cmd : { command: cmd });
      }

      // run commands in parallel
      if (globalOpts.mode === PARALLEL) {
        const promises = commands.map(cmd => {
          const resolvedOpts = Object.assign({}, globalOpts, cmd);
          return run(resolvedOpts);
        });
        Promise.all(promises).then(resolve, reject);
      }
      else { // run commands in sequence (default)
        async(function* () {
          try {
            const results = [];
            for (const i in commands) {
              const cmd = commands[i];
              const resolvedOpts = Object.assign({}, globalOpts, cmd);
              const result = yield run(resolvedOpts);
              results.push(result);
            }
            if (results) {
              resolve(results);
            }
            else {
              reject('Falsy value in results');
            }
          }
          catch (e) {
            reject(e);
          }
        })();
      }
    }
    else {
      reject('Invalid input');
    }
  });
}
function runMultiple ( input , options ) { return new Promise ( ( resolve , reject ) => { let commands = input ; // set default options const defaultOpts = { cwd : process . cwd ( ) , verbose : DEFAULT_VERBOSE , mode : SEQUENTIAL , logger : console . log , } ; // set global options const globalOpts = Object . assign ( { } , defaultOpts , options ) ; // resolve string to proper input type if ( typeof commands === 'string' ) { commands = [ { command : commands } ] ; } // start execution if ( commands && typeof commands === 'object' ) { if ( Object . prototype . toString . call ( commands ) !== '[object Array]' ) { // not array commands = [ commands ] ; } else { // is array, check type of children commands = commands . map ( cmd => typeof cmd === 'object' ? cmd : { command : cmd } ) ; } // run commands in parallel if ( globalOpts . mode === PARALLEL ) { const promises = commands . map ( cmd => { const resolvedOpts = Object . assign ( { } , globalOpts , cmd ) ; return run ( resolvedOpts ) ; } ) ; Promise . all ( promises ) . then ( resolve , reject ) ; } else { // run commands in sequence (default) async ( function * ( ) { try { const results = [ ] ; for ( const i in commands ) { const cmd = commands [ i ] ; const resolvedOpts = Object . assign ( { } , globalOpts , cmd ) ; const result = yield run ( resolvedOpts ) ; results . push ( result ) ; } if ( results ) { resolve ( results ) ; } else { reject ( 'Falsy value in results' ) ; } } catch ( e ) { reject ( e ) ; } } ) ( ) ; } } else { reject ( 'Invalid input' ) ; } } ) ; }
Run multiple commands.
@param {Array} commands - an array of objects or strings containing details of commands to run
@param {Object} options - an object containing global options for each command
@example
runMultiple(['mkdir test', 'cd test', 'ls']);
@example
runMultiple(['mkdir test', 'cd test', 'ls'], { cwd: '../myCustomWorkingDirectory' });
@example
const commandsToRun = [
{ command: 'ls', onData: function(data) { console.log(data) } },
override globalOptions working directory with each commands' options
{ command: 'mkdir test', cwd: '../../customCwdNumber2', onDone: function() { console.log('done mkdir!') } },
'ls ~/Desktop' // finish up with simple string command
];
const globalOptions = { cwd: '../myCustomWorkingDirectory' };
runMultiple(commandsToRun, globalOptions);
Run multiple commands .