File: index.d.ts

package info (click to toggle)
node-hook-std 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 144 kB
  • sloc: javascript: 510; makefile: 2
file content (172 lines) | stat: -rw-r--r-- 3,869 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/// <reference types="node"/>

declare namespace hookStd {
	/**
	`unhook()` method which, when called, unhooks from a stream and resolves the Promise.
	*/
	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.
	*/
	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(…)`.
	*/
	type SilentTransform = (
		output: string,
		unhook: Unhook
	) => boolean | void;

	interface Options {
		/**
		Automatically unhooks after the first call.

		@default false
		*/
		readonly once?: boolean;

		/**
		Suppress stdout/stderr output.

		@default true
		*/
		readonly silent?: boolean;
	}

	interface StreamsOptions extends Options {
		/**
		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]
		*/
		readonly streams?: NodeJS.WritableStream[];
	}

	interface SilentFalseOptions extends Options {
		/**
		Suppress stdout/stderr output.

		@default true
		*/
		readonly silent: false;
	}

	interface SilentTrueOptions extends Options {
		/**
		Suppress stdout/stderr output.

		@default true
		*/
		readonly silent?: true;
	}

	interface StreamsSilentFalseOptions extends StreamsOptions {
		/**
		Suppress stdout/stderr output.

		@default true
		*/
		readonly silent: false;
	}

	interface StreamsSilentTrueOptions extends StreamsOptions {
		/**
		Suppress stdout/stderr output.

		@default true
		*/
		readonly silent?: true;
	}

	/**
	Promise with a `unhook()` method which, when called, resolves the Promise with an empty result.
	*/
	interface HookPromise extends Promise<void> {
		unhook: Unhook;
	}
}

declare const hookStd: {
	/**
	Hooks streams in options or stdout & stderr if none are specified.

	@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;

	/**
	Hooks stdout.

	@returns A `Promise` with a `unhook()` method which, when called, unhooks the streams and resolves the `Promise`.

	@example
	```
	import * as assert from 'assert';
	import hookStd = require('hook-std');

	(async () => {
		const promise = hookStd.stdout(output => {
			promise.unhook();
			assert.strictEqual(output.trim(), 'unicorn');
		});

		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;