I am trying to run the following shell command from within a Node application:
git grep MySearchString -- 'MyPathToSearchFor'
This command runs successfully using's node's child_process.exec:
exec("git grep MySearchString -- 'MyPathToSearchFor'") // succeeds!
However, I cannot get the command working using child_process.spawn, which requires arguments to be provided as an array of strings.
// Spawn works without the pathspec args:
spawn('git', ['grep', 'MySearchString']) // success! exits with code 0
// It fails when the pathspec args are provided:
spawn('git', ['grep', 'MySearchString', '--', "'MyPathToSearchFor'"]) // exits with code 1
spawn('git', ['grep', 'MySearchString', "-- 'MyPathToSearchFor'"]) // exits with code 128
How can I provide spawn the -- MyPathToSearchFor args? How should I translate the dash-dash and pathspec args into spawn parameters?
The problem seems related to quotes in the args, but I'm not sure how to handle those.
spawn('git', ['grep', 'MySearchString', '--', 'MyPathToSearchFor'])should be the correct form