output-interceptor

Intercepts stdout/ stderr.

Usage no npm install needed!

<script type="module">
  import outputInterceptor from 'https://cdn.skypack.dev/output-interceptor';
</script>

README

output-interceptor

GitSpo Mentions Travis build status Coveralls NPM version Canonical Code Style Twitter Follow

Intercepts stdout/ stderr.

Implementation

This module uses domain to capture asynchronous function output.

Read Capturing stdout/ stderr in Node.js using Domain module.

Usage

import {
  createOutputInterceptor
} from 'output-interceptor';

const interceptOutput = createOutputInterceptor();

const main = async () => {
  const result = await interceptOutput(() => {
    console.log('foo');
    console.error('bar');

    return Promise.resolve('baz');
  });

  result === 'baz';

  interceptOutput.output === 'foo\nbar\n';
};

main();

API

/**
 * @property interceptStderr Default: true.
 * @property interceptStdout Default: true.
 * @property stripAnsi Default: true.
 */
export type OutputInterceptorUserConfigurationType = {|
  +interceptStderr?: boolean,
  +interceptStdout?: boolean,
  +stripAnsi?: boolean
|};

/**
 * @returns Intercepted output.
 */
type FlushType = () => string;

/**
 * @property output Output produced during the executing of the `routine`.
 */
export type OutputInterceptorType = {|
  <T>(routine: () => Promise<T> | T): Promise<T>,
  output: ''
|};

createOutputInterceptor(userConfiguration?: OutputInterceptorUserConfigurationType): OutputInterceptorType;