From bb60babf00b8ddec51c6c25c4ae042a4cfd16207 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 18 Aug 2024 11:44:43 +0100 Subject: [PATCH] Make arguments array optional --- index.js | 8 ++++++-- test.js | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4e5e75e..cbcf7c2 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,12 @@ import {spawn} from 'node:child_process'; import {lineIterator, combineAsyncIterables} from './utilities.js'; -export default function nanoSpawn(command, arguments_ = [], {signal, timeout, nativeOptions} = {}) { - const subprocess = spawn(command, arguments_, {...nativeOptions, signal, timeout}); +export default function nanoSpawn(command, rawArguments = [], rawOptions = {}) { + const [commandArguments, {signal, timeout, nativeOptions}] = Array.isArray(rawArguments) + ? [rawArguments, rawOptions] + : [[], rawArguments]; + + const subprocess = spawn(command, commandArguments, {...nativeOptions, signal, timeout}); // eslint-disable-next-line no-async-promise-executor const promise = new Promise(async (resolve, reject) => { diff --git a/test.js b/test.js index 2b4235f..da14622 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,11 @@ import test from 'ava'; import nanoSpawn from './index.js'; +test('can pass options object without any arguments', async t => { + const {exitCode} = await nanoSpawn('node', {timeout: 1}); + t.is(exitCode, null); +}); + test('can be awaited', async t => { const result = await nanoSpawn('echo', ['🦄']); // TODO