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.
Execute a pipe of commands on linux or windows using Node
#!/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'));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment