Skip to content

Instantly share code, notes, and snippets.

@felippe-regazio
Created January 24, 2022 18:58
Show Gist options
  • Save felippe-regazio/2997254d4fcbc839842bc58df38c86d2 to your computer and use it in GitHub Desktop.
Save felippe-regazio/2997254d4fcbc839842bc58df38c86d2 to your computer and use it in GitHub Desktop.

Revisions

  1. felippe-regazio created this gist Jan 24, 2022.
    31 changes: 31 additions & 0 deletions exec-pipe.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #!/usr/bin/env node

    const exec = require('child_process').exec;

    /**
    * Cross-platform command execution script with promise pipe.
    * If any part of pipe fail, the entire pipe will also fail.
    *
    * @param command string
    * @returns Promise<String>
    */
    const run = command => new Promise(resolve => {
    exec(command, (error, stdout) => {
    if (error) {
    console.error(error);

    process.exit(1);
    }

    console.log(stdout);
    resolve(stdout);
    });
    });

    (async () => {
    run('echo "Command 1"')
    .then(async () => run('echo "Command 2"'))
    .then(async () => run('echo "Command 3"'))
    .then(async () => run('echo "And so on..."'))
    .then(() => console.log('All commands runned'));
    })();