Skip to content

Instantly share code, notes, and snippets.

@bobrosoft
Last active September 9, 2021 20:21
Show Gist options
  • Save bobrosoft/bcdc0c0ba71ab55cad995cd1d7f1029a to your computer and use it in GitHub Desktop.
Save bobrosoft/bcdc0c0ba71ab55cad995cd1d7f1029a to your computer and use it in GitHub Desktop.
Node.js: writing shell scripts using modern JavaScript instead of Bash — https://medium.com/@vladimirtolstikov/774e0859f965
/**
* Executes shell command as it would happen in BASH script
* @param {string} command
* @param {Object} [options] Object with options. Set `capture` to TRUE, to capture and return stdout.
* Set `echo` to TRUE, to echo command passed.
* @returns {Promise<{code: number, data: string | undefined, error: Object}>}
*/
module.exports.exec = function (command, {capture = false, echo = false} = {}) {
command = command.replace(/\\?\n/g, ''); // need to merge multi-line commands into one string
if (echo) {
console.log(command);
}
const spawn = require('child_process').spawn;
const childProcess = spawn('bash', ['-c', command], {stdio: capture ? 'pipe' : 'inherit'});
return new Promise((resolve, reject) => {
let stdout = '';
if (capture) {
childProcess.stdout.on('data', (data) => {
stdout += data;
});
}
childProcess.on('error', function (error) {
reject({code: 1, error: error});
});
childProcess.on('close', function (code) {
if (code > 0) {
reject({code: code, error: 'Command failed with code ' + code});
} else {
resolve({code: code, data: stdout});
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment