Skip to content

Commit

Permalink
feat(prompt): make prompt module optional, be more compatible to unde…
Browse files Browse the repository at this point in the history
…rlying enqurier

fix #34
  • Loading branch information
cenk1cenk2 committed Jun 2, 2020
1 parent b34e9d0 commit 64cecc1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ _Please refer to [Throw Errors Section](#Throw-Errors) for more detailed and fur

The input module uses the beautiful [enquirer](https://www.npmjs.com/package/enquirer).

> ## Attention: Enquirer is a optional dependency. Please install it first.
So with running a `task.prompt` function, you can get access to any [enquirer](https://www.npmjs.com/package/enquirer) default prompts as well as using a custom enquirer prompt.

To get an input you can assign the task a new prompt in an async function and write the response to the context.
Expand All @@ -296,6 +298,10 @@ Prompts always rendered at the bottom of the tasks when using the default render

_Please note that I rewrote the types for enquirer, since some of them was failing for me. So it may have a chance of having some mistakes in it since I usually do not use all of them._

**>v2.1.0, defining the prompt style has been changed a little..**

##### Single Prompt

```typescript
new Listr<Ctx>(
[
Expand All @@ -317,6 +323,34 @@ new Listr<Ctx>(
)
```

##### Multiple Prompts

**Important: If you want to pass in an array of prompts, becareful that you should name them. This gets handled to return the single value directly in non-array variation of prompts by internal trickery.**

```typescript
new Listr<Ctx>(
[
{
title: 'This task will get your input.',
task: async (ctx, task): Promise<void> => {
ctx.input = await task.prompt<{ first: boolean; second: boolean }>([
{ type: 'Toggle', name: 'first', message: 'Do you love me?' },
{ type: 'Toggle', name: 'second', message: 'Do you love me?' }
])
// do something
if (ctx.input.first === false) {
logger.log('oh okay')
}
if (ctx.input.second === false) {
throw new Error('You did not had to tell me for the second time')
}
}
}
],
{ concurrent: false }
)
```

#### Use an Custom Prompt

You can either use a custom prompt out of the npm registry or custom-created one as long as it works with [enquirer](https://www.npmjs.com/package/enquirer), it will work expectedly. Instead of passing in the prompt name use the not-generated class.
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
"@types/uuid": "^7.0.2",
"cz-conventional-changelog": "3.2.0",
"delay": "^4.3.0",
"enquirer": "^2.3.5",
"eslint": "^7.1.0",
"husky": "^4.2.5",
"jest": "^26.0.1",
Expand All @@ -87,9 +86,12 @@
"tscpaths": "^0.0.9",
"typescript": "^3.9.3"
},
"optionalDependencies": {
"enquirer": ">= 2.3.0 < 3"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
}
4 changes: 2 additions & 2 deletions src/utils/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export async function createPrompt (options: PromptOptions | PromptOptions[], se
}

options = options.reduce((o, option) => {
return [ ...o, Object.assign(option, { stdout: this.stdout(), onCancel: cancelCallback.bind(this)(settings) }) ]
return [ ...o, Object.assign(option, { stdout: this.stdout(), onCancel: cancelCallback.bind(this, settings) }) ]
}, [])

try {
const { prompt } = (await import('enquirer') as any).default
const { prompt } = ((await import('enquirer')) as any).default
// if this is a custom prompt
return prompt(options as any)
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ end-of-stream@^1.1.0:
dependencies:
once "^1.4.0"

enquirer@^2.3.5:
"enquirer@>= 2.3.0 < 3", enquirer@^2.3.5:
version "2.3.5"
resolved "https://registry.yarnpkg.com/enquirer/-/enquirer-2.3.5.tgz#3ab2b838df0a9d8ab9e7dff235b0e8712ef92381"
integrity sha512-BNT1C08P9XD0vNg3J475yIUG+mVdp9T6towYFHUv897X0KoHBjB1shyrNmhmtHWKP17iSWgo7Gqh7BBuzLZMSA==
Expand Down

0 comments on commit 64cecc1

Please sign in to comment.