Skip to content
Myr

Print process completion with verbose option (#887)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 5abc429 · Browse files · 16 files+211 added−26 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

16 files+211 added−26 removed

  • tests:+121 added−1 removed
  • other:+90 added−25 removed

16 files+211 added−26 removed

modified:docs/scripts.md+1 added
419 unmodified lines
420421```422[19:49:00.360] [0] $ echo example423example424[19:49:00.383] [0] √ (done in 23ms)425```426427### Piping stdout to another command428
290 unmodified lines
modified:lib/return/early-error.js+3 added-3 removed
modified:lib/return/output.js+4 added-1 removed
added:lib/verbose/complete.js+37 added
modified:lib/verbose/info.js+5 added-1 removed
modified:lib/verbose/log.js+3 added
modified:lib/async.js+18 added-11 removed
7 unmodified lines
8import {spawnedKill} from './exit/kill.js';9import {cleanupOnExit} from './exit/cleanup.js';10import {pipeToProcess} from './pipe/setup.js';11import {PROCESS_OPTIONS} from './pipe/validate.js';12import {logEarlyResult} from './verbose/complete.js';13import {makeAllStream} from './stream/all.js';14import {getSpawnedResult} from './stream/resolve.js';15import {mergePromise} from './promise.js';1617export const execa = (rawFile, rawArgs, rawOptions) => {17 const {file, args, command, escapedCommand, options, stdioStreamsGroups, stdioState} = handleAsyncArguments(rawFile, rawArgs, rawOptions);18 const {spawned, promise} = spawnProcessAsync({file, args, options, command, escapedCommand, stdioStreamsGroups, stdioState});18 const {file, args, command, escapedCommand, verboseInfo, options, stdioStreamsGroups, stdioState} = handleAsyncArguments(rawFile, rawArgs, rawOptions);19 const {spawned, promise} = spawnProcessAsync({file, args, options, verboseInfo, command, escapedCommand, stdioStreamsGroups, stdioState});20 spawned.pipe = pipeToProcess.bind(undefined, {source: spawned, sourcePromise: promise, stdioStreamsGroups, destinationOptions: {}});21 mergePromise(spawned, promise);22 PROCESS_OPTIONS.set(spawned, options);23 return spawned;
1 unmodified line
2526const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => {27 [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);28 const {command, escapedCommand, verboseInfo} = handleCommand(rawFile, rawArgs, rawOptions);2930 try {28 const {file, args, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);29 const options = handleAsyncOptions(normalizedOptions);30 const {stdioStreamsGroups, stdioState} = handleInputAsync(options, verboseInfo);31 return {file, args, command, escapedCommand, options, stdioStreamsGroups, stdioState};31 const {file, args, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);32 const options = handleAsyncOptions(normalizedOptions);33 const {stdioStreamsGroups, stdioState} = handleInputAsync(options, verboseInfo);34 return {file, args, command, escapedCommand, verboseInfo, options, stdioStreamsGroups, stdioState};35 } catch (error) {36 logEarlyResult(error, verboseInfo);37 throw error;38 }39};4041// Prevent passing the `timeout` option directly to `child_process.spawn()`42const handleAsyncOptions = ({timeout, signal, cancelSignal, ...options}) => {
3 unmodified lines
4647 return {...options, timeoutDuration: timeout, signal: cancelSignal};48};4943const spawnProcessAsync = ({file, args, options, command, escapedCommand, stdioStreamsGroups, stdioState}) => {50const spawnProcessAsync = ({file, args, options, verboseInfo, command, escapedCommand, stdioStreamsGroups, stdioState}) => {51 let spawned;52 try {53 spawned = spawn(file, args, options);54 } catch (error) {48 return handleEarlyError({error, command, escapedCommand, stdioStreamsGroups, options});55 return handleEarlyError({error, command, escapedCommand, stdioStreamsGroups, options, verboseInfo});56 }5758 const controller = new AbortController();59 setMaxListeners(Number.POSITIVE_INFINITY, controller.signal);
4 unmodified lines
6465 spawned.kill = spawnedKill.bind(undefined, {kill: spawned.kill.bind(spawned), spawned, options, controller});66 spawned.all = makeAllStream(spawned, options);6761 const promise = handlePromise({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller});68 const promise = handlePromise({spawned, options, verboseInfo, stdioStreamsGroups, originalStreams, command, escapedCommand, controller});69 return {spawned, promise};70};7165const handlePromise = async ({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller}) => {72const handlePromise = async ({spawned, options, verboseInfo, stdioStreamsGroups, originalStreams, command, escapedCommand, controller}) => {73 const context = {timedOut: false};7475 const [76 errorInfo,
5 unmodified lines
8283 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));84 const all = handleOutput(options, allResult);85 const result = getAsyncResult({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand});79 return handleResult(result, options);86 return handleResult(result, verboseInfo, options);87};8889const getAsyncResult = ({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand}) => 'error' in errorInfo90 ? makeError({
12 unmodified lines
modified:lib/sync.js+14 added-7 removed
1 unmodified line
2import {normalizeArguments, handleCommand, handleArguments} from './arguments/options.js';3import {makeError, makeEarlyError, makeSuccessResult} from './return/error.js';4import {handleOutput, handleResult} from './return/output.js';5import {handleInputSync, pipeOutputSync} from './stdio/sync.js';6import {logEarlyResult} from './verbose/complete.js';7import {isFailedExit} from './exit/code.js';89export const execaSync = (rawFile, rawArgs, rawOptions) => {9 const {file, args, command, escapedCommand, options, stdioStreamsGroups} = handleSyncArguments(rawFile, rawArgs, rawOptions);10 const {file, args, command, escapedCommand, verboseInfo, options, stdioStreamsGroups} = handleSyncArguments(rawFile, rawArgs, rawOptions);11 const result = spawnProcessSync({file, args, options, command, escapedCommand, stdioStreamsGroups});11 return handleResult(result, options);12 return handleResult(result, verboseInfo, options);13};1415const handleSyncArguments = (rawFile, rawArgs, rawOptions) => {16 [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);17 const {command, escapedCommand, verboseInfo} = handleCommand(rawFile, rawArgs, rawOptions);1819 try {17 const syncOptions = normalizeSyncOptions(rawOptions);18 const {file, args, options} = handleArguments(rawFile, rawArgs, syncOptions);19 validateSyncOptions(options);20 const stdioStreamsGroups = handleInputSync(options, verboseInfo);21 return {file, args, command, escapedCommand, options, stdioStreamsGroups};20 const syncOptions = normalizeSyncOptions(rawOptions);21 const {file, args, options} = handleArguments(rawFile, rawArgs, syncOptions);22 validateSyncOptions(options);23 const stdioStreamsGroups = handleInputSync(options, verboseInfo);24 return {file, args, command, escapedCommand, verboseInfo, options, stdioStreamsGroups};25 } catch (error) {26 logEarlyResult(error, verboseInfo);27 throw error;28 }29};3031const normalizeSyncOptions = options => options.node && !options.ipc ? {...options, ipc: false} : options;32
34 unmodified lines
added:test/fixtures/nested-fail.js+8 added
1#!/usr/bin/env node2import process from 'node:process';3import {execa} from '../../index.js';45const [options, file, ...args] = process.argv.slice(2);6const childProcess = execa(file, args, JSON.parse(options));7childProcess.kill(new Error(args[0]));8await childProcess;
added:test/fixtures/noop-continuous.js+9 added
1#!/usr/bin/env node2import process from 'node:process';3import {setTimeout} from 'node:timers/promises';45for (const character of process.argv[2]) {6 console.log(character);7 // eslint-disable-next-line no-await-in-loop8 await setTimeout(100);9}
modified:test/helpers/verbose.js+10 added-1 removed
19 unmodified lines
20 t.true(stderr.includes('exit code 2'));21 return stderr;22};2324export const runWarningProcess = async (t, execaMethod) => {25 const {stderr} = await execaMethod('noop-fail.js', ['1', foobarString], {verbose: 'short', reject: false});26 return stderr;27};2829export const runEarlyErrorProcess = async (t, execaMethod) => {30 const {stderr} = await t.throwsAsync(execaMethod('noop.js', [foobarString], {verbose: 'short', cwd: true}));31 t.true(stderr.includes('The "cwd" option must'));32 return stderr;
4 unmodified lines
37const isCommandLine = line => line.includes(' $ ') || line.includes(' | ');38export const getOutputLine = stderr => getOutputLines(stderr)[0];39export const getOutputLines = stderr => getNormalizedLines(stderr).filter(line => isOutputLine(line));40const isOutputLine = line => line.includes('] ');41export const getCompletionLine = stderr => getCompletionLines(stderr)[0];42export const getCompletionLines = stderr => getNormalizedLines(stderr).filter(line => isCompletionLine(line));43const isCompletionLine = line => line.includes('(done in');44export const getNormalizedLines = stderr => splitLines(normalizeStderr(stderr));45const splitLines = stderr => stderr.split('\n');4639const normalizeStderr = stderr => normalizeTimestamp(stripVTControlCharacters(stderr));47const normalizeStderr = stderr => normalizeDuration(normalizeTimestamp(stripVTControlCharacters(stderr)));48export const testTimestamp = '[00:00:00.000]';49const normalizeTimestamp = stderr => stderr.replaceAll(/^\[\d{2}:\d{2}:\d{2}.\d{3}]/gm, testTimestamp);50const normalizeDuration = stderr => stderr.replaceAll(/\(done in [^)]+\)/g, '(done in 0ms)');5152export const getVerboseOption = (isVerbose, verbose = 'short') => ({verbose: isVerbose ? verbose : 'none'});
added:test/verbose/complete.js+92 added
1import {stripVTControlCharacters} from 'node:util';2import test from 'ava';3import {execa} from '../../index.js';4import {setFixtureDir} from '../helpers/fixtures-dir.js';5import {foobarString} from '../helpers/input.js';6import {7 nestedExecaAsync,8 nestedExecaSync,9 runErrorProcess,10 runWarningProcess,11 runEarlyErrorProcess,12 getCompletionLine,13 getCompletionLines,14 testTimestamp,15 getVerboseOption,16} from '../helpers/verbose.js';1718setFixtureDir();1920const testPrintCompletion = async (t, verbose, execaMethod) => {21 const {stderr} = await execaMethod('noop.js', [foobarString], {verbose});22 t.is(getCompletionLine(stderr), `${testTimestamp} [0] √ (done in 0ms)`);23};2425test('Prints completion, verbose "short"', testPrintCompletion, 'short', nestedExecaAsync);26test('Prints completion, verbose "full"', testPrintCompletion, 'full', nestedExecaAsync);27test('Prints completion, verbose "short", sync', testPrintCompletion, 'short', nestedExecaSync);28test('Prints completion, verbose "full", sync', testPrintCompletion, 'full', nestedExecaSync);2930const testNoPrintCompletion = async (t, execaMethod) => {31 const {stderr} = await execaMethod('noop.js', [foobarString], {verbose: 'none'});32 t.is(stderr, '');33};3435test('Does not print completion, verbose "none"', testNoPrintCompletion, nestedExecaAsync);36test('Does not print completion, verbose "none", sync', testNoPrintCompletion, nestedExecaSync);3738const testPrintCompletionError = async (t, execaMethod) => {39 const stderr = await runErrorProcess(t, 'short', execaMethod);40 t.is(getCompletionLine(stderr), `${testTimestamp} [0] × (done in 0ms)`);41};4243test('Prints completion after errors', testPrintCompletionError, nestedExecaAsync);44test('Prints completion after errors, sync', testPrintCompletionError, nestedExecaSync);4546const testPrintCompletionWarning = async (t, execaMethod) => {47 const stderr = await runWarningProcess(t, execaMethod);48 t.is(getCompletionLine(stderr), `${testTimestamp} [0] ‼ (done in 0ms)`);49};5051test('Prints completion after errors, "reject" false', testPrintCompletionWarning, nestedExecaAsync);52test('Prints completion after errors, "reject" false, sync', testPrintCompletionWarning, nestedExecaSync);5354const testPrintCompletionEarly = async (t, execaMethod) => {55 const stderr = await runEarlyErrorProcess(t, execaMethod);56 t.is(getCompletionLine(stderr), `${testTimestamp} [0] × (done in 0ms)`);57};5859test('Prints completion after early validation errors', testPrintCompletionEarly, nestedExecaAsync);60test('Prints completion after early validation errors, sync', testPrintCompletionEarly, nestedExecaSync);6162test.serial('Prints duration', async t => {63 const {stderr} = await nestedExecaAsync('delay.js', ['1000'], {verbose: 'short'});64 t.regex(stripVTControlCharacters(stderr).split('\n').at(-1), /\(done in [\d.]+s\)/);65});6667const testPipeDuration = async (t, fixtureName, sourceVerbose, destinationVerbose) => {68 const {stderr} = await execa(`nested-pipe-${fixtureName}.js`, [69 JSON.stringify(getVerboseOption(sourceVerbose)),70 'noop.js',71 foobarString,72 JSON.stringify(getVerboseOption(destinationVerbose)),73 'stdin.js',74 ]);7576 const lines = getCompletionLines(stderr);77 t.is(lines.includes(`${testTimestamp} [0] √ (done in 0ms)`), sourceVerbose || destinationVerbose);78 t.is(lines.includes(`${testTimestamp} [1] √ (done in 0ms)`), sourceVerbose && destinationVerbose);79};8081test('Prints both durations piped with .pipe("file")', testPipeDuration, 'file', true, true);82test('Prints both durations piped with .pipe`command`', testPipeDuration, 'script', true, true);83test('Prints both durations piped with .pipe(childProcess)', testPipeDuration, 'process', true, true);84test('Prints first duration piped with .pipe("file")', testPipeDuration, 'file', true, false);85test('Prints first duration piped with .pipe`command`', testPipeDuration, 'script', true, false);86test('Prints first duration piped with .pipe(childProcess)', testPipeDuration, 'process', true, false);87test('Prints second duration piped with .pipe("file")', testPipeDuration, 'file', false, true);88test('Prints second duration piped with .pipe`command`', testPipeDuration, 'script', false, true);89test('Prints second duration piped with .pipe(childProcess)', testPipeDuration, 'process', false, true);90test('Prints neither durations piped with .pipe("file")', testPipeDuration, 'file', false, false);91test('Prints neither durations piped with .pipe`command`', testPipeDuration, 'script', false, false);92test('Prints neither durations piped with .pipe(childProcess)', testPipeDuration, 'process', false, false);
modified:test/verbose/info.js+2 added
17 unmodified lines
18 const {all} = await execa('verbose-script.js', {env: {NODE_DEBUG: 'execa'}, all: true});19 t.deepEqual(getNormalizedLines(all), [20 `${testTimestamp} [0] $ node -e ${QUOTE}console.error(1)${QUOTE}`,21 '1',22 `${testTimestamp} [0] √ (done in 0ms)`,23 `${testTimestamp} [1] $ node -e ${QUOTE}process.exit(2)${QUOTE}`,24 `${testTimestamp} [1] ‼ (done in 0ms)`,25 ]);26});2728test('NODE_DEBUG=execa changes verbose default value to "full"', async t => {
13 unmodified lines
modified:index.d.ts+1 added-1 removed
541 unmodified lines
542 */543 readonly windowsHide?: boolean;544545 /**546 If `verbose` is `'short'` or `'full'`, prints each command on `stderr` before executing it.546 If `verbose` is `'short'` or `'full'`, prints each command on `stderr` before executing it. When the command completes, prints its duration.547548 If `verbose` is `'full'`, the command's `stdout` and `stderr` are printed too, unless either:549 - the `stdout`/`stderr` option is `ignore` or `inherit`.550 - the `stdout`/`stderr` is redirected to [a stream](https://nodejs.org/api/stream.html#readablepipedestination-options), a file, a file descriptor, or another child process.
835 unmodified lines
modified:package.json+1 added
50 unmodified lines
51 "human-signals": "^6.0.0",52 "is-plain-obj": "^4.1.0",53 "is-stream": "^4.0.1",54 "npm-run-path": "^5.2.0",55 "pretty-ms": "^9.0.0",56 "signal-exit": "^4.1.0",57 "strip-final-newline": "^4.0.0"58 },59 "devDependencies": {
36 unmodified lines
modified:readme.md+3 added-1 removed
142 unmodified lines
143144> NODE_DEBUG=execa node file.js145[19:49:00.360] [0] $ echo unicorns146unicorns147[19:49:00.383] [0] √ (done in 23ms)148[19:49:00.383] [1] $ echo rainbows149rainbows150[19:49:00.404] [1] √ (done in 21ms)151```152153### Input/output154
513 unmodified lines
668669Type: `'none' | 'short' | 'full'`\670Default: `'none'`671670If `verbose` is `'short'` or `'full'`, [prints each command](#verbose-mode) on `stderr` before executing it.672If `verbose` is `'short'` or `'full'`, [prints each command](#verbose-mode) on `stderr` before executing it. When the command completes, prints its duration.673674If `verbose` is `'full'`, the command's `stdout` and `stderr` are printed too, unless either:675- the [`stdout`](#stdout-1)/[`stderr`](#stderr-1) option is `ignore` or `inherit`.676- the `stdout`/`stderr` is redirected to [a stream](https://nodejs.org/api/stream.html#readablepipedestination-options), [a file](#stdout-1), a file descriptor, or [another child process](#pipefile-arguments-options).
373 unmodified lines