Skip to content

Instantly share code, notes, and snippets.

@BeejLuig
Created July 5, 2019 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BeejLuig/8581aca5d4ba835af1dadea552411627 to your computer and use it in GitHub Desktop.
Save BeejLuig/8581aca5d4ba835af1dadea552411627 to your computer and use it in GitHub Desktop.
Node helper to execute multiple lines of shell script
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