Skip to content

Instantly share code, notes, and snippets.

@andywer
Created September 16, 2016 07:00
Show Gist options
  • Save andywer/11855be9cb700be4ddae520b3330437e to your computer and use it in GitHub Desktop.
Save andywer/11855be9cb700be4ddae520b3330437e to your computer and use it in GitHub Desktop.
Listr 0.6.0 Issue
const execa = require('execa')
const Listr = require('listr')
const Observable = require('zen-observable')
const rootListr = new Listr([
{
title: 'root',
task: () => new Listr([
{
title: 'sleep 1s',
task: () => shellObservable('sleep 1')
},
{
title: 'sleep 2s',
task: () => shellObservable('sleep 2')
}
], { concurrent: true })
}
])
rootListr.run().catch((error) => {
console.error(error)
})
///////////////////////////
// Copied from npm-launch:
/**
* @param {string} command
* @return {Observable<string>}
* Notifies its subscribers constantly about the lastest line of stdout/stderr output.
*/
function shellObservable (command) {
const promisifiedProcess = execa.shell(command)
return new Observable((observer) => {
promisifiedProcess
.then(() => observer.complete())
.catch((error) => observer.error(error))
if (promisifiedProcess.stdout) {
observeStream(promisifiedProcess.stdout, (mostRecentLine) => observer.next(mostRecentLine))
}
if (promisifiedProcess.stderr) {
observeStream(promisifiedProcess.stderr, (mostRecentLine) => observer.next(mostRecentLine))
}
})
}
/**
* @param {Stream} stream
* @param {function} onNewLine function (mostRecentLine: string)
* @return void
*/
function observeStream (stream, onNewLine) {
stream.on('data', (text) => {
if (typeof text !== 'string') {
return
}
const mostRecentLine = text.split('\n').filter((line) => line.length > 0).pop()
onNewLine(mostRecentLine)
})
}
{
"name": "listr-0.6.0-test",
"version": "0.0.0",
"dependencies": {
"execa": "^0.4.0",
"listr": "^0.6.0",
"zen-observable": "^0.3.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment