@topcli/prompts 0.1.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -10
- package/index.d.ts +9 -5
- package/index.js +7 -4
- package/package.json +1 -1
- package/src/{text-prompt.js → question-prompt.js} +1 -1
- package/src/validators.js +6 -0
package/README.md
CHANGED
|
@@ -27,23 +27,28 @@ $ yarn add @topcli/prompts
|
|
|
27
27
|
You can locally run `node ./demo.js`
|
|
28
28
|
|
|
29
29
|
```js
|
|
30
|
-
import { prompt, confirm, select } from '@topcli/prompts'
|
|
30
|
+
import { prompt, confirm, select } from '@topcli/prompts';
|
|
31
31
|
|
|
32
|
-
const kTestRunner = ['node', 'tap', 'tape', 'vitest', 'mocha', 'ava']
|
|
32
|
+
const kTestRunner = ['node', 'tap', 'tape', 'vitest', 'mocha', 'ava'];
|
|
33
33
|
|
|
34
|
-
const name = await prompt('Project name ?')
|
|
35
|
-
const runner = await select('Choose a test runner', {
|
|
36
|
-
|
|
34
|
+
const name = await prompt('Project name ?');
|
|
35
|
+
const runner = await select('Choose a test runner', {
|
|
36
|
+
choices: kTestRunner,
|
|
37
|
+
maxVisible: 5
|
|
38
|
+
});
|
|
39
|
+
const isCLI = await confirm('Your project is a CLI ?', {
|
|
40
|
+
initial: true
|
|
41
|
+
});
|
|
37
42
|
|
|
38
|
-
console.log(name, runner, isCLI)
|
|
43
|
+
console.log(name, runner, isCLI);^
|
|
39
44
|
```
|
|
40
45
|
|
|
41
46
|
## API
|
|
42
47
|
|
|
43
|
-
### `
|
|
48
|
+
### `question()`
|
|
44
49
|
|
|
45
50
|
```ts
|
|
46
|
-
|
|
51
|
+
question(message: string, options?: PromptOptions): Promise<string>
|
|
47
52
|
```
|
|
48
53
|
|
|
49
54
|
Simple prompt, similar to `rl.question()` with an improved UI.
|
|
@@ -52,14 +57,26 @@ Use `options.validators` to handle user input.
|
|
|
52
57
|
**Example**
|
|
53
58
|
|
|
54
59
|
```js
|
|
55
|
-
const packageName = await
|
|
60
|
+
const packageName = await question('Package name', {
|
|
56
61
|
validators: [
|
|
57
62
|
{
|
|
58
63
|
validate: (value) => !existsSync(join(process.cwd(), value)),
|
|
59
64
|
error: (value) => `Folder ${value} already exists`
|
|
60
65
|
}
|
|
61
66
|
]
|
|
62
|
-
})
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**This package provide some validators for common usage**
|
|
71
|
+
|
|
72
|
+
- required
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
import { prompt, required } from "@topcli/prompts";
|
|
76
|
+
|
|
77
|
+
const name = await prompt("What's your name ?", {
|
|
78
|
+
validators: [required()]
|
|
79
|
+
});
|
|
63
80
|
```
|
|
64
81
|
|
|
65
82
|
### `select()`
|
package/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export interface PromptOptions {
|
|
2
|
-
validators?:
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
validators?: Validator[];
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export interface Validator {
|
|
6
|
+
validate: (input: string) => boolean;
|
|
7
|
+
error: (input?: string) => string;
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export interface Choice {
|
|
@@ -21,6 +23,8 @@ export interface ConfirmOptions {
|
|
|
21
23
|
initial?: boolean;
|
|
22
24
|
}
|
|
23
25
|
|
|
24
|
-
export function
|
|
26
|
+
export function question(message: string, options?: PromptOptions): Promise<string>;
|
|
25
27
|
export function select(message: string, options: SelectOptions): Promise<string>;
|
|
26
28
|
export function confirm(message: string, options?: ConfirmOptions): Promise<string>;
|
|
29
|
+
|
|
30
|
+
export function required(): Validator;
|
package/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
// Import Internal Dependencies
|
|
2
2
|
import { ConfirmPrompt } from "./src/confirm-prompt.js";
|
|
3
3
|
import { SelectPrompt } from "./src/select-prompt.js";
|
|
4
|
-
import {
|
|
4
|
+
import { QuestionPrompt } from "./src/question-prompt.js";
|
|
5
|
+
import { required } from "./src/validators.js";
|
|
5
6
|
|
|
6
|
-
export async function
|
|
7
|
-
const
|
|
7
|
+
export async function question(message, options = {}) {
|
|
8
|
+
const questionPrompt = new QuestionPrompt(message, options);
|
|
8
9
|
|
|
9
|
-
return
|
|
10
|
+
return questionPrompt.question();
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export async function select(message, options) {
|
|
@@ -20,3 +21,5 @@ export async function confirm(message, options) {
|
|
|
20
21
|
|
|
21
22
|
return confirmPrompt.confirm();
|
|
22
23
|
}
|
|
24
|
+
|
|
25
|
+
export { required };
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@ import stripAnsi from "strip-ansi";
|
|
|
9
9
|
import { AbstractPrompt } from "./abstract-prompt.js";
|
|
10
10
|
import { SYMBOLS } from "./constants.js";
|
|
11
11
|
|
|
12
|
-
export class
|
|
12
|
+
export class QuestionPrompt extends AbstractPrompt {
|
|
13
13
|
#validators;
|
|
14
14
|
|
|
15
15
|
constructor(message, options = {}) {
|