privatenumber / tasuku

✅ タスク — The minimal task visualizer for Node.js
MIT License
1.93k stars 30 forks source link

Recommended way to use prompts? #24

Open mmkal opened 6 months ago

mmkal commented 6 months ago

Is your feature request related to a problem?

Not exactly - I currently have a CLI app that's using listr2, but tasuku looks nicer in that each task can have a return value, rather than listr2 which relies on mutating the context, which isn't as type safe.

listr2 has a specific prompt adapter. I'm not sure if tasuku would need an adapter, but it could be useful to have a docs suggestion of how you would recommend integrating enquirer or something

Describe the solution you'd like

What would be really great, is some sort of cleye x taskuku x enquirer thing. Where cleye defines its arguments/flags, andtasuku adds tasks to prompt the user for those arguments if they haven't been defined already. It could be built in userland, but wondered if there'd be interest in incorporating here.

Describe alternatives you've considered

DIY - just install enquirer.

Additional context

dstaver commented 5 months ago

I ran into the same issue and did this to get a working prompt:

import type { TaskInnerAPI } from 'tasuku'
import { confirm } from '@inquirer/prompts'
import { Stream } from 'node:stream'

export function confirmTask(
  message: string,
  setOutput: TaskInnerAPI['setOutput'],
) {
  return confirm(
    {
      message,
    },
    {
      output: new Stream.Writable({
        write(chunk, _encoding, next) {
          setOutput(String(chunk))
          next()
        },
      }),
    },
  )
}

Use it like this:

await task('Some task', async ({ setError, setOutput }) => {
  const confirmed = await confirmTask('Delete file', setOutput)
  if (!confirmed) {
    // Cancel...
    setError('Aborted')
  } else {
    // Delete file...
  }
})