From a2905ed4635207c092a88153f9939b02bd244967 Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Thu, 23 Jul 2020 02:11:59 +0100 Subject: [PATCH] fix(spawn): support string argument --- README.md | 8 +++++++- lib/spawn.js | 2 ++ test/spawn.spec.js | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e072147..7166a3d0 100644 --- a/README.md +++ b/README.md @@ -498,7 +498,13 @@ Option | Description | Default `encoding` | Sets the encoding of the output string | `utf8` ``` js -spawn('cat', 'test.txt').then(function(content){ +spawn('cat', 'test.txt').then((content) => { + console.log(content); +}); + +// $ cd "/target/folder" +// $ cat "foo.txt" "bar.txt" +spawn('cat', ['foo.txt', 'bar.txt'], { cwd: '/target/folder' }).then((content) => { console.log(content); }); ``` diff --git a/lib/spawn.js b/lib/spawn.js index 13d2c4af..a9dbcf59 100644 --- a/lib/spawn.js +++ b/lib/spawn.js @@ -7,6 +7,8 @@ const CacheStream = require('./cache_stream'); function promiseSpawn(command, args = [], options) { if (!command) throw new TypeError('command is required!'); + if (typeof args === 'string') args = [args]; + if (!options && !Array.isArray(args)) { options = args; args = []; diff --git a/test/spawn.spec.js b/test/spawn.spec.js index f2911f3a..e1dc0ba8 100644 --- a/test/spawn.spec.js +++ b/test/spawn.spec.js @@ -24,6 +24,8 @@ describe('spawn', () => { it('default', () => spawn(catCommand, [fixturePath]).should.become(fixture)); + it('default - string', () => spawn(catCommand, fixturePath).should.become(fixture)); + it('command is required', () => { spawn.should.throw('command is required!'); });