Skip to content

Commit

Permalink
Merge pull request #220 from curbengh/spawn-string
Browse files Browse the repository at this point in the history
fix(spawn): support string argument
  • Loading branch information
curbengh authored Jul 23, 2020
2 parents cd6dcfd + 5e6d6d6 commit 69865cb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
```
Expand Down
8 changes: 4 additions & 4 deletions lib/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const spawn = require('cross-spawn');
const Promise = require('bluebird');
const CacheStream = require('./cache_stream');

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

if (!options && !Array.isArray(args)) {
if (typeof args === 'string') args = [args];

if (!Array.isArray(args)) {
options = args;
args = [];
}

options = options || {};

return new Promise((resolve, reject) => {
const task = spawn(command, args, options);
const verbose = options.verbose;
Expand Down
22 changes: 22 additions & 0 deletions test/spawn.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ describe('spawn', () => {

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

it('default - string', () => spawn(catCommand, fixturePath).should.become(fixture));

it('default - empty argument and options', async () => {
if (isWindows) {
const out = await spawn('ver');
out.trim().startsWith('Microsoft Windows').should.eql(true);
} else {
const out = await spawn('uname');
out.trim().should.eql('Linux');
}
});

it('default - options and empty argument', async () => {
if (isWindows) {
const out = await spawn('chdir', { cwd: __dirname });
out.trim().should.eql(__dirname);
} else {
const out = await spawn('pwd', { cwd: __dirname });
out.trim().should.eql(__dirname);
}
});

it('command is required', () => {
spawn.should.throw('command is required!');
});
Expand Down

0 comments on commit 69865cb

Please sign in to comment.