Skip to content

Commit

Permalink
Simplify passing options
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 18, 2024
1 parent cb4116e commit ecb7ccb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import {spawn} from 'node:child_process';
import {once} from 'node:events';
import {lineIterator, combineAsyncIterators} from './utilities.js';

export default function nanoSpawn(command, rawArguments = [], rawOptions = {}) {
const [commandArguments, {signal, timeout, nativeOptions}] = Array.isArray(rawArguments)
? [rawArguments, rawOptions]
: [[], rawArguments];
export default function nanoSpawn(command, commandArguments = [], options = {}) {
[commandArguments, options] = Array.isArray(commandArguments)
? [commandArguments, options]
: [[], commandArguments];

const subprocess = spawn(command, commandArguments, {...nativeOptions, signal, timeout});
const subprocess = spawn(command, commandArguments, options);

const promise = getResult(subprocess);

Expand Down
9 changes: 8 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const arrayFromAsync = async asyncIterable => {
return chunks;
};

const testString = 'test';

test('can pass options.argv0', async t => {
const {stdout} = await nanoSpawn('node', ['-p', 'process.argv0'], {argv0: testString});
t.is(stdout, testString);
});

test('can pass options object without any arguments', async t => {
const {exitCode, signalName} = await nanoSpawn('node', {timeout: 1});
t.is(exitCode, undefined);
Expand All @@ -37,7 +44,7 @@ test('result.exitCode|signalName on signal termination', async t => {
});

test('result.exitCode|signalName on invalid child_process options', t => {
const {exitCode, signalName} = t.throws(() => nanoSpawn('node', ['--version'], {nativeOptions: {detached: 'true'}}));
const {exitCode, signalName} = t.throws(() => nanoSpawn('node', ['--version'], {detached: 'true'}));
t.is(exitCode, undefined);
t.is(signalName, undefined);
});
Expand Down

0 comments on commit ecb7ccb

Please sign in to comment.