Created
August 8, 2018 18:29
-
-
Save naganowl/e09982fdef3a898ba2f5a55a9ebf6d86 to your computer and use it in GitHub Desktop.
Different ways to shell out in 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
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