Skip to content

Commit

Permalink
Add "verbose" option
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 26, 2023
1 parent a6d1141 commit 04f6c05
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions a.js
@@ -0,0 +1,6 @@
import {$} from './index.js'

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

Missing semicolon.

Check failure on line 1 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

Missing semicolon.

const $$ = $({ stdio: 'inherit' })

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

Missing semicolon.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

There should be no space after '{'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

There should be no space before '}'.

Check failure on line 3 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

Missing semicolon.

await $$`echo one`

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

Missing semicolon.

Check failure on line 5 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

Missing semicolon.
await $$`echo two`

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on ubuntu-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on macos-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 18 on windows-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on ubuntu-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on macos-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 16 on windows-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on ubuntu-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on macos-latest

Missing semicolon.

Check failure on line 6 in a.js

View workflow job for this annotation

GitHub Actions / Node.js 14 on windows-latest

Missing semicolon.
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -12,6 +12,7 @@ import {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandle
import {handleInput, getSpawnedResult, makeAllStream, validateInputSync} from './lib/stream.js';
import {mergePromise, getSpawnedPromise} from './lib/promise.js';
import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';
import {logCommand, verboseDefault} from './lib/verbose.js';

const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;

Expand Down Expand Up @@ -44,6 +45,7 @@ const handleArguments = (file, args, options = {}) => {
cleanup: true,
all: false,
windowsHide: true,
verbose: verboseDefault,
...options,
};

Expand Down Expand Up @@ -76,6 +78,7 @@ export function execa(file, args, options) {
const parsed = handleArguments(file, args, options);
const command = joinCommand(file, args);
const escapedCommand = getEscapedCommand(file, args);
logCommand(escapedCommand, parsed.options);

validateTimeout(parsed.options);

Expand Down Expand Up @@ -165,6 +168,7 @@ export function execaSync(file, args, options) {
const parsed = handleArguments(file, args, options);
const command = joinCommand(file, args);
const escapedCommand = getEscapedCommand(file, args);
logCommand(escapedCommand, parsed.options);

validateInputSync(parsed.options);

Expand Down
19 changes: 19 additions & 0 deletions lib/verbose.js
@@ -0,0 +1,19 @@
import {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`);
};
7 changes: 7 additions & 0 deletions test/fixtures/nested.js
@@ -0,0 +1,7 @@
#!/usr/bin/env node
import 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);
6 changes: 6 additions & 0 deletions test/fixtures/verbose-script.js
@@ -0,0 +1,6 @@
#!/usr/bin/env node
import {$} from '../../index.js';

const $$ = $({stdio: 'inherit'});
await $$`echo one`;
await $$`echo two`;
35 changes: 35 additions & 0 deletions test/verbose.js
@@ -0,0 +1,35 @@
import 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} echo one
one
${testTimestamp} echo two
two`);
});

0 comments on commit 04f6c05

Please sign in to comment.