Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spawn): support string argument #220

Merged
merged 3 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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