Skip to content

Commit f8bfbeb

Browse files
committed
feat: wrapStd
1 parent e01ee6a commit f8bfbeb

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

examples/wrap-std.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env node -r esm
2+
3+
import { consola } from './utils'
4+
5+
function foo () {
6+
console.info('console foo') // eslint-disable-line no-console
7+
process.stdout.write('called from stdout foo\n')
8+
process.stderr.write('called from stderr foo\n')
9+
}
10+
11+
consola.wrapStd()
12+
foo()
13+
consola.restoreStd()
14+
foo()

src/consola.js

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import Types from './types.js'
22
import { isLogObj } from './utils/index.js'
33

4-
const originalConsole = Object.assign({}, console)
5-
64
let paused = false
75
const queue = []
86

@@ -97,12 +95,60 @@ export default class Consola {
9795

9896
wrapConsole () {
9997
for (const type in this._types) {
98+
// Backup original value
99+
if (!console['__' + type]) { // eslint-disable-line no-console
100+
console['__' + type] = console[type] // eslint-disable-line no-console
101+
}
102+
// Override
100103
console[type] = this[type] // eslint-disable-line no-console
101104
}
102105
}
103106

104107
restoreConsole () {
105-
Object.assign(console, originalConsole)
108+
for (const type in this._types) {
109+
// Restore if backup is available
110+
if (console['__' + type]) { // eslint-disable-line no-console
111+
console[type] = console['__' + type] // eslint-disable-line no-console
112+
delete console['__' + type] // eslint-disable-line no-console
113+
}
114+
}
115+
}
116+
117+
wrapStd () {
118+
this._wrapStream(this._stdout, 'log')
119+
this._wrapStream(this._stderr, 'error')
120+
}
121+
122+
_wrapStream (stream, type) {
123+
if (!stream) {
124+
return
125+
}
126+
127+
// Backup original value
128+
if (!stream.__write) {
129+
stream.__write = stream.write
130+
}
131+
132+
// Override
133+
stream.write = (data) => {
134+
this[type](String(data).trim())
135+
}
136+
}
137+
138+
restoreStd () {
139+
this._restoreStream(this._stdout)
140+
this._restoreStream(this._stderr)
141+
}
142+
143+
_restoreStream (stream) {
144+
if (!stream) {
145+
return
146+
}
147+
148+
if (stream.__write) {
149+
stream.write = stream.__write
150+
delete stream.__write
151+
}
106152
}
107153

108154
pause () {

src/utils/stream.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { writeSync } from 'fs'
22

33
export function writeStream (data, stream, mode = 'default') {
4+
const write = stream.__write || stream.write
5+
46
switch (mode) {
57
case 'async':
68
return new Promise((resolve) => {
7-
if (stream.write(data) === true) {
9+
if (write.call(stream, data) === true) {
810
resolve()
911
} else {
1012
stream.once('drain', () => { resolve() })
@@ -13,6 +15,6 @@ export function writeStream (data, stream, mode = 'default') {
1315
case 'sync':
1416
return writeSync(stream.fd, data)
1517
default:
16-
return stream.write(data)
18+
return write.call(stream, data)
1719
}
1820
}

0 commit comments

Comments
 (0)