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

Simplify passing options #13

Merged
merged 1 commit into from
Aug 22, 2024
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
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