Skip to content

Commit

Permalink
feat: add related documentation to process output and update with fea…
Browse files Browse the repository at this point in the history
…ture flag
  • Loading branch information
cenk1cenk2 committed Apr 11, 2023
1 parent e6d6474 commit c1ede63
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
16 changes: 15 additions & 1 deletion docs/renderer/process-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,18 @@ After the renderer releases the _ProcessOutput_ and marks it ready to use, every

## Extending Process Output

You can override the default _ProcessOutput_ by extending the class with your expected behavior (e.g. writing to a log file) on the _ListrLogger_ since all of the renderers, that either use or do not use the hijacking function, use _ProcessOutput_ through _ListrLogger_ itself. For most cases, just creating a `new ProcessOutput()` by passing your own `WriteStream` for `process.stdout` and `process.stderr` through the constructor should be good enough.
You can override the default _ProcessOutput_ by extending the class with your expected behavior (e.g. writing to a log file) on the _ListrLogger_ since all the renderers, that either use or do not use the hijacking function, use _ProcessOutput_ through _ListrLogger_ itself. For most cases, just creating a `new ProcessOutput()` by passing your own `WriteStream` for `process.stdout` and `process.stderr` through the constructor should be good enough.

### Changing the Behavior

<Badge><FontIcon icon="mdi:tag-text-outline"/>v6.1.0</Badge><Badge type="warning"><FontIcon icon="mdi:github"/><a href="https://github.com/listr2/listr2/issues/670" target="_blank">#670</a></Badge>

You can change the behavior of the _ProcessOutput_ through injecting it to the logger. Since every renderer at some level uses the underlying logger, this can effectively be used to change the behavior of the ProcessOutput as well.

If you do not like the behavior of the _ProcessOutput_, you can always implement and bring your own through this interface as well.

::: details <FontIcon icon="material-symbols:code-blocks-outline" /> Code Example

@[code typescript](../../examples/docs/renderer/process-output/change-behavior.ts)

:::
43 changes: 43 additions & 0 deletions examples/docs/renderer/process-output/change-behavior.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { EOL } from 'os'

import { Listr, ListrLogger, ProcessOutput, delay } from 'listr2'

const tasks = new Listr(
[
{
title: 'This task will execute.',
task: async (): Promise<void> => {
await delay(500)

console.log('i am logging some stuff out')
}
},

{
title: 'This task will execute.',
task: async (): Promise<void> => {
await delay(500)

console.log('i am logging some more stuff out')
}
},

{
title: 'This task will execute.',
task: async (): Promise<void> => {
await delay(500)

process.stdout.write('writing something here' + EOL)
process.stdout.write('writing something here' + EOL)
}
}
],
{
concurrent: true,
rendererOptions: {
logger: new ListrLogger({ processOutput: new ProcessOutput(null, null, { dump: [] }) })
}
}
)

await tasks.run()
6 changes: 3 additions & 3 deletions src/utils/process-output/process-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export class ProcessOutput {
public readonly stream: ProcessOutputStreamMap
private active: boolean

constructor (stdout: NodeJS.WriteStream = process.stdout, stderr: NodeJS.WriteStream = process.stderr, private readonly options?: ProcessOutputOptions) {
constructor (stdout: NodeJS.WriteStream, stderr: NodeJS.WriteStream, private readonly options?: ProcessOutputOptions) {
this.stream = {
stdout: new ProcessOutputStream(stdout),
stderr: new ProcessOutputStream(stderr)
stdout: new ProcessOutputStream(stdout ?? process.stdout),
stderr: new ProcessOutputStream(stderr ?? process.stderr)
}

this.options = {
Expand Down

0 comments on commit c1ede63

Please sign in to comment.