Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add result.durationMs #30

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ export default function nanoSpawn(file, commandArguments = [], options = {}) {
[commandArguments, options] = Array.isArray(commandArguments)
? [commandArguments, options]
: [[], commandArguments];
const start = process.hrtime.bigint();
const command = getCommand(file, commandArguments);

const subprocess = spawn(file, commandArguments, getOptions(options));

const promise = getResult(subprocess, command);
const promise = getResult(subprocess, start, command);

const stdoutLines = lineIterator(subprocess.stdout);
const stderrLines = lineIterator(subprocess.stderr);
Expand Down Expand Up @@ -49,20 +50,20 @@ const getCommandPart = part => {
: part;
};

const getResult = async (subprocess, command) => {
const getResult = async (subprocess, start, command) => {
const result = {};
const onExit = waitForExit(subprocess);
const onStdoutDone = bufferOutput(subprocess.stdout, result, 'stdout');
const onStderrDone = bufferOutput(subprocess.stderr, result, 'stderr');

try {
await Promise.all([onExit, onStdoutDone, onStderrDone]);
const output = getOutput(subprocess, result, command);
const output = getOutput(subprocess, result, command, start);
checkFailure(command, output);
return output;
} catch (error) {
await Promise.allSettled([onExit, onStdoutDone, onStderrDone]);
throw Object.assign(getResultError(error, command), getOutput(subprocess, result, command));
throw Object.assign(getResultError(error, command), getOutput(subprocess, result, command, start));
}
};

Expand Down Expand Up @@ -96,13 +97,14 @@ const bufferOutput = async (stream, result, streamName) => {
await finished(stream, {cleanup: true});
};

const getOutput = ({exitCode, signalCode}, {stdout, stderr}, command) => ({
const getOutput = ({exitCode, signalCode}, {stdout, stderr}, command, start) => ({
// `exitCode` can be a negative number (`errno`) when the `error` event is emitted on the subprocess
...(exitCode === null || exitCode < 0 ? {} : {exitCode}),
...(signalCode === null ? {} : {signalName: signalCode}),
stdout: stripNewline(stdout),
stderr: stripNewline(stderr),
command,
durationMs: Number(process.hrtime.bigint() - start) / 1e6,
});

const stripNewline = input => input?.at(-1) === '\n'
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,18 @@ test('result.command strips ANSI sequences', async t => {
t.is(stdout, red('.'));
});

test('result.durationMs is set', async t => {
const {durationMs} = await nanoSpawn('node', ['--version']);
t.true(Number.isFinite(durationMs));
t.true(durationMs > 0);
});

test('error.durationMs is set', async t => {
const {durationMs} = await t.throwsAsync(nanoSpawn('node', ['--unknown']));
t.true(Number.isFinite(durationMs));
t.true(durationMs > 0);
});

if (isWindows) {
test('Can run .exe file', async t => {
t.is(path.extname(process.execPath), '.exe');
Expand Down