mirror of
https://github.com/pnpm/pnpm.git
synced 2025-12-24 07:38:12 -05:00
feat(default-reporter): return an unsubscribe function (#4040)
This commit is contained in:
5
.changeset/khaki-ladybugs-reflect.md
Normal file
5
.changeset/khaki-ladybugs-reflect.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@pnpm/default-reporter": minor
|
||||
---
|
||||
|
||||
The default reporter returns an unsubscribe function to stop reporting.
|
||||
@@ -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.
|
||||
|
||||
@@ -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$ (
|
||||
|
||||
@@ -8,7 +8,7 @@ export default function (
|
||||
log$: Rx.Observable<Log>,
|
||||
config?: Config
|
||||
) {
|
||||
log$.subscribe({
|
||||
return log$.subscribe({
|
||||
complete: () => undefined,
|
||||
error: () => undefined,
|
||||
next (log) {
|
||||
|
||||
Reference in New Issue
Block a user