Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended way to use prompts? #24

Open
mmkal opened this issue Mar 18, 2024 · 1 comment
Open

Recommended way to use prompts? #24

mmkal opened this issue Mar 18, 2024 · 1 comment

Comments

@mmkal
Copy link

mmkal commented Mar 18, 2024

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
Copy link

dstaver commented Apr 25, 2024

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...
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants