Skip to content

Commit

Permalink
Fix verbose option
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Dec 14, 2023
1 parent e67f3e0 commit 887ecce
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/verbose.js
@@ -1,3 +1,4 @@
import {writeFileSync} from 'node:fs';
import {debuglog} from 'node:util';
import process from 'node:process';

Expand All @@ -15,5 +16,7 @@ export const logCommand = (escapedCommand, {verbose}) => {
return;
}

process.stderr.write(`[${getTimestamp()}] ${escapedCommand}\n`);
// Write synchronously to ensure it is written before spawning the child process.
// This guarantees this line is written to `stderr` before the child process prints anything.
writeFileSync(process.stderr.fd, `[${getTimestamp()}] ${escapedCommand}\n`);
};
4 changes: 2 additions & 2 deletions test/fixtures/verbose-script.js
Expand Up @@ -2,5 +2,5 @@
import {$} from '../../index.js';

const $$ = $({stdio: 'inherit'});
await $$`node -e console.error("one")`;
await $$`node -e console.error("two")`;
await $$`node -p "one"`;
await $$`node -p "two"`;
12 changes: 7 additions & 5 deletions test/verbose.js
Expand Up @@ -8,15 +8,17 @@ const normalizeTimestamp = output => output.replaceAll(/\d/g, '0');
const testTimestamp = '[00:00:00.000]';

test('Prints command when "verbose" is true', async t => {
const {stdout, stderr} = await execa('nested.js', [JSON.stringify({verbose: true}), 'noop.js', 'test'], {all: true});
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} = await execa('nested.js', [JSON.stringify({}), 'noop.js', 'test'], {all: true, env: {NODE_DEBUG: 'execa'}});
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 => {
Expand All @@ -25,9 +27,9 @@ test('Escape verbose command', async t => {
});

test('Verbose option works with inherit', async t => {
const {stderr} = await execa('verbose-script.js', {all: true, env: {NODE_DEBUG: 'execa'}});
t.is(normalizeTimestamp(stderr), `${testTimestamp} node -e "console.error(\\"one\\")"
const {all} = await execa('verbose-script.js', {all: true, env: {NODE_DEBUG: 'execa'}});
t.is(normalizeTimestamp(all), `${testTimestamp} node -p "\\"one\\""
one
${testTimestamp} node -e "console.error(\\"two\\")"
${testTimestamp} node -p "\\"two\\""
two`);
});

0 comments on commit 887ecce

Please sign in to comment.