@inquirer/input 4.2.5 → 4.3.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 CHANGED
@@ -4,16 +4,6 @@ Interactive free text input component for command line interfaces. Supports vali
4
4
 
5
5
  ![Input prompt](https://cdn.rawgit.com/SBoudrias/Inquirer.js/28ae8337ba51d93e359ef4f7ee24e79b69898962/assets/screenshots/input.svg)
6
6
 
7
- # Special Thanks
8
-
9
- <div align="center" markdown="1">
10
-
11
- [![Graphite](https://github.com/user-attachments/assets/53db40ca-2254-481a-a094-6597f8716e29)](https://graphite.dev/?utm_source=npmjs&utm_medium=repo&utm_campaign=inquirerjs)<br>
12
-
13
- ### [Graphite is the AI developer productivity platform helping teams on GitHub ship higher quality software, faster](https://graphite.dev/?utm_source=npmjs&utm_medium=repo&utm_campaign=inquirerjs)
14
-
15
- </div>
16
-
17
7
  # Installation
18
8
 
19
9
  <table>
@@ -70,15 +60,17 @@ const answer = await input({ message: 'Enter your name' });
70
60
 
71
61
  ## Options
72
62
 
73
- | Property | Type | Required | Description |
74
- | ----------- | ----------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
75
- | message | `string` | yes | The question to ask |
76
- | default | `string` | no | Default value if no answer is provided; see the prefill option below for governing it's behaviour. |
77
- | prefill | `'tab' \| 'editable'` | no | Defaults to `'tab'`. If set to `'tab'`, pressing `backspace` will clear the default and pressing `tab` will inline the value for edits; If set to `'editable'`, the default value will already be inlined to edit. |
78
- | required | `boolean` | no | Defaults to `false`. If set to true, `undefined` (empty) will not be accepted for this. |
79
- | transformer | `(string, { isFinal: boolean }) => string` | no | Transform/Format the raw value entered by the user. Once the prompt is completed, `isFinal` will be `true`. This function is purely visual, modify the answer in your code if needed. |
80
- | validate | `string => boolean \| string \| Promise<boolean \| string>` | no | On submit, validate the filtered answered content. When returning a string, it'll be used as the error message displayed to the user. Note: returning a rejected promise, we'll assume a code error happened and crash. |
81
- | theme | [See Theming](#Theming) | no | Customize look of the prompt. |
63
+ | Property | Type | Required | Description |
64
+ | ------------ | ----------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
65
+ | message | `string` | yes | The question to ask |
66
+ | default | `string` | no | Default value if no answer is provided; see the prefill option below for governing it's behaviour. |
67
+ | prefill | `'tab' \| 'editable'` | no | Defaults to `'tab'`. If set to `'tab'`, pressing `backspace` will clear the default and pressing `tab` will inline the value for edits; If set to `'editable'`, the default value will already be inlined to edit. |
68
+ | required | `boolean` | no | Defaults to `false`. If set to true, `undefined` (empty) will not be accepted for this. |
69
+ | transformer | `(string, { isFinal: boolean }) => string` | no | Transform/Format the raw value entered by the user. Once the prompt is completed, `isFinal` will be `true`. This function is purely visual, modify the answer in your code if needed. |
70
+ | validate | `string => boolean \| string \| Promise<boolean \| string>` | no | On submit, validate the filtered answered content. When returning a string, it'll be used as the error message displayed to the user. Note: returning a rejected promise, we'll assume a code error happened and crash. |
71
+ | pattern | `RegExp` | no | Regular expression to validate the input against. If the input doesn't match the pattern, validation will fail with the error message specified in `patternError`. |
72
+ | patternError | `string` | no | Error message to display when the input doesn't match the `pattern`. Defaults to `'Invalid input'`. |
73
+ | theme | [See Theming](#Theming) | no | Customize look of the prompt. |
82
74
 
83
75
  ## Theming
84
76
 
@@ -13,6 +13,8 @@ type InputConfig = {
13
13
  }) => string;
14
14
  validate?: (value: string) => boolean | string | Promise<string | boolean>;
15
15
  theme?: PartialDeep<Theme<InputTheme>>;
16
+ pattern?: RegExp;
17
+ patternError?: string;
16
18
  };
17
19
  declare const _default: import("@inquirer/type").Prompt<string, InputConfig>;
18
20
  export default _default;
@@ -5,13 +5,26 @@ const inputTheme = {
5
5
  validationFailureMode: 'keep',
6
6
  };
7
7
  exports.default = (0, core_1.createPrompt)((config, done) => {
8
- const { required, validate = () => true, prefill = 'tab' } = config;
8
+ const { prefill = 'tab' } = config;
9
9
  const theme = (0, core_1.makeTheme)(inputTheme, config.theme);
10
10
  const [status, setStatus] = (0, core_1.useState)('idle');
11
11
  const [defaultValue = '', setDefaultValue] = (0, core_1.useState)(config.default);
12
12
  const [errorMsg, setError] = (0, core_1.useState)();
13
13
  const [value, setValue] = (0, core_1.useState)('');
14
14
  const prefix = (0, core_1.usePrefix)({ status, theme });
15
+ async function validate(value) {
16
+ const { required, pattern, patternError = 'Invalid input' } = config;
17
+ if (required && !value) {
18
+ return 'You must provide a value';
19
+ }
20
+ if (pattern && !pattern.test(value)) {
21
+ return patternError;
22
+ }
23
+ if (typeof config.validate === 'function') {
24
+ return (await config.validate(value)) || 'You must provide a valid value';
25
+ }
26
+ return true;
27
+ }
15
28
  (0, core_1.useKeypress)(async (key, rl) => {
16
29
  // Ignore keypress while our prompt is doing other processing.
17
30
  if (status !== 'idle') {
@@ -20,7 +33,7 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
20
33
  if ((0, core_1.isEnterKey)(key)) {
21
34
  const answer = value || defaultValue;
22
35
  setStatus('loading');
23
- const isValid = required && !answer ? 'You must provide a value' : await validate(answer);
36
+ const isValid = await validate(answer);
24
37
  if (isValid === true) {
25
38
  setValue(answer);
26
39
  setStatus('done');
@@ -35,7 +48,7 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
35
48
  // get cleared, forcing the user to re-enter the value instead of fixing it.
36
49
  rl.write(value);
37
50
  }
38
- setError(isValid || 'You must provide a valid value');
51
+ setError(isValid);
39
52
  setStatus('idle');
40
53
  }
41
54
  }
@@ -13,6 +13,8 @@ type InputConfig = {
13
13
  }) => string;
14
14
  validate?: (value: string) => boolean | string | Promise<string | boolean>;
15
15
  theme?: PartialDeep<Theme<InputTheme>>;
16
+ pattern?: RegExp;
17
+ patternError?: string;
16
18
  };
17
19
  declare const _default: import("@inquirer/type").Prompt<string, InputConfig>;
18
20
  export default _default;
package/dist/esm/index.js CHANGED
@@ -3,13 +3,26 @@ const inputTheme = {
3
3
  validationFailureMode: 'keep',
4
4
  };
5
5
  export default createPrompt((config, done) => {
6
- const { required, validate = () => true, prefill = 'tab' } = config;
6
+ const { prefill = 'tab' } = config;
7
7
  const theme = makeTheme(inputTheme, config.theme);
8
8
  const [status, setStatus] = useState('idle');
9
9
  const [defaultValue = '', setDefaultValue] = useState(config.default);
10
10
  const [errorMsg, setError] = useState();
11
11
  const [value, setValue] = useState('');
12
12
  const prefix = usePrefix({ status, theme });
13
+ async function validate(value) {
14
+ const { required, pattern, patternError = 'Invalid input' } = config;
15
+ if (required && !value) {
16
+ return 'You must provide a value';
17
+ }
18
+ if (pattern && !pattern.test(value)) {
19
+ return patternError;
20
+ }
21
+ if (typeof config.validate === 'function') {
22
+ return (await config.validate(value)) || 'You must provide a valid value';
23
+ }
24
+ return true;
25
+ }
13
26
  useKeypress(async (key, rl) => {
14
27
  // Ignore keypress while our prompt is doing other processing.
15
28
  if (status !== 'idle') {
@@ -18,7 +31,7 @@ export default createPrompt((config, done) => {
18
31
  if (isEnterKey(key)) {
19
32
  const answer = value || defaultValue;
20
33
  setStatus('loading');
21
- const isValid = required && !answer ? 'You must provide a value' : await validate(answer);
34
+ const isValid = await validate(answer);
22
35
  if (isValid === true) {
23
36
  setValue(answer);
24
37
  setStatus('done');
@@ -33,7 +46,7 @@ export default createPrompt((config, done) => {
33
46
  // get cleared, forcing the user to re-enter the value instead of fixing it.
34
47
  rl.write(value);
35
48
  }
36
- setError(isValid || 'You must provide a valid value');
49
+ setError(isValid);
37
50
  setStatus('idle');
38
51
  }
39
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/input",
3
- "version": "4.2.5",
3
+ "version": "4.3.0",
4
4
  "description": "Inquirer input text prompt",
5
5
  "keywords": [
6
6
  "answer",
@@ -74,13 +74,14 @@
74
74
  "tsc": "tshy"
75
75
  },
76
76
  "dependencies": {
77
- "@inquirer/core": "^10.3.0",
78
- "@inquirer/type": "^3.0.9"
77
+ "@inquirer/core": "^10.3.1",
78
+ "@inquirer/type": "^3.0.10"
79
79
  },
80
80
  "devDependencies": {
81
81
  "@arethetypeswrong/cli": "^0.18.2",
82
- "@inquirer/testing": "^2.1.51",
83
- "tshy": "^3.0.2"
82
+ "@inquirer/testing": "^2.1.52",
83
+ "@repo/tsconfig": "0.0.0",
84
+ "tshy": "^3.0.3"
84
85
  },
85
86
  "engines": {
86
87
  "node": ">=18"
@@ -105,5 +106,5 @@
105
106
  "optional": true
106
107
  }
107
108
  },
108
- "gitHead": "87cb01e67a25983bdaf0d74a7685915c0afb5f23"
109
+ "gitHead": "6881993e517e76fa891b72e1f5086fd11f7676ac"
109
110
  }