Skip to content

Commit

Permalink
fix(stream): fixed streams added example
Browse files Browse the repository at this point in the history
fix #37
  • Loading branch information
cenk1cenk2 committed Jun 3, 2020
1 parent ca7752a commit 614d89f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ new Listr<Ctx>(

Since observables and streams are supported they can also be used to generate output.

_Please refer to [examples section](examples/stream.example.ts) for more detailed and further examples._

```typescript
new Listr<Ctx>(
[
Expand Down
42 changes: 42 additions & 0 deletions examples/stream.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable @typescript-eslint/no-empty-function */

import { spawn } from 'child_process'
import { Readable } from 'stream'

import { Listr } from '@root/index'
import { Logger } from '@utils/logger'

interface Ctx {
output: string
}

const logger = new Logger({ useIcons: false })

async function main (): Promise<void> {
let task: Listr<Ctx>

logger.start('Example output from a task.')

// eslint-disable-next-line prefer-const
task = new Listr<Ctx>([
{
title: 'This task will execute.',
task: async (): Promise<Readable> => {
return spawn('ls').stdout
},
options: {
persistentOutput: true
}
}
], { concurrent: false })

try {
const context = await task.run()
logger.success(`Context: ${JSON.stringify(context)}`)
} catch(e) {
logger.fail(e)
}

}

main()
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"idle"
],
"dependencies": {
"@samverschueren/stream-to-observable": "^0.3.0",
"chalk": "^4.0.0",
"cli-cursor": "^3.1.0",
"cli-truncate": "^2.1.0",
Expand Down Expand Up @@ -95,4 +94,4 @@
"path": "./node_modules/cz-conventional-changelog"
}
}
}
}
2 changes: 2 additions & 0 deletions src/lib/task-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export class TaskWrapper<Ctx, Renderer extends ListrRendererFactory> implements
return through((chunk: string) => {
const pattern = new RegExp('(?:\\u001b|\\u009b)\\[[\\=><~/#&.:=?%@~_-]*[0-9]*[\\a-ln-tqyz=><~/#&.:=?%@~_-]+', 'gmi')

chunk = chunk.toString()

chunk = chunk.replace(pattern, '')
chunk = chunk.replace(new RegExp(/\u0007/, 'gmi'), '')
if (chunk !== '') {
Expand Down
18 changes: 14 additions & 4 deletions src/lib/task.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sttoob from '@samverschueren/stream-to-observable'
import { Observable, Subject } from 'rxjs'
import { Stream } from 'stream'
import { Readable } from 'stream'
import { v4 as uuid } from 'uuid'

import {
Expand Down Expand Up @@ -143,9 +142,20 @@ export class Task<Ctx, Renderer extends ListrRendererFactory> extends Subject<Li
} else if (result instanceof Promise) {
// Detect promise
result = result.then(handleResult)
} else if (result instanceof Stream.Readable) {
} else if (result instanceof Readable) {
// Detect stream
result = sttoob(result)
result = new Promise((resolve, reject) => {
result.on('data', (data: Buffer) => {
this.output = data.toString()

this.next({
type: 'DATA',
data: data.toString()
})
})
result.on('error', (error: Error) => reject(error))
result.on('end', () => resolve())
})
} else if (result instanceof Observable) {
// Detect Observable
result = new Promise((resolve, reject) => {
Expand Down

0 comments on commit 614d89f

Please sign in to comment.