diff --git a/README.md b/README.md index 718dac5..93565d8 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,15 @@ This will execute the commands `echo 1` `echo 2` and `echo 3` simultaneously. Note that on Windows, you need to use double-quotes to avoid confusing the argument parser. +#### Setting The Current Working Directory (CWD) + +To run a command in a specified directory, pass a JSON array with the first element as the CWD and the second element as the command: + +```bash +parallelshell '["/some/folder","some command"]' +``` +**Note**: Due to how JSON.parse works, string input in the arrays need to be in double quotes. + Available options: ``` -h, --help output usage information diff --git a/index.js b/index.js index c93f8a0..2f51c5b 100755 --- a/index.js +++ b/index.js @@ -98,11 +98,20 @@ if (process.platform === 'win32') { // start the children children = []; cmds.forEach(function (cmd) { + var cwd = undefined; + try { + var cmd_tmp = JSON.parse(cmd); + if (cmd_tmp.length === 2) { + cwd = cmd_tmp[0]; + cmd = cmd_tmp[1]; + } + } catch(SyntaxError) {}; // Squash this error + if (process.platform != 'win32') { cmd = "exec "+cmd; } var child = spawn(sh,[shFlag,cmd], { - cwd: process.cwd, + cwd: (cwd === undefined) ? process.cwd : cwd, env: process.env, stdio: ['pipe', process.stdout, process.stderr] })