Created
July 5, 2019 17:56
-
-
Save BeejLuig/8581aca5d4ba835af1dadea552411627 to your computer and use it in GitHub Desktop.
Node helper to execute multiple lines of shell script
This file contains 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
const { execSync: exec } = require('child_process'); | |
/** | |
Executes multiple lines of script. | |
@param {String} scriptStr - A single or multi-line string of executable code | |
@example | |
execute(` | |
echo 'Hello' | |
echo 'World' | |
echo '!' | |
`) | |
*/ | |
function execute(scriptsStr) { | |
const scripts = scriptsStr | |
.split(/[\n]/) | |
.map(s => s.trim()) | |
.filter(Boolean); | |
scripts.forEach((script) => { | |
exec(script, (err, stdout) => { | |
if (err) { | |
console.error('Script failed with error:', err); | |
return; | |
} | |
console.log(stdout); | |
}); | |
}); | |
} | |
module.exports = execute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment