Created
January 24, 2022 18:58
-
-
Save felippe-regazio/2997254d4fcbc839842bc58df38c86d2 to your computer and use it in GitHub Desktop.
Execute a pipe of commands on linux or windows using Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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