oclif/cli-ux

cli.table throws when output is piped to head

Opened this issue · 1 comments

OS: Linux
cli-ux: 5.4.5
node: v12.16.1
head: head (GNU coreutils) 8.28

When piping the output of cli.table() to head I get Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed.

Error:

Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at Object.Table.options.printLine (~/oclif_bugs/head/node_modules/cli-ux/lib/styled/table.js:39:60)
    at ~/oclif_bugs/head/node_modules/cli-ux/lib/styled/table.js:257:25
    at Array.forEach (<anonymous>)
    at Table.outputTable (~/oclif_bugs/head/node_modules/cli-ux/lib/styled/table.js:243:27)
    at Table.display (~/oclif_bugs/head/node_modules/cli-ux/lib/styled/table.js:103:22)
    at Object.table (~/oclif_bugs/head/node_modules/cli-ux/lib/styled/table.js:263:39)
    at HeadCommand.run (~/oclif_bugs/head/src/index.js:19:9)

Steps To Reproduce

Create a single oclif app.
Copy / Paste the script below as a command:

const {Command, flags} = require('@oclif/command')
const fs = require('fs').promises
const path = require('path')
const lockfile = require('@yarnpkg/lockfile')
const { cli } = require('cli-ux')
const R = require('ramda')

class HeadCommand extends Command {
  async run() {
    const {flags} = this.parse(HeadCommand)

    const yarnLock = new URL(`file://${path.resolve(__dirname, '..')}/yarn.lock`)
    const lotOfText = await fs.readFile(yarnLock)
    const data = lockfile.parse(lotOfText.toString()).object
    const dataList = []

    R.forEachObjIndexed((value) => { dataList.push(R.pick(['version'], value))}, data)

    cli.table(dataList, {version:{}}, flags)
  }
}

HeadCommand.description = `Describe the command here
...
Extra documentation goes here
`

HeadCommand.flags = {
  // add flags from cli.table
  ...cli.table.flags(),
  // add --version flag to show CLI version
  version: flags.version({char: 'v'}),
  // add --help flag to show CLI version
  help: flags.help({char: 'h'}),
}

module.exports = HeadCommand

Install: yarn add cli-ux @yarnpkg/lockfile ramda

Run: ./bin/run | head

If you have created a "multi" app, then the path to yarn.lock need to be changed.

Confirmed. Will take a look.