@inquirer/confirm 6.3.2 → 6.3.4
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 +81 -5
- package/dist/index.d.ts +10 -0
- package/dist/index.js +29 -6
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -8,46 +8,116 @@ Simple interactive command line prompt to gather boolean input from users.
|
|
|
8
8
|
|
|
9
9
|
<table>
|
|
10
10
|
<tr>
|
|
11
|
-
<th>npm</th>
|
|
12
11
|
<th>yarn</th>
|
|
12
|
+
<th>pnpm</th>
|
|
13
13
|
</tr>
|
|
14
14
|
<tr>
|
|
15
15
|
<td>
|
|
16
16
|
|
|
17
17
|
```sh
|
|
18
|
-
|
|
18
|
+
yarn add @inquirer/prompts
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
</td>
|
|
22
22
|
<td>
|
|
23
23
|
|
|
24
24
|
```sh
|
|
25
|
-
|
|
25
|
+
pnpm add @inquirer/prompts
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
</td>
|
|
29
29
|
</tr>
|
|
30
30
|
<tr>
|
|
31
|
-
<
|
|
31
|
+
<th>bun</th>
|
|
32
|
+
<th>deno</th>
|
|
32
33
|
</tr>
|
|
33
34
|
<tr>
|
|
34
35
|
<td>
|
|
35
36
|
|
|
36
37
|
```sh
|
|
37
|
-
|
|
38
|
+
bun add @inquirer/prompts
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
</td>
|
|
42
|
+
<td>
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
deno add npm:@inquirer/prompts
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
</td>
|
|
49
|
+
</tr>
|
|
50
|
+
<tr>
|
|
51
|
+
<th colSpan="2">npm</th>
|
|
52
|
+
</tr>
|
|
53
|
+
<tr>
|
|
54
|
+
<td colSpan="2">
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
npm install @inquirer/prompts
|
|
38
58
|
```
|
|
39
59
|
|
|
40
60
|
</td>
|
|
61
|
+
</tr>
|
|
62
|
+
<tr>
|
|
63
|
+
<td colSpan="2" align="center">Or</td>
|
|
64
|
+
</tr>
|
|
65
|
+
<tr>
|
|
66
|
+
<th>yarn</th>
|
|
67
|
+
<th>pnpm</th>
|
|
68
|
+
</tr>
|
|
69
|
+
<tr>
|
|
41
70
|
<td>
|
|
42
71
|
|
|
43
72
|
```sh
|
|
44
73
|
yarn add @inquirer/confirm
|
|
45
74
|
```
|
|
46
75
|
|
|
76
|
+
</td>
|
|
77
|
+
<td>
|
|
78
|
+
|
|
79
|
+
```sh
|
|
80
|
+
pnpm add @inquirer/confirm
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
</td>
|
|
84
|
+
</tr>
|
|
85
|
+
<tr>
|
|
86
|
+
<th>bun</th>
|
|
87
|
+
<th>deno</th>
|
|
88
|
+
</tr>
|
|
89
|
+
<tr>
|
|
90
|
+
<td>
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
bun add @inquirer/confirm
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
</td>
|
|
97
|
+
<td>
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
deno add npm:@inquirer/confirm
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
</td>
|
|
104
|
+
</tr>
|
|
105
|
+
<tr>
|
|
106
|
+
<th colSpan="2">npm</th>
|
|
107
|
+
</tr>
|
|
108
|
+
<tr>
|
|
109
|
+
<td colSpan="2">
|
|
110
|
+
|
|
111
|
+
```sh
|
|
112
|
+
npm install @inquirer/confirm
|
|
113
|
+
```
|
|
114
|
+
|
|
47
115
|
</td>
|
|
48
116
|
</tr>
|
|
49
117
|
</table>
|
|
50
118
|
|
|
119
|
+
On Deno, prompts detect the terminal's unicode and color support from the environment, so run your script with `--allow-env`.
|
|
120
|
+
|
|
51
121
|
# Usage
|
|
52
122
|
|
|
53
123
|
```js
|
|
@@ -81,6 +151,7 @@ type Theme = {
|
|
|
81
151
|
keywords: {
|
|
82
152
|
yes: string;
|
|
83
153
|
no: string;
|
|
154
|
+
error: (keywords: { yes: string; no: string }) => string;
|
|
84
155
|
};
|
|
85
156
|
style: {
|
|
86
157
|
answer: (text: string) => string;
|
|
@@ -94,6 +165,11 @@ type Theme = {
|
|
|
94
165
|
The `keywords` property defines the words accepted as "yes" and "no". Matching is
|
|
95
166
|
prefix-based and case-insensitive, and the first character of each word is shown in
|
|
96
167
|
the hint (e.g. `Y/n`). The matched word is also displayed once the prompt is answered.
|
|
168
|
+
|
|
169
|
+
The built-in `y`/`n`/`yes`/`no` answers are always accepted, whatever the keywords
|
|
170
|
+
are. Unrecognized input shows the `keywords.error` message instead of silently
|
|
171
|
+
falling back on the default answer.
|
|
172
|
+
|
|
97
173
|
Overriding `keywords` is how `@inquirer/i18n` localizes the prompt:
|
|
98
174
|
|
|
99
175
|
```ts
|
package/dist/index.d.ts
CHANGED
|
@@ -11,10 +11,20 @@ type ConfirmTheme = {
|
|
|
11
11
|
* Words accepted as "yes" and "no" answers. Matching is prefix-based and
|
|
12
12
|
* case-insensitive, and the first character of each word is shown in the
|
|
13
13
|
* hint. These words are also displayed once the prompt is answered.
|
|
14
|
+
* Regardless of these keywords, the built-in `y`/`n`/`yes`/`no` answers are
|
|
15
|
+
* always accepted.
|
|
14
16
|
*/
|
|
15
17
|
keywords: {
|
|
16
18
|
yes: string;
|
|
17
19
|
no: string;
|
|
20
|
+
/**
|
|
21
|
+
* Message shown when the submitted input matches no keyword. Receives the
|
|
22
|
+
* yes/no keywords; this is how `@inquirer/i18n` localizes the message.
|
|
23
|
+
*/
|
|
24
|
+
error: (keywords: {
|
|
25
|
+
yes: string;
|
|
26
|
+
no: string;
|
|
27
|
+
}) => string;
|
|
18
28
|
};
|
|
19
29
|
style: {
|
|
20
30
|
/**
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ const confirmTheme = {
|
|
|
4
4
|
keywords: {
|
|
5
5
|
yes: 'Yes',
|
|
6
6
|
no: 'No',
|
|
7
|
+
error: ({ yes, no }) => `You must answer with "${yes}" or "${no}"`,
|
|
7
8
|
},
|
|
8
9
|
style: {
|
|
9
10
|
confirmDefault: (text) => {
|
|
@@ -18,6 +19,7 @@ const confirmTheme = {
|
|
|
18
19
|
export default createPrompt((config, done) => {
|
|
19
20
|
const [status, setStatus] = useState('idle');
|
|
20
21
|
const [value, setValue] = useState('');
|
|
22
|
+
const [errorMsg, setError] = useState();
|
|
21
23
|
const theme = makeTheme(confirmTheme, config.theme);
|
|
22
24
|
const prefix = usePrefix({ status, theme });
|
|
23
25
|
const { yes, no } = theme.keywords;
|
|
@@ -32,33 +34,50 @@ export default createPrompt((config, done) => {
|
|
|
32
34
|
return value ? yes : no;
|
|
33
35
|
}
|
|
34
36
|
const { transformer = boolToString } = config;
|
|
35
|
-
|
|
37
|
+
// Note: the built-in English `y`/`n`/`yes`/`no` answers are always accepted,
|
|
38
|
+
// whatever the localized keywords are; typing a single ASCII key is the
|
|
39
|
+
// terminal muscle memory, even in non-Latin locales.
|
|
40
|
+
function getBooleanValue(value) {
|
|
36
41
|
const v = value.trim().toLowerCase();
|
|
42
|
+
// Empty input selects the default answer (the one highlighted in the hint).
|
|
37
43
|
if (v === '')
|
|
38
|
-
return
|
|
44
|
+
return config.default !== false;
|
|
39
45
|
if (yes.toLowerCase().startsWith(v))
|
|
40
46
|
return true;
|
|
41
47
|
if (no.toLowerCase().startsWith(v))
|
|
42
48
|
return false;
|
|
43
|
-
|
|
49
|
+
if ('yes'.startsWith(v))
|
|
50
|
+
return true;
|
|
51
|
+
if ('no'.startsWith(v))
|
|
52
|
+
return false;
|
|
53
|
+
// Unrecognized input is rejected by the caller (shows an error).
|
|
54
|
+
return undefined;
|
|
44
55
|
}
|
|
45
56
|
useKeypress((key, rl) => {
|
|
46
57
|
if (status !== 'idle')
|
|
47
58
|
return;
|
|
48
59
|
if (isEnterKey(key)) {
|
|
49
|
-
const answer = getBooleanValue(value
|
|
60
|
+
const answer = getBooleanValue(value);
|
|
61
|
+
if (answer === undefined) {
|
|
62
|
+
// Reject unrecognized input instead of silently falling back on the
|
|
63
|
+
// default. Restore the typed input (the line event cleared it).
|
|
64
|
+
rl.write(value);
|
|
65
|
+
setError(theme.keywords.error({ yes, no }));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
50
68
|
setValue(transformer(answer));
|
|
51
69
|
setStatus('done');
|
|
52
70
|
done(answer);
|
|
53
71
|
}
|
|
54
72
|
else if (isTabKey(key)) {
|
|
55
|
-
const answer = boolToString(!getBooleanValue(value
|
|
73
|
+
const answer = boolToString(!(getBooleanValue(value) ?? config.default !== false));
|
|
56
74
|
rl.clearLine(0); // Remove the tab character.
|
|
57
75
|
rl.write(answer);
|
|
58
76
|
setValue(answer);
|
|
59
77
|
}
|
|
60
78
|
else {
|
|
61
79
|
setValue(rl.line);
|
|
80
|
+
setError(undefined);
|
|
62
81
|
}
|
|
63
82
|
});
|
|
64
83
|
let formattedValue = value;
|
|
@@ -70,5 +89,9 @@ export default createPrompt((config, done) => {
|
|
|
70
89
|
defaultValue = ` ${theme.style.defaultAnswer(hint)}`;
|
|
71
90
|
}
|
|
72
91
|
const message = theme.style.message(config.message, status);
|
|
73
|
-
|
|
92
|
+
let error = '';
|
|
93
|
+
if (errorMsg) {
|
|
94
|
+
error = theme.style.error(errorMsg);
|
|
95
|
+
}
|
|
96
|
+
return [`${prefix} ${message}${defaultValue} ${formattedValue}`, error];
|
|
74
97
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/confirm",
|
|
3
|
-
"version": "6.3.
|
|
3
|
+
"version": "6.3.4",
|
|
4
4
|
"description": "Inquirer confirm prompt",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"answer",
|
|
@@ -67,8 +67,8 @@
|
|
|
67
67
|
"tsc": "tsc"
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@inquirer/core": "^12.0.
|
|
71
|
-
"@inquirer/type": "4.
|
|
70
|
+
"@inquirer/core": "^12.0.5",
|
|
71
|
+
"@inquirer/type": "4.2.0"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@repo/tsconfig": "0.0.0",
|
|
@@ -87,5 +87,5 @@
|
|
|
87
87
|
},
|
|
88
88
|
"main": "./dist/index.js",
|
|
89
89
|
"types": "./dist/index.d.ts",
|
|
90
|
-
"gitHead": "
|
|
90
|
+
"gitHead": "ea3130ebc609a6a3910b5397244044de7eda3d05"
|
|
91
91
|
}
|