Skip to content

Commit 69865cb

Browse files
authored
Merge pull request #220 from curbengh/spawn-string
fix(spawn): support string argument
2 parents cd6dcfd + 5e6d6d6 commit 69865cb

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,13 @@ Option | Description | Default
498498
`encoding` | Sets the encoding of the output string | `utf8`
499499

500500
``` js
501-
spawn('cat', 'test.txt').then(function(content){
501+
spawn('cat', 'test.txt').then((content) => {
502+
console.log(content);
503+
});
504+
505+
// $ cd "/target/folder"
506+
// $ cat "foo.txt" "bar.txt"
507+
spawn('cat', ['foo.txt', 'bar.txt'], { cwd: '/target/folder' }).then((content) => {
502508
console.log(content);
503509
});
504510
```

lib/spawn.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ const spawn = require('cross-spawn');
44
const Promise = require('bluebird');
55
const CacheStream = require('./cache_stream');
66

7-
function promiseSpawn(command, args = [], options) {
7+
function promiseSpawn(command, args = [], options = {}) {
88
if (!command) throw new TypeError('command is required!');
99

10-
if (!options && !Array.isArray(args)) {
10+
if (typeof args === 'string') args = [args];
11+
12+
if (!Array.isArray(args)) {
1113
options = args;
1214
args = [];
1315
}
1416

15-
options = options || {};
16-
1717
return new Promise((resolve, reject) => {
1818
const task = spawn(command, args, options);
1919
const verbose = options.verbose;

test/spawn.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ describe('spawn', () => {
2424

2525
it('default', () => spawn(catCommand, [fixturePath]).should.become(fixture));
2626

27+
it('default - string', () => spawn(catCommand, fixturePath).should.become(fixture));
28+
29+
it('default - empty argument and options', async () => {
30+
if (isWindows) {
31+
const out = await spawn('ver');
32+
out.trim().startsWith('Microsoft Windows').should.eql(true);
33+
} else {
34+
const out = await spawn('uname');
35+
out.trim().should.eql('Linux');
36+
}
37+
});
38+
39+
it('default - options and empty argument', async () => {
40+
if (isWindows) {
41+
const out = await spawn('chdir', { cwd: __dirname });
42+
out.trim().should.eql(__dirname);
43+
} else {
44+
const out = await spawn('pwd', { cwd: __dirname });
45+
out.trim().should.eql(__dirname);
46+
}
47+
});
48+
2749
it('command is required', () => {
2850
spawn.should.throw('command is required!');
2951
});

0 commit comments

Comments
 (0)