From ecb7ccb8e65059998e91314ef8c4ce8f5a00a543 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 18 Aug 2024 14:08:24 +0100 Subject: [PATCH] Simplify passing options --- index.js | 10 +++++----- test.js | 9 ++++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 7eca76e..8224e45 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/test.js b/test.js index b616d6d..8938f73 100644 --- a/test.js +++ b/test.js @@ -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); @@ -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); });