diff --git a/index.js b/index.js index c8739e4549..5c8f962363 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ const npmRunPath = require('npm-run-path'); const onetime = require('onetime'); const makeError = require('./lib/error'); const normalizeStdio = require('./lib/stdio'); -const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler} = require('./lib/kill'); +const {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandler} = require('./lib/kill'); const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream.js'); const {mergePromise, getSpawnedPromise} = require('./lib/promise.js'); const {joinCommand, parseCommand} = require('./lib/command.js'); @@ -75,6 +75,8 @@ const execa = (file, args, options) => { const parsed = handleArguments(file, args, options); const command = joinCommand(file, args); + validateTimeout(parsed.options); + let spawned; try { spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options); diff --git a/lib/kill.js b/lib/kill.js index 7f6f8dd7f2..287a14238e 100644 --- a/lib/kill.js +++ b/lib/kill.js @@ -71,10 +71,6 @@ const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise return spawnedPromise; } - if (!Number.isFinite(timeout) || timeout < 0) { - throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`); - } - let timeoutId; const timeoutPromise = new Promise((resolve, reject) => { timeoutId = setTimeout(() => { @@ -89,6 +85,12 @@ const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise return Promise.race([timeoutPromise, safeSpawnedPromise]); }; +const validateTimeout = ({timeout}) => { + if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) { + throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`); + } +}; + // `cleanup` option handling const setExitHandler = async (spawned, {cleanup, detached}, timedPromise) => { if (!cleanup || detached) { @@ -108,5 +110,6 @@ module.exports = { spawnedKill, spawnedCancel, setupTimeout, + validateTimeout, setExitHandler };