Comparing version 2.0.0 to 3.0.0
274
index.d.ts
@@ -1,172 +0,164 @@ | ||
/// <reference types="node"/> | ||
import {Buffer} from 'node:buffer'; | ||
declare namespace hookStd { | ||
/** | ||
`unhook()` method which, when called, unhooks from a stream and resolves the Promise. | ||
*/ | ||
export type Unhook = () => void; | ||
/** | ||
@param output - String from stream output. | ||
@param unhook - Method which, when called, unhooks from stream. | ||
@returns A Buffer or string to modify the value in the stream. | ||
*/ | ||
export type Transform = ( | ||
output: string, | ||
unhook: Unhook | ||
) => Buffer | string | void; | ||
/** | ||
@param output - String from stream output. | ||
@param unhook - Method which, when called, unhooks from stream. | ||
@returns A boolean to influence the return value of `.write(…)`. | ||
*/ | ||
export type SilentTransform = ( | ||
output: string, | ||
unhook: Unhook | ||
) => boolean | void; | ||
export interface Options { | ||
/** | ||
`unhook()` method which, when called, unhooks from a stream and resolves the Promise. | ||
Automatically unhook after the first call. | ||
@default false | ||
*/ | ||
type Unhook = () => void; | ||
readonly once?: boolean; | ||
/** | ||
@param output - String from stream output. | ||
@param unhook - Method which, when called, unhooks from stream. | ||
@returns A Buffer or string to modify the value in the stream. | ||
Suppress stdout/stderr output. | ||
@default true | ||
*/ | ||
type Transform = ( | ||
output: string, | ||
unhook: Unhook | ||
) => Buffer | string | void; | ||
readonly silent?: boolean; | ||
} | ||
export interface StreamsOptions extends Options { | ||
/** | ||
@param output - String from stream output. | ||
@param unhook - Method which, when called, unhooks from stream. | ||
@returns A boolean to influence the return value of `.write(…)`. | ||
The writable streams to hook. This can be useful for libraries allowing users to configure a writable stream to write to. | ||
@default [process.stdout, process.stderr] | ||
*/ | ||
type SilentTransform = ( | ||
output: string, | ||
unhook: Unhook | ||
) => boolean | void; | ||
readonly streams?: readonly NodeJS.WritableStream[]; | ||
} | ||
interface Options { | ||
/** | ||
Automatically unhooks after the first call. | ||
/** | ||
Promise with a `unhook()` method which, when called, resolves the Promise with an empty result. | ||
*/ | ||
export interface HookPromise extends Promise<void> { | ||
unhook: Unhook; | ||
} | ||
@default false | ||
*/ | ||
readonly once?: boolean; | ||
/** | ||
Hook streams in the `streams` options, or stdout and stderr if none are specified. | ||
/** | ||
Suppress stdout/stderr output. | ||
@returns A `Promise` with a `unhook()` method which, when called, unhooks both stdout and stderr and resolves the `Promise` with an empty result. | ||
*/ | ||
export function hookStd(transform: SilentTransform): HookPromise; | ||
export function hookStd( | ||
options: StreamsOptions & {silent?: false}, | ||
transform: Transform | ||
): HookPromise; | ||
export function hookStd( | ||
options: StreamsOptions & {silent?: true}, | ||
transform: SilentTransform | ||
): HookPromise; | ||
@default true | ||
*/ | ||
readonly silent?: boolean; | ||
} | ||
/** | ||
Hook stdout. | ||
interface StreamsOptions extends Options { | ||
/** | ||
Writable streams to hook. This can be useful for libraries allowing users to configure a Writable Stream to write to. | ||
@returns A `Promise` with a `unhook()` method which, when called, unhooks stdout and resolves the `Promise` with an empty result. | ||
@default [process.stdout, process.stderr] | ||
*/ | ||
readonly streams?: NodeJS.WritableStream[]; | ||
} | ||
@example | ||
``` | ||
import assert from 'node:assert'; | ||
import {hookStdout} from 'hook-std'; | ||
interface SilentFalseOptions extends Options { | ||
/** | ||
Suppress stdout/stderr output. | ||
const promise = hookStdout(output => { | ||
promise.unhook(); | ||
assert.strictEqual(output.trim(), 'unicorn'); | ||
}); | ||
@default true | ||
*/ | ||
readonly silent: false; | ||
} | ||
console.log('unicorn'); | ||
await promise; | ||
``` | ||
interface SilentTrueOptions extends Options { | ||
/** | ||
Suppress stdout/stderr output. | ||
You can also unhook using the second `transform` method parameter: | ||
@default true | ||
*/ | ||
readonly silent?: true; | ||
} | ||
@example | ||
``` | ||
import assert from 'node:assert'; | ||
import {hookStdout} from 'hook-std'; | ||
interface StreamsSilentFalseOptions extends StreamsOptions { | ||
/** | ||
Suppress stdout/stderr output. | ||
const promise = hookStdout((output, unhook) => { | ||
unhook(); | ||
assert.strictEqual(output.trim(), 'unicorn'); | ||
}); | ||
@default true | ||
*/ | ||
readonly silent: false; | ||
} | ||
console.log('unicorn'); | ||
await promise; | ||
``` | ||
*/ | ||
export function hookStdout(transform: SilentTransform): HookPromise; | ||
export function hookStdout( | ||
options: Options & {silent?: false}, | ||
transform: Transform | ||
): HookPromise; | ||
export function hookStdout( | ||
options: Options & {silent?: true}, | ||
transform: SilentTransform | ||
): HookPromise; | ||
interface StreamsSilentTrueOptions extends StreamsOptions { | ||
/** | ||
Suppress stdout/stderr output. | ||
/** | ||
Hook stderr. | ||
@default true | ||
*/ | ||
readonly silent?: true; | ||
} | ||
@returns A `Promise` with a `unhook()` method which, when called, unhooks stderr and resolves the `Promise` with an empty result. | ||
/** | ||
Promise with a `unhook()` method which, when called, resolves the Promise with an empty result. | ||
*/ | ||
interface HookPromise extends Promise<void> { | ||
unhook: Unhook; | ||
} | ||
} | ||
@example | ||
``` | ||
import assert from 'node:assert'; | ||
import {hookStderr} from 'hook-std'; | ||
declare const hookStd: { | ||
/** | ||
Hooks streams in options or stdout & stderr if none are specified. | ||
const promise = hookStdout(output => { | ||
promise.unhook(); | ||
assert.strictEqual(output.trim(), 'unicorn'); | ||
}); | ||
@returns A `Promise` with a `unhook()` method which, when called, unhooks the streams and resolves the `Promise`. | ||
*/ | ||
(transform: hookStd.SilentTransform): hookStd.HookPromise; | ||
( | ||
options: hookStd.StreamsSilentFalseOptions, | ||
transform: hookStd.Transform | ||
): hookStd.HookPromise; | ||
( | ||
options: hookStd.StreamsSilentTrueOptions, | ||
transform: hookStd.SilentTransform | ||
): hookStd.HookPromise; | ||
console.error('unicorn'); | ||
await promise; | ||
``` | ||
/** | ||
Hooks stdout. | ||
You can also unhook using the second `transform` method parameter: | ||
@returns A `Promise` with a `unhook()` method which, when called, unhooks the streams and resolves the `Promise`. | ||
@example | ||
``` | ||
import assert from 'node:assert'; | ||
import {hookStderr} from 'hook-std'; | ||
@example | ||
``` | ||
import * as assert from 'assert'; | ||
import hookStd = require('hook-std'); | ||
const promise = hookStderr((output, unhook) => { | ||
unhook(); | ||
assert.strictEqual(output.trim(), 'unicorn'); | ||
}); | ||
(async () => { | ||
const promise = hookStd.stdout(output => { | ||
promise.unhook(); | ||
assert.strictEqual(output.trim(), 'unicorn'); | ||
}); | ||
console.error('unicorn'); | ||
await promise; | ||
``` | ||
*/ | ||
export function hookStderr(transform: SilentTransform): HookPromise; | ||
export function hookStderr( | ||
options: Options & {silent?: false}, | ||
transform: Transform | ||
): HookPromise; | ||
export function hookStderr( | ||
options: Options & {silent?: true}, | ||
transform: SilentTransform | ||
): HookPromise; | ||
console.log('unicorn'); | ||
await promise; | ||
})(); | ||
// You can also unhook using the second `transform` method parameter: | ||
(async () => { | ||
const promise = hookStd.stdout((output, unhook) => { | ||
unhook(); | ||
assert.strictEqual(output.trim(), 'unicorn'); | ||
}); | ||
console.log('unicorn'); | ||
await promise; | ||
})(); | ||
``` | ||
*/ | ||
stdout(transform: hookStd.SilentTransform): hookStd.HookPromise; | ||
stdout( | ||
options: hookStd.SilentFalseOptions, | ||
transform: hookStd.Transform | ||
): hookStd.HookPromise; | ||
stdout( | ||
options: hookStd.SilentTrueOptions, | ||
transform: hookStd.SilentTransform | ||
): hookStd.HookPromise; | ||
/** | ||
Hooks stderr. | ||
@returns A `Promise` with a `unhook()` method which, when called, unhooks the streams and resolves the `Promise`. | ||
*/ | ||
stderr(transform: hookStd.SilentTransform): hookStd.HookPromise; | ||
stderr( | ||
options: hookStd.SilentFalseOptions, | ||
transform: hookStd.Transform | ||
): hookStd.HookPromise; | ||
stderr( | ||
options: hookStd.SilentTrueOptions, | ||
transform: hookStd.SilentTransform | ||
): hookStd.HookPromise; | ||
}; | ||
export = hookStd; |
18
index.js
@@ -1,2 +0,3 @@ | ||
'use strict'; | ||
import process from 'node:process'; | ||
import {Buffer} from 'node:buffer'; | ||
@@ -12,3 +13,3 @@ const hook = (stream, options, transform) => { | ||
once: false, | ||
...options | ||
...options, | ||
}; | ||
@@ -55,3 +56,3 @@ | ||
const hookStd = (options, transform) => { | ||
export function hookStd(options, transform) { | ||
const streams = options.streams || [process.stdout, process.stderr]; | ||
@@ -68,7 +69,10 @@ const streamPromises = streams.map(stream => hook(stream, options, transform)); | ||
return promise; | ||
}; | ||
} | ||
hookStd.stdout = (...arguments_) => hook(process.stdout, ...arguments_); | ||
hookStd.stderr = (...arguments_) => hook(process.stderr, ...arguments_); | ||
export function hookStdout(...arguments_) { | ||
return hook(process.stdout, ...arguments_); | ||
} | ||
module.exports = hookStd; | ||
export function hookStderr(...arguments_) { | ||
return hook(process.stderr, ...arguments_); | ||
} |
{ | ||
"name": "hook-std", | ||
"version": "2.0.0", | ||
"description": "Hook and modify stdout/stderr", | ||
"version": "3.0.0", | ||
"description": "Hook and modify stdout and stderr", | ||
"license": "MIT", | ||
"repository": "sindresorhus/hook-std", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=8" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -40,7 +43,10 @@ "scripts": { | ||
"devDependencies": { | ||
"@types/node": "^11.13.0", | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
"@types/node": "^16.9.2", | ||
"ava": "^3.15.0", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
}, | ||
"ava": { | ||
"serial": true | ||
} | ||
} |
@@ -1,28 +0,24 @@ | ||
# hook-std [![Build Status](https://travis-ci.org/sindresorhus/hook-std.svg?branch=master)](https://travis-ci.org/sindresorhus/hook-std) | ||
# hook-std | ||
> Hook and modify stdout and stderr | ||
## Install | ||
```sh | ||
npm install hook-std | ||
``` | ||
$ npm install hook-std | ||
``` | ||
## Usage | ||
```js | ||
const assert = require('assert'); | ||
const hookStd = require('hook-std'); | ||
import assert from 'node:assert'; | ||
import {hookStdout} from 'hook-std'; | ||
(async () => { | ||
const promise = hookStd.stdout(output => { | ||
promise.unhook(); | ||
assert.strictEqual(output.trim(), 'unicorn'); | ||
}); | ||
const promise = hookStdout(output => { | ||
promise.unhook(); | ||
assert.strictEqual(output.trim(), 'unicorn'); | ||
}); | ||
console.log('unicorn'); | ||
await promise; | ||
})(); | ||
console.log('unicorn'); | ||
await promise; | ||
``` | ||
@@ -33,41 +29,41 @@ | ||
```js | ||
(async () => { | ||
const promise = hookStd.stdout((output, unhook) => { | ||
unhook(); | ||
assert.strictEqual(output.trim(), 'unicorn'); | ||
}); | ||
import assert from 'node:assert'; | ||
import {hookStdout} from 'hook-std'; | ||
console.log('unicorn'); | ||
await promise; | ||
})(); | ||
const promise = hookStdout((output, unhook) => { | ||
unhook(); | ||
assert.strictEqual(output.trim(), 'unicorn'); | ||
}); | ||
console.log('unicorn'); | ||
await promise; | ||
``` | ||
## API | ||
### hookStd([options], transform) | ||
### hookStd(options?, transform) | ||
Hook streams in [streams option](#streams), by default stdout and stderr. | ||
Hook streams in [`streams` option](#streams), or stdout and stderr if none are specified. | ||
Returns a `Promise` with a `unhook()` method which, when called, unhooks both stdout and stderr and resolves the `Promise` with an empty result. | ||
### hookStd.stdout([options], transform) | ||
### hookStdout(options?, transform) | ||
Hook stdout. | ||
Returns a `Promise` with a `unhook()` method which, when called, resolves the `Promise` with an empty result. | ||
Returns a `Promise` with a `unhook()` method which, when called, unhooks stdout and resolves the `Promise` with an empty result. | ||
### hookStd.stderr([options], transform) | ||
### hookStderr(options?, transform) | ||
Hook stderr. | ||
Returns a `Promise` with a `unhook()` method which, when called, resolves the `Promise` with an empty result. | ||
Returns a `Promise` with a `unhook()` method which, when called, unhooks stderr and resolves the `Promise` with an empty result. | ||
#### options | ||
Type: `Object` | ||
Type: `object` | ||
##### silent | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -79,13 +75,13 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
Automatically unhooks after the first call. | ||
Automatically unhook after the first call. | ||
##### streams | ||
Type: `stream.Writable[]`<br> | ||
Type: `stream.Writable[]`\ | ||
Default: `[process.stdout, process.stderr]` | ||
[Writable streams](https://nodejs.org/api/stream.html#stream_writable_streams) to hook. This can be useful for libraries allowing users to configure a Writable Stream to write to. | ||
The [writable streams](https://nodejs.org/api/stream.html#stream_writable_streams) to hook. This can be useful for libraries allowing users to configure a writable stream to write to. | ||
@@ -96,7 +92,2 @@ #### transform | ||
Receives stdout/stderr as the first argument and the unhook method as the second argument. Return a string to modify it. Optionally, when in silent mode, you may return a `boolean` to influence the return value of `.write(...)`. | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
Receives stdout/stderr as the first argument and the unhook method as the second argument. Return a string to modify it. Optionally, when in silent mode, you may return a `boolean` to influence the return value of `.write(…)`. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9574
Yes
189
91