Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f129d4

Browse files
committed
Meta tweaks
1 parent cca6371 commit 1f129d4

File tree

4 files changed

+42
-48
lines changed

4 files changed

+42
-48
lines changed

index.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict';
22

3-
const hook = (stream, opts, transform) => {
4-
if (typeof opts !== 'object') {
5-
transform = opts;
6-
opts = {};
3+
const hook = (stream, options, transform) => {
4+
if (typeof options !== 'object') {
5+
transform = options;
6+
options = {};
77
}
88

9-
opts = Object.assign({
9+
options = Object.assign({
1010
silent: true,
1111
once: false
12-
}, opts);
12+
}, options);
1313

1414
let unhookFn;
1515

@@ -24,16 +24,15 @@ const hook = (stream, opts, transform) => {
2424
stream.write = (output, enc, cb) => {
2525
const cbRet = transform(String(output), unhook);
2626

27-
if (opts.once) {
27+
if (options.once) {
2828
unhook();
2929
}
3030

31-
if (opts.silent) {
31+
if (options.silent) {
3232
return typeof cbRet === 'boolean' ? cbRet : true;
3333
}
3434

3535
let ret;
36-
3736
if (typeof cbRet === 'string') {
3837
ret = typeof enc === 'string' ? Buffer.from(cbRet).toString(enc) : cbRet;
3938
}
@@ -51,9 +50,9 @@ const hook = (stream, opts, transform) => {
5150
return promise;
5251
};
5352

54-
const hookStd = (opts, transform) => {
55-
const streams = opts.streams || [process.stdout, process.stderr];
56-
const streamPromises = streams.map(stream => hook(stream, opts, transform));
53+
const hookStd = (options, transform) => {
54+
const streams = options.streams || [process.stdout, process.stderr];
55+
const streamPromises = streams.map(stream => hook(stream, options, transform));
5756

5857
const promise = Promise.all(streamPromises);
5958
promise.unhook = () => {
@@ -68,8 +67,5 @@ const hookStd = (opts, transform) => {
6867
hookStd.stdout = (...args) => hook(process.stdout, ...args);
6968
hookStd.stderr = (...args) => hook(process.stderr, ...args);
7069

71-
module.exports.stdout = hookStd.stdout;
72-
module.exports.stderr = hookStd.stderr;
73-
74-
module.exports.default = hookStd;
7570
module.exports = hookStd;
71+
module.exports.default = hookStd;

index.test-d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ expectType<HookPromise>(stdout(() => true));
3030
expectType<HookPromise>(stderr(() => null));
3131
expectType<HookPromise>(stderr(() => true));
3232

33-
expectType<(transform: Transform) => HookPromise>(stdout)
34-
expectType<(options: Options, transform: Transform) => HookPromise>(stdout)
33+
expectType<(transform: Transform) => HookPromise>(stdout);
34+
expectType<(options: Options, transform: Transform) => HookPromise>(stdout);
3535

36-
expectType<(transform: Transform) => HookPromise>(stderr)
37-
expectType<(options: Options, transform: Transform) => HookPromise>(stderr)
36+
expectType<(transform: Transform) => HookPromise>(stderr);
37+
expectType<(options: Options, transform: Transform) => HookPromise>(stderr);
3838

39-
expectType<(transform: Transform) => HookPromise>(hookStd)
40-
expectType<(opts: Options, transform: Transform) => HookPromise>(hookStd)
39+
expectType<(transform: Transform) => HookPromise>(hookStd);
40+
expectType<(options: Options, transform: Transform) => HookPromise>(hookStd);

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@
3636
"log",
3737
"process"
3838
],
39-
"dependencies": {
40-
"@types/node": "*"
41-
},
4239
"devDependencies": {
43-
"ava": "*",
44-
"tsd-check": "*",
45-
"xo": "*"
40+
"@types/node": "^10.12.9",
41+
"ava": "^0.25.0",
42+
"tsd-check": "^0.2.1",
43+
"xo": "^0.23.0"
4644
}
4745
}

test.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {serial as test} from 'ava';
2-
import m from '.';
2+
import hookStd from '.';
33

44
const {stdout, stderr} = process;
55

@@ -27,7 +27,7 @@ test.cb('hook stdout & stderr', t => {
2727

2828
let i = 0;
2929

30-
const promise = m(str => {
30+
const promise = hookStd(str => {
3131
if (str === 'foo' || str === 'bar') {
3232
t.pass();
3333
}
@@ -45,7 +45,7 @@ test.cb('hook stdout & stderr', t => {
4545
test.cb('hook stdout', t => {
4646
t.plan(1);
4747

48-
const promise = m.stdout(str => {
48+
const promise = hookStd.stdout(str => {
4949
t.is(str, 'foo');
5050
promise.unhook();
5151
t.end();
@@ -57,7 +57,7 @@ test.cb('hook stdout', t => {
5757
test.cb('hook stderr', t => {
5858
t.plan(1);
5959

60-
const promise = m.stderr(str => {
60+
const promise = hookStd.stderr(str => {
6161
t.is(str, 'foo');
6262
promise.unhook();
6363
t.end();
@@ -72,7 +72,7 @@ test.cb('hook custom stream', t => {
7272

7373
let i = 0;
7474

75-
const promise = m({streams}, str => {
75+
const promise = hookStd({streams}, str => {
7676
if (str === 'foo') {
7777
t.pass();
7878
}
@@ -108,7 +108,7 @@ test('passes through the return value of the underlying write call', t => {
108108
write: loggingWrite(log, () => returnValue)
109109
};
110110

111-
m.stdout({silent: false}, str => str);
111+
hookStd.stdout({silent: false}, str => str);
112112

113113
t.false(process.stdout.write('foo'));
114114
returnValue = true;
@@ -124,7 +124,7 @@ test('if silent, returns true by default', t => {
124124
write: () => t.fail()
125125
};
126126

127-
m.stdout(str => {
127+
hookStd.stdout(str => {
128128
log.push(str);
129129
return str;
130130
});
@@ -142,7 +142,7 @@ test('if silent, callback can return a boolean', t => {
142142
write: () => t.fail()
143143
};
144144

145-
m.stdout(str => {
145+
hookStd.stdout(str => {
146146
log.push(str);
147147
return returnValue;
148148
});
@@ -161,7 +161,7 @@ test('callback can return a buffer', t => {
161161
write: loggingWrite(log, () => true)
162162
};
163163

164-
m.stdout({silent: false}, str => Buffer.from(str));
164+
hookStd.stdout({silent: false}, str => Buffer.from(str));
165165

166166
t.true(process.stdout.write('foo'));
167167
t.true(process.stdout.write('bar'));
@@ -177,7 +177,7 @@ test('if no options are assigned, behave as silent', t => {
177177
write: loggingWrite(log, () => returnValue)
178178
};
179179

180-
m.stdout(str => str);
180+
hookStd.stdout(str => str);
181181

182182
process.stdout.write('foo');
183183
returnValue = true;
@@ -193,7 +193,7 @@ test('if once option is true, only the first write is silent', t => {
193193
write: loggingWrite(log, () => returnValue)
194194
};
195195

196-
m.stdout({once: true}, str => str);
196+
hookStd.stdout({once: true}, str => str);
197197

198198
process.stdout.write('foo');
199199
process.stdout.write('bar');
@@ -211,7 +211,7 @@ test('if once option is true and silent is false, hook only prints the first wri
211211
write: loggingWrite(log, () => true)
212212
};
213213

214-
m.stdout({silent: false, once: true}, str => {
214+
hookStd.stdout({silent: false, once: true}, str => {
215215
hookReturnValue = str;
216216
return str;
217217
});
@@ -231,7 +231,7 @@ test('output is converted to string', t => {
231231
t.plan(4);
232232
const log = [];
233233

234-
m.stdout(str => log.push(str));
234+
hookStd.stdout(str => log.push(str));
235235

236236
process.stdout.write('foo');
237237
t.deepEqual(log, ['foo']);
@@ -253,7 +253,7 @@ test('string returned by callback is converted to correct encoding', t => {
253253
write: output => output
254254
};
255255

256-
m.stdout({silent: false}, () => 'tést');
256+
hookStd.stdout({silent: false}, () => 'tést');
257257

258258
t.is(process.stdout.write('foo', 'hex'), '74c3a97374');
259259
t.is(process.stdout.write('bar', 'ascii'), 'tC)st');
@@ -266,7 +266,7 @@ test('string returned by callback is not converted if encoding is invalid', t =>
266266
write: output => output
267267
};
268268

269-
m.stdout({silent: false}, () => 'tést');
269+
hookStd.stdout({silent: false}, () => 'tést');
270270

271271
t.is(process.stdout.write('foo', 123), 'tést');
272272
t.is(process.stdout.write('bar', null), 'tést');
@@ -278,7 +278,7 @@ test('promise resolves when stdout & stderr are hooked and released via promise
278278
t.plan(1);
279279
const log = [];
280280

281-
const promise = m(str => log.push(str));
281+
const promise = hookStd(str => log.push(str));
282282

283283
process.stdout.write('foo');
284284
process.stderr.write('bar');
@@ -292,7 +292,7 @@ test('promise resolves when stdout & stderr are hooked and released via callback
292292
t.plan(1);
293293
const log = [];
294294

295-
const promise = m((str, unhook) => {
295+
const promise = hookStd((str, unhook) => {
296296
log.push(str);
297297
unhook();
298298
});
@@ -306,7 +306,7 @@ test('promise resolves when stdout & stderr are hooked and released via callback
306306

307307
test('promise resolves when stdout is released via promise unhook method', async t => {
308308
t.plan(1);
309-
const promise = m.stdout(str => {
309+
const promise = hookStd.stdout(str => {
310310
t.is(str, 'foo');
311311
});
312312
process.stdout.write('foo');
@@ -316,7 +316,7 @@ test('promise resolves when stdout is released via promise unhook method', async
316316

317317
test('promise resolves when stderr is released via promise unhook method', async t => {
318318
t.plan(1);
319-
const promise = m.stderr(str => {
319+
const promise = hookStd.stderr(str => {
320320
t.is(str, 'foo');
321321
});
322322
process.stderr.write('foo');
@@ -329,7 +329,7 @@ test('promise resolves when streams are hooked and released via callback', async
329329
const log = [];
330330
const streams = [{write: () => {}}, {write: () => {}}];
331331

332-
const promise = m({streams}, (str, unhook) => {
332+
const promise = hookStd({streams}, (str, unhook) => {
333333
log.push(str);
334334
unhook();
335335
});

0 commit comments

Comments
 (0)