Skip to content

Instantly share code, notes, and snippets.

@naganowl
Created August 8, 2018 18:29
Show Gist options
  • Save naganowl/e09982fdef3a898ba2f5a55a9ebf6d86 to your computer and use it in GitHub Desktop.
Save naganowl/e09982fdef3a898ba2f5a55a9ebf6d86 to your computer and use it in GitHub Desktop.
Different ways to shell out in node
const childProcess = require('child_process');
function runShell(cmd, args, options) {
const output = childProcess.spawnSync(cmd, args, options);
return output.stdout.toString();
};
// 1. With just the base command
const findCmd = `find app/assets -type f -name *.js -not \( -path *spec* \) -o -name *.coffee -not \( -path *spec* \)`;
const cmdArr = findCmd.split(' ');
let sourceFiles = runShell(cmdArr[0], cmdArr.slice(1));
// 2. Using `sh`
sourceFiles = runShell('sh', ['-c', `find app/assets -type f -name '*.js' -not \\( -path '*spec*' \\) -o -name '*.coffee' -not \\( -path '*spec*' \\)`]);
// NOTES
// Quotes are dropped for file extensions in #1
// Need to double escape slashes for parens in #2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment