feat(default-reporter): return an unsubscribe function (#4040)

This commit is contained in:
Zoltan Kochan
2021-11-25 23:37:20 +02:00
committed by GitHub
parent 3b4dc17c7b
commit 597a28e3cf
4 changed files with 33 additions and 7 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/default-reporter": minor
---
The default reporter returns an unsubscribe function to stop reporting.

View File

@@ -8,6 +8,26 @@
<pnpm|npm|yarn> add @pnpm/default-reporter
```
## Usage
```ts
import { streamParser } from '@pnpm/logger'
import defaultReporter from '@pnpm/default-reporter'
const stopReporting = defaultReporter({
context: {
argv: [],
},
streamParser,
})
try {
// calling some pnpm APIs
} finally {
stopReporting()
}
```
## Style Guide
1. Never use blue or grey as font color as they are hard to read in many consoles.

View File

@@ -28,12 +28,12 @@ export default function (
config?: Config
}
}
) {
): () => void {
if (opts.context.argv[0] === 'server') {
// eslint-disable-next-line
const log$ = Rx.fromEvent<logs.Log>(opts.streamParser as any, 'data')
reporterForServer(log$, opts.context.config)
return
const subscription = reporterForServer(log$, opts.context.config)
return () => subscription.unsubscribe()
}
const outputMaxWidth = opts.reportingOptions?.outputMaxWidth ?? (process.stdout.columns && process.stdout.columns - 2) ?? 80
const output$ = toOutput$({ ...opts, reportingOptions: { ...opts.reportingOptions, outputMaxWidth } })
@@ -41,19 +41,19 @@ export default function (
const writeNext = opts.useStderr
? console.error.bind(console)
: console.log.bind(console)
output$
const subscription = output$
.subscribe({
complete () {}, // eslint-disable-line:no-empty
error: (err) => console.error(err.message),
next: writeNext,
})
return
return () => subscription.unsubscribe()
}
const diff = createDiffer({
height: process.stdout.rows,
outputMaxWidth,
})
output$
const subscription = output$
.subscribe({
complete () {}, // eslint-disable-line:no-empty
error: (err) => logUpdate(err.message),
@@ -69,6 +69,7 @@ export default function (
if (!view.endsWith(EOL)) view += EOL
write(diff.update(view))
}
return () => subscription.unsubscribe()
}
export function toOutput$ (

View File

@@ -8,7 +8,7 @@ export default function (
log$: Rx.Observable<Log>,
config?: Config
) {
log$.subscribe({
return log$.subscribe({
complete: () => undefined,
error: () => undefined,
next (log) {