object-to-spawn-args
Advanced tools
| var toSpawnArgs = require("../"); | ||
| var spawn = require("child_process").spawn; | ||
| var options = { | ||
| l: true, | ||
| a: true | ||
| }; | ||
| spawn("ls", toSpawnArgs(options), { stdio: "inherit" }); |
+16
-5
@@ -7,8 +7,19 @@ module.exports = function(object, options){ | ||
| var value = object[prop]; | ||
| if (value){ | ||
| output.push("--" + prop); | ||
| if (options.quote){ | ||
| output.push("\"" + value + "\""); | ||
| if (value !== undefined){ | ||
| if (prop.length === 1){ | ||
| output.push("-" + prop); | ||
| if (value !== true){ | ||
| if (options.quote){ | ||
| output.push("\"" + value + "\""); | ||
| } else { | ||
| output.push(value); | ||
| } | ||
| } | ||
| } else { | ||
| output.push(value); | ||
| output.push("--" + prop); | ||
| if (options.quote){ | ||
| output.push("\"" + value + "\""); | ||
| } else { | ||
| output.push(value); | ||
| } | ||
| } | ||
@@ -15,0 +26,0 @@ } |
+1
-1
| { | ||
| "name": "object-to-spawn-args", | ||
| "version": "0.0.0", | ||
| "version": "0.1.0", | ||
| "description": "Converts an object to a child_process.spawn args array", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/75lb/object-to-spawn-args", |
+40
-3
@@ -5,5 +5,42 @@ [](https://www.npmjs.org/package/object-to-spawn-args) | ||
| [](https://david-dm.org/75lb/object-to-spawn-args) | ||
|  | ||
| object-to-spawn-args | ||
| ==================== | ||
| #object-to-spawn-args | ||
| Converts an options object to an array suitable for passing to `child_process.spawn()`. | ||
| Single letter object properties (e.g. `c: "red"`) convert to short-option args (e.g. `-c red`). Longer object properties (e.g. `colour: "red"`) convert to long-option args (e.g. `--colour red`). Object property values equalling `true` convert to flags (e.g. `-l`). | ||
| This options object: | ||
| ```js | ||
| var options = { | ||
| l: true, | ||
| c: "red", | ||
| man: "pete", | ||
| tramp: true | ||
| } | ||
| ``` | ||
| converts to | ||
| ```js | ||
| [ "-l", "-c", "red", "--man", "pete", "--tramp" ] | ||
| ``` | ||
| ##Installation | ||
| Move into your project directory then run: | ||
| ```sh | ||
| $ npm install object-to-spawn-args --save | ||
| ``` | ||
| *Mac / Linux users may need to run with `sudo`*. | ||
| ##Usage | ||
| ```js | ||
| var toSpawnArgs = require("object-to-spawn-args"); | ||
| var spawn = require("child_process").spawn; | ||
| var options = { | ||
| l: true, | ||
| a: true | ||
| }; | ||
| spawn("ls", toSpawnArgs(options), { stdio: "inherit" }); | ||
| ``` |
+11
-2
| var test = require("tap").test; | ||
| var toSpawnArgs = require("../"); | ||
| test("simple", function(t){ | ||
| test("long option", function(t){ | ||
| var object = { | ||
@@ -13,4 +13,13 @@ one: 1, | ||
| test("simple", function(t){ | ||
| test("short option", function(t){ | ||
| var object = { | ||
| l: true, | ||
| c: "red" | ||
| }; | ||
| t.deepEqual(toSpawnArgs(object), [ "-l", "-c", "red" ]); | ||
| t.end(); | ||
| }); | ||
| test("with quotes", function(t){ | ||
| var object = { | ||
| one: 1, | ||
@@ -17,0 +26,0 @@ two: 2 |
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
3461
82.45%6
20%60
76.47%46
411.11%1
Infinity%