Add `verbose` option (#532)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 58ea251 · Browse files · 8 files·+104 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+104 added−0 removed
- tests:+48 added−0 removed
- other:+56 added−0 removed
Provenance
- Commit
69ce814- Tree
d9801e4- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
58ea251
8 files changed+104 added−0 removed
added:lib/verbose.js+19 added
12345678910111213141516171819import {debuglog} from 'node:util';import process from 'node:process';
export const verboseDefault = debuglog('execa').enabled;
const padField = (field, padding) => String(field).padStart(padding, '0');
const getTimestamp = () => { const date = new Date(); return `${padField(date.getHours(), 2)}:${padField(date.getMinutes(), 2)}:${padField(date.getSeconds(), 2)}.${padField(date.getMilliseconds(), 3)}`;};
export const logCommand = (escapedCommand, {verbose}) => { if (!verbose) { return; }
process.stderr.write(`[${getTimestamp()}] ${escapedCommand}\n`);};added:test/fixtures/nested.js+7 added
1234567#!/usr/bin/env nodeimport process from 'node:process';import {execa} from '../../index.js';
const [options, file, ...args] = process.argv.slice(2);const nestedOptions = {stdio: 'inherit', ...JSON.parse(options)};await execa(file, args, nestedOptions);added:test/fixtures/verbose-script.js+6 added
123456#!/usr/bin/env nodeimport {$} from '../../index.js';
const $$ = $({stdio: 'inherit'});await $$`node -p "one"`;await $$`node -p "two"`;added:test/verbose.js+35 added
1234567891011121314151617181920212223242526272829303132333435import test from 'ava';import {execa} from '../index.js';import {setFixtureDir} from './helpers/fixtures-dir.js';
setFixtureDir();
const normalizeTimestamp = output => output.replace(/\d/g, '0');const testTimestamp = '[00:00:00.000]';
test('Prints command when "verbose" is true', async t => { const {stdout, stderr, all} = await execa('nested.js', [JSON.stringify({verbose: true}), 'noop.js', 'test'], {all: true}); t.is(stdout, 'test'); t.is(normalizeTimestamp(stderr), `${testTimestamp} noop.js test`); t.is(normalizeTimestamp(all), `${testTimestamp} noop.js test\ntest`);});
test('Prints command with NODE_DEBUG=execa', async t => { const {stdout, stderr, all} = await execa('nested.js', [JSON.stringify({}), 'noop.js', 'test'], {all: true, env: {NODE_DEBUG: 'execa'}}); t.is(stdout, 'test'); t.is(normalizeTimestamp(stderr), `${testTimestamp} noop.js test`); t.is(normalizeTimestamp(all), `${testTimestamp} noop.js test\ntest`);});
test('Escape verbose command', async t => { const {stderr} = await execa('nested.js', [JSON.stringify({verbose: true}), 'noop.js', 'one two', '"'], {all: true}); t.true(stderr.endsWith('"one two" "\\""'));});
test('Verbose option works with inherit', async t => { const {all} = await execa('verbose-script.js', {all: true, env: {NODE_DEBUG: 'execa'}}); t.is(normalizeTimestamp(all), `${testTimestamp} node -p "\\"one\\""one${testTimestamp} node -p "\\"two\\""two`);});modified:index.d.ts+9 added
242 unmodified lines243 244 @default true245 */246 readonly windowsHide?: boolean;247+248+ /**249+ Print each command on `stderr` before executing it.250+251+ This can also be enabled by setting the `NODE_DEBUG=execa` environment variable in the current process.252+253+ @default false254+ */255+ readonly verbose?: boolean;256 };257 258 export type Options<EncodingType = string> = {259 /**497 unmodified linesmodified:index.js+4 added
10 unmodified lines11 import {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandler} from './lib/kill.js';12 import {handleInput, getSpawnedResult, makeAllStream, validateInputSync} from './lib/stream.js';13 import {mergePromise, getSpawnedPromise} from './lib/promise.js';14 import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';15+import {logCommand, verboseDefault} from './lib/verbose.js';16 17 const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;18 19 const getEnv = ({env: envOption, extendEnv, preferLocal, localDir, execPath}) => {24 unmodified lines44 reject: true,45 cleanup: true,46 all: false,47 windowsHide: true,48+ verbose: verboseDefault,49 ...options,50 };51 52 options.env = getEnv(options);24 unmodified lines77 export function execa(file, args, options) {78 const parsed = handleArguments(file, args, options);79 const command = joinCommand(file, args);80 const escapedCommand = getEscapedCommand(file, args);81+ logCommand(escapedCommand, parsed.options);82 83 validateTimeout(parsed.options);84 85 let spawned;81 unmodified lines167 export function execaSync(file, args, options) {168 const parsed = handleArguments(file, args, options);169 const command = joinCommand(file, args);170 const escapedCommand = getEscapedCommand(file, args);171+ logCommand(escapedCommand, parsed.options);172 173 validateInputSync(parsed.options);174 175 let result;116 unmodified linesmodified:index.test-d.ts+1 added
148 unmodified lines149 execa('unicorns', {killSignal: 9});150 execa('unicorns', {signal: new AbortController().signal});151 execa('unicorns', {windowsVerbatimArguments: true});152 execa('unicorns', {windowsHide: false});153+execa('unicorns', {verbose: false});154 /* eslint-enable @typescript-eslint/no-floating-promises */155 execa('unicorns').kill();156 execa('unicorns').kill('SIGKILL');157 execa('unicorns').kill(undefined);121 unmodified linesmodified:readme.md+23 added
99 unmodified lines100 $({stdio: 'inherit'}).sync`echo ${unicorns} rainbows`;101 //=> 'unicorns rainbows'102 ```103 104+#### Verbose mode105+106+```sh107+> node file.js108+unicorns109+rainbows110+111+> NODE_DEBUG=execa node file.js112+[16:50:03.305] echo unicorns113+unicorns114+[16:50:03.308] echo rainbows115+rainbows116+```117+118 ### Pipe the child process stdout to the parent119 120 ```js121 import {execa} from 'execa';543 unmodified lines665 Default: `true`666 667 On Windows, do not create a new console window. Please note this also prevents `CTRL-C` [from working](https://github.com/nodejs/node/issues/29837) on Windows.668 669+#### verbose670+671+Type: `boolean`\672+Default: `false`673+674+[Print each command](#verbose-mode) on `stderr` before executing it.675+676+This can also be enabled by setting the `NODE_DEBUG=execa` environment variable in the current process.677+678 #### nodePath *(For `.node()` only)*679 680 Type: `string`\681 Default: [`process.execPath`](https://nodejs.org/api/process.html#process_process_execpath)92 unmodified lines