inquirerer 2.1.0 → 2.1.1
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/LICENSE +1 -1
- package/README.md +637 -69
- package/commander.d.ts +0 -1
- package/esm/prompt.js +38 -38
- package/esm/utils.js +3 -3
- package/keypress.d.ts +0 -1
- package/package.js +1 -2
- package/package.json +13 -10
- package/prompt.d.ts +0 -1
- package/prompt.js +40 -40
- package/utils.js +5 -9
package/README.md
CHANGED
|
@@ -1,163 +1,731 @@
|
|
|
1
1
|
# inquirerer
|
|
2
2
|
|
|
3
3
|
<p align="center" width="100%">
|
|
4
|
-
<img height="90" src="https://
|
|
4
|
+
<img height="90" src="https://raw.githubusercontent.com/hyperweb-io/dev-utils/refs/heads/main/docs/img/inquirerer.svg" />
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
7
|
<p align="center" width="100%">
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
<img height="20" src="https://github.com/pyramation/inquirerer/actions/workflows/run-tests.yml/badge.svg" />
|
|
8
|
+
<a href="https://github.com/hyperweb-io/dev-utils/actions/workflows/ci.yml">
|
|
9
|
+
<img height="20" src="https://github.com/hyperweb-io/dev-utils/actions/workflows/ci.yml/badge.svg" />
|
|
11
10
|
</a>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
<a href="https://github.com/hyperweb-io/dev-utils/blob/main/LICENSE">
|
|
12
|
+
<img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/>
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://www.npmjs.com/package/inquirerer"><img height="20" src="https://img.shields.io/npm/dt/inquirerer"></a>
|
|
15
|
+
<a href="https://www.npmjs.com/package/inquirerer"><img height="20" src="https://img.shields.io/github/package-json/v/hyperweb-io/dev-utils?filename=packages%2Finquirerer%2Fpackage.json"></a>
|
|
15
16
|
</p>
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
A powerful, TypeScript-first library for building beautiful command-line interfaces. Create interactive CLI tools with ease using intuitive prompts, validation, and rich user experiences.
|
|
18
19
|
|
|
19
|
-
##
|
|
20
|
+
## Installation
|
|
20
21
|
|
|
21
|
-
```
|
|
22
|
+
```bash
|
|
22
23
|
npm install inquirerer
|
|
23
24
|
```
|
|
24
25
|
|
|
25
26
|
## Features
|
|
26
27
|
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
-
|
|
30
|
-
-
|
|
28
|
+
- 🔌 **CLI Builder** - Build command-line utilties fast
|
|
29
|
+
- 🖊 **Multiple Question Types** - Support for text, autocomplete, checkbox, and confirm questions
|
|
30
|
+
- 🤖 **Non-Interactive Mode** - Fallback to defaults for CI/CD environments, great for testing
|
|
31
|
+
- ✅ **Smart Validation** - Built-in pattern matching, custom validators, and sanitizers
|
|
32
|
+
- 🔀 **Conditional Logic** - Show/hide questions based on previous answers
|
|
33
|
+
- 🎨 **Interactive UX** - Fuzzy search, keyboard navigation, and visual feedback
|
|
31
34
|
|
|
32
35
|
## Table of Contents
|
|
33
36
|
|
|
34
|
-
- [
|
|
35
|
-
|
|
36
|
-
- [
|
|
37
|
-
- [
|
|
38
|
-
- [
|
|
39
|
-
|
|
37
|
+
- [Quick Start](#quick-start)
|
|
38
|
+
- [Core Concepts](#core-concepts)
|
|
39
|
+
- [TypeScript Support](#typescript-support)
|
|
40
|
+
- [Question Types](#question-types)
|
|
41
|
+
- [Non-Interactive Mode](#non-interactive-mode)
|
|
42
|
+
- [API Reference](#api-reference)
|
|
43
|
+
- [Inquirerer Class](#inquirerer-class)
|
|
44
|
+
- [Question Types](#question-types-1)
|
|
40
45
|
- [Text Question](#text-question)
|
|
46
|
+
- [Number Question](#number-question)
|
|
41
47
|
- [Confirm Question](#confirm-question)
|
|
48
|
+
- [List Question](#list-question)
|
|
42
49
|
- [Autocomplete Question](#autocomplete-question)
|
|
43
50
|
- [Checkbox Question](#checkbox-question)
|
|
44
|
-
- [
|
|
51
|
+
- [Advanced Question Options](#advanced-question-options)
|
|
52
|
+
- [Real-World Examples](#real-world-examples)
|
|
53
|
+
- [Project Setup Wizard](#project-setup-wizard)
|
|
54
|
+
- [Configuration Builder](#configuration-builder)
|
|
55
|
+
- [CLI with Commander Integration](#cli-with-commander-integration)
|
|
56
|
+
- [Dynamic Dependencies](#dynamic-dependencies)
|
|
57
|
+
- [Custom Validation](#custom-validation)
|
|
58
|
+
- [CLI Helper](#cli-helper)
|
|
45
59
|
- [Developing](#developing)
|
|
46
|
-
- [Credits](#credits)
|
|
47
60
|
- [Disclaimer](#disclaimer)
|
|
48
61
|
|
|
49
|
-
##
|
|
62
|
+
## Quick Start
|
|
50
63
|
|
|
51
|
-
|
|
64
|
+
```typescript
|
|
65
|
+
import { Inquirerer } from 'inquirerer';
|
|
52
66
|
|
|
53
|
-
|
|
54
|
-
|
|
67
|
+
const prompter = new Inquirerer();
|
|
68
|
+
|
|
69
|
+
const answers = await prompter.prompt({}, [
|
|
70
|
+
{
|
|
71
|
+
type: 'text',
|
|
72
|
+
name: 'username',
|
|
73
|
+
message: 'What is your username?',
|
|
74
|
+
required: true
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
type: 'confirm',
|
|
78
|
+
name: 'newsletter',
|
|
79
|
+
message: 'Subscribe to our newsletter?',
|
|
80
|
+
default: true
|
|
81
|
+
}
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
console.log(answers);
|
|
85
|
+
// { username: 'john_doe', newsletter: true }
|
|
55
86
|
```
|
|
56
87
|
|
|
57
|
-
##
|
|
88
|
+
## Core Concepts
|
|
89
|
+
|
|
90
|
+
### TypeScript Support
|
|
91
|
+
|
|
92
|
+
Import types for full type safety:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import {
|
|
96
|
+
Inquirerer,
|
|
97
|
+
Question,
|
|
98
|
+
TextQuestion,
|
|
99
|
+
NumberQuestion,
|
|
100
|
+
ConfirmQuestion,
|
|
101
|
+
ListQuestion,
|
|
102
|
+
AutocompleteQuestion,
|
|
103
|
+
CheckboxQuestion,
|
|
104
|
+
InquirererOptions
|
|
105
|
+
} from 'inquirerer';
|
|
106
|
+
|
|
107
|
+
interface UserConfig {
|
|
108
|
+
name: string;
|
|
109
|
+
age: number;
|
|
110
|
+
newsletter: boolean;
|
|
111
|
+
}
|
|
58
112
|
|
|
59
|
-
|
|
113
|
+
const answers = await prompter.prompt<UserConfig>({}, questions);
|
|
114
|
+
// answers is typed as UserConfig
|
|
115
|
+
```
|
|
60
116
|
|
|
61
|
-
|
|
62
|
-
|
|
117
|
+
### Question Types
|
|
118
|
+
|
|
119
|
+
All questions support these base properties:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
interface BaseQuestion {
|
|
123
|
+
name: string; // Property name in result object
|
|
124
|
+
type: string; // Question type
|
|
125
|
+
message?: string; // Prompt message to display
|
|
126
|
+
description?: string; // Additional context
|
|
127
|
+
default?: any; // Default value
|
|
128
|
+
useDefault?: boolean; // Skip prompt and use default
|
|
129
|
+
required?: boolean; // Validation requirement
|
|
130
|
+
validate?: (input: any, answers: any) => boolean | Validation;
|
|
131
|
+
sanitize?: (input: any, answers: any) => any;
|
|
132
|
+
pattern?: string; // Regex pattern for validation
|
|
133
|
+
dependsOn?: string[]; // Question dependencies
|
|
134
|
+
when?: (answers: any) => boolean; // Conditional display
|
|
135
|
+
}
|
|
63
136
|
```
|
|
64
137
|
|
|
65
|
-
###
|
|
138
|
+
### Non-Interactive Mode
|
|
139
|
+
|
|
140
|
+
When running in CI/CD or without a TTY, inquirerer automatically falls back to default values:
|
|
66
141
|
|
|
67
|
-
|
|
142
|
+
```typescript
|
|
143
|
+
const prompter = new Inquirerer({
|
|
144
|
+
noTty: true, // Force non-interactive mode
|
|
145
|
+
useDefaults: true // Use defaults without prompting
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## API Reference
|
|
150
|
+
|
|
151
|
+
### Inquirerer Class
|
|
152
|
+
|
|
153
|
+
#### Constructor Options
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
interface InquirererOptions {
|
|
157
|
+
noTty?: boolean; // Disable interactive mode
|
|
158
|
+
input?: Readable; // Input stream (default: process.stdin)
|
|
159
|
+
output?: Writable; // Output stream (default: process.stdout)
|
|
160
|
+
useDefaults?: boolean; // Skip prompts and use defaults
|
|
161
|
+
globalMaxLines?: number; // Max lines for list displays (default: 10)
|
|
162
|
+
mutateArgs?: boolean; // Mutate argv object (default: true)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const prompter = new Inquirerer(options);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### Methods
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
// Main prompt method
|
|
172
|
+
prompt<T>(argv: T, questions: Question[], options?: PromptOptions): Promise<T>
|
|
173
|
+
|
|
174
|
+
// Generate man page documentation
|
|
175
|
+
generateManPage(info: ManPageInfo): string
|
|
176
|
+
|
|
177
|
+
// Clean up resources
|
|
178
|
+
close(): void
|
|
179
|
+
exit(): void
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Question Types
|
|
68
183
|
|
|
69
184
|
#### Text Question
|
|
70
185
|
|
|
71
|
-
|
|
186
|
+
Collect string input from users.
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
72
189
|
{
|
|
73
190
|
type: 'text',
|
|
74
|
-
name: '
|
|
75
|
-
message: '
|
|
191
|
+
name: 'projectName',
|
|
192
|
+
message: 'What is your project name?',
|
|
193
|
+
default: 'my-app',
|
|
194
|
+
required: true,
|
|
195
|
+
pattern: '^[a-z0-9-]+$', // Regex validation
|
|
196
|
+
validate: (input) => {
|
|
197
|
+
if (input.length < 3) {
|
|
198
|
+
return { success: false, reason: 'Name must be at least 3 characters' };
|
|
199
|
+
}
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
#### Number Question
|
|
206
|
+
|
|
207
|
+
Collect numeric input.
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
{
|
|
211
|
+
type: 'number',
|
|
212
|
+
name: 'port',
|
|
213
|
+
message: 'Which port to use?',
|
|
214
|
+
default: 3000,
|
|
215
|
+
validate: (input) => {
|
|
216
|
+
if (input < 1 || input > 65535) {
|
|
217
|
+
return { success: false, reason: 'Port must be between 1 and 65535' };
|
|
218
|
+
}
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
76
221
|
}
|
|
77
222
|
```
|
|
78
223
|
|
|
79
224
|
#### Confirm Question
|
|
80
225
|
|
|
81
|
-
|
|
226
|
+
Yes/no questions.
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
82
229
|
{
|
|
83
230
|
type: 'confirm',
|
|
84
|
-
name: '
|
|
85
|
-
message: '
|
|
86
|
-
default: true
|
|
231
|
+
name: 'useTypeScript',
|
|
232
|
+
message: 'Use TypeScript?',
|
|
233
|
+
default: true // Default to 'yes'
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
#### List Question
|
|
238
|
+
|
|
239
|
+
Select one option from a list (no search).
|
|
240
|
+
|
|
241
|
+
```typescript
|
|
242
|
+
{
|
|
243
|
+
type: 'list',
|
|
244
|
+
name: 'license',
|
|
245
|
+
message: 'Choose a license',
|
|
246
|
+
options: ['MIT', 'Apache-2.0', 'GPL-3.0', 'BSD-3-Clause'],
|
|
247
|
+
default: 'MIT',
|
|
248
|
+
maxDisplayLines: 5
|
|
87
249
|
}
|
|
88
250
|
```
|
|
89
251
|
|
|
90
252
|
#### Autocomplete Question
|
|
91
253
|
|
|
92
|
-
|
|
254
|
+
Select with fuzzy search capabilities.
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
93
257
|
{
|
|
94
258
|
type: 'autocomplete',
|
|
95
|
-
name: '
|
|
96
|
-
message: '
|
|
259
|
+
name: 'framework',
|
|
260
|
+
message: 'Choose your framework',
|
|
97
261
|
options: [
|
|
98
|
-
'
|
|
262
|
+
{ name: 'React', value: 'react' },
|
|
263
|
+
{ name: 'Vue.js', value: 'vue' },
|
|
264
|
+
{ name: 'Angular', value: 'angular' },
|
|
265
|
+
{ name: 'Svelte', value: 'svelte' }
|
|
99
266
|
],
|
|
100
|
-
|
|
267
|
+
allowCustomOptions: true, // Allow user to enter custom value
|
|
268
|
+
maxDisplayLines: 8
|
|
101
269
|
}
|
|
102
270
|
```
|
|
103
271
|
|
|
104
272
|
#### Checkbox Question
|
|
105
273
|
|
|
106
|
-
|
|
274
|
+
Multi-select with search.
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
107
277
|
{
|
|
108
278
|
type: 'checkbox',
|
|
109
|
-
name: '
|
|
110
|
-
message: '
|
|
279
|
+
name: 'features',
|
|
280
|
+
message: 'Select features to include',
|
|
111
281
|
options: [
|
|
112
|
-
'
|
|
282
|
+
'Authentication',
|
|
283
|
+
'Database',
|
|
284
|
+
'API Routes',
|
|
285
|
+
'Testing',
|
|
286
|
+
'Documentation'
|
|
113
287
|
],
|
|
114
|
-
|
|
288
|
+
default: ['Authentication', 'API Routes'],
|
|
289
|
+
returnFullResults: false, // Only return selected items
|
|
115
290
|
required: true
|
|
116
291
|
}
|
|
117
292
|
```
|
|
118
293
|
|
|
119
|
-
|
|
294
|
+
With `returnFullResults: true`, returns all options with selection status:
|
|
295
|
+
|
|
296
|
+
```typescript
|
|
297
|
+
[
|
|
298
|
+
{ name: 'Authentication', value: 'Authentication', selected: true },
|
|
299
|
+
{ name: 'Database', value: 'Database', selected: false },
|
|
300
|
+
// ...
|
|
301
|
+
]
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Advanced Question Options
|
|
305
|
+
|
|
306
|
+
#### Custom Validation
|
|
120
307
|
|
|
121
|
-
|
|
308
|
+
```typescript
|
|
309
|
+
{
|
|
310
|
+
type: 'text',
|
|
311
|
+
name: 'email',
|
|
312
|
+
message: 'Enter your email',
|
|
313
|
+
pattern: '^[^@]+@[^@]+\\.[^@]+$',
|
|
314
|
+
validate: (email, answers) => {
|
|
315
|
+
// Custom async validation possible
|
|
316
|
+
if (email.endsWith('@example.com')) {
|
|
317
|
+
return {
|
|
318
|
+
success: false,
|
|
319
|
+
reason: 'Please use a real email address'
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
return { success: true };
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
```
|
|
122
326
|
|
|
123
|
-
|
|
327
|
+
#### Value Sanitization
|
|
328
|
+
|
|
329
|
+
```typescript
|
|
330
|
+
{
|
|
331
|
+
type: 'text',
|
|
332
|
+
name: 'tags',
|
|
333
|
+
message: 'Enter tags (comma-separated)',
|
|
334
|
+
sanitize: (input) => {
|
|
335
|
+
return input.split(',').map(tag => tag.trim());
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
#### Conditional Questions
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
const questions: Question[] = [
|
|
344
|
+
{
|
|
345
|
+
type: 'confirm',
|
|
346
|
+
name: 'useDatabase',
|
|
347
|
+
message: 'Do you need a database?',
|
|
348
|
+
default: false
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
type: 'list',
|
|
352
|
+
name: 'database',
|
|
353
|
+
message: 'Which database?',
|
|
354
|
+
options: ['PostgreSQL', 'MySQL', 'MongoDB', 'SQLite'],
|
|
355
|
+
when: (answers) => answers.useDatabase === true // Only show if useDatabase is true
|
|
356
|
+
}
|
|
357
|
+
];
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
#### Question Dependencies
|
|
361
|
+
|
|
362
|
+
Ensure questions appear in the correct order:
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
[
|
|
366
|
+
{
|
|
367
|
+
type: 'checkbox',
|
|
368
|
+
name: 'services',
|
|
369
|
+
message: 'Select services',
|
|
370
|
+
options: ['Auth', 'Storage', 'Functions']
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
type: 'text',
|
|
374
|
+
name: 'authProvider',
|
|
375
|
+
message: 'Which auth provider?',
|
|
376
|
+
dependsOn: ['services'], // Wait for services question
|
|
377
|
+
when: (answers) => {
|
|
378
|
+
const selected = answers.services.find(s => s.name === 'Auth');
|
|
379
|
+
return selected?.selected === true;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
]
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
## Real-World Examples
|
|
386
|
+
|
|
387
|
+
### Project Setup Wizard
|
|
388
|
+
|
|
389
|
+
```typescript
|
|
390
|
+
import { Inquirerer, Question } from 'inquirerer';
|
|
391
|
+
import minimist from 'minimist';
|
|
392
|
+
|
|
393
|
+
const argv = minimist(process.argv.slice(2));
|
|
124
394
|
const prompter = new Inquirerer();
|
|
125
395
|
|
|
126
|
-
|
|
127
|
-
|
|
396
|
+
const questions: Question[] = [
|
|
397
|
+
{
|
|
398
|
+
type: 'text',
|
|
399
|
+
name: 'projectName',
|
|
400
|
+
message: 'Project name',
|
|
401
|
+
required: true,
|
|
402
|
+
pattern: '^[a-z0-9-]+$'
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
type: 'text',
|
|
406
|
+
name: 'description',
|
|
407
|
+
message: 'Project description',
|
|
408
|
+
default: 'My awesome project'
|
|
409
|
+
},
|
|
410
|
+
{
|
|
411
|
+
type: 'confirm',
|
|
412
|
+
name: 'typescript',
|
|
413
|
+
message: 'Use TypeScript?',
|
|
414
|
+
default: true
|
|
415
|
+
},
|
|
416
|
+
{
|
|
417
|
+
type: 'autocomplete',
|
|
418
|
+
name: 'framework',
|
|
419
|
+
message: 'Choose a framework',
|
|
420
|
+
options: ['React', 'Vue', 'Svelte', 'None'],
|
|
421
|
+
default: 'React'
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
type: 'checkbox',
|
|
425
|
+
name: 'tools',
|
|
426
|
+
message: 'Additional tools',
|
|
427
|
+
options: ['ESLint', 'Prettier', 'Jest', 'Husky'],
|
|
428
|
+
default: ['ESLint', 'Prettier']
|
|
429
|
+
}
|
|
430
|
+
];
|
|
431
|
+
|
|
432
|
+
const config = await prompter.prompt(argv, questions);
|
|
433
|
+
console.log('Creating project with:', config);
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
Run interactively:
|
|
437
|
+
```bash
|
|
438
|
+
node setup.js
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
Or with CLI args:
|
|
442
|
+
```bash
|
|
443
|
+
node setup.js --projectName=my-app --typescript --framework=React
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
### Configuration Builder
|
|
447
|
+
|
|
448
|
+
```typescript
|
|
449
|
+
interface AppConfig {
|
|
450
|
+
port: number;
|
|
451
|
+
host: string;
|
|
452
|
+
ssl: boolean;
|
|
453
|
+
sslCert?: string;
|
|
454
|
+
sslKey?: string;
|
|
455
|
+
database: string;
|
|
456
|
+
logLevel: string;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const questions: Question[] = [
|
|
460
|
+
{
|
|
461
|
+
type: 'number',
|
|
462
|
+
name: 'port',
|
|
463
|
+
message: 'Server port',
|
|
464
|
+
default: 3000,
|
|
465
|
+
validate: (port) => port > 0 && port < 65536
|
|
466
|
+
},
|
|
467
|
+
{
|
|
468
|
+
type: 'text',
|
|
469
|
+
name: 'host',
|
|
470
|
+
message: 'Server host',
|
|
471
|
+
default: '0.0.0.0'
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
type: 'confirm',
|
|
475
|
+
name: 'ssl',
|
|
476
|
+
message: 'Enable SSL?',
|
|
477
|
+
default: false
|
|
478
|
+
},
|
|
479
|
+
{
|
|
480
|
+
type: 'text',
|
|
481
|
+
name: 'sslCert',
|
|
482
|
+
message: 'SSL certificate path',
|
|
483
|
+
when: (answers) => answers.ssl === true,
|
|
484
|
+
required: true
|
|
485
|
+
},
|
|
486
|
+
{
|
|
487
|
+
type: 'text',
|
|
488
|
+
name: 'sslKey',
|
|
489
|
+
message: 'SSL key path',
|
|
490
|
+
when: (answers) => answers.ssl === true,
|
|
491
|
+
required: true
|
|
492
|
+
},
|
|
493
|
+
{
|
|
494
|
+
type: 'list',
|
|
495
|
+
name: 'database',
|
|
496
|
+
message: 'Database type',
|
|
497
|
+
options: ['PostgreSQL', 'MySQL', 'SQLite'],
|
|
498
|
+
default: 'PostgreSQL'
|
|
499
|
+
},
|
|
500
|
+
{
|
|
501
|
+
type: 'list',
|
|
502
|
+
name: 'logLevel',
|
|
503
|
+
message: 'Log level',
|
|
504
|
+
options: ['error', 'warn', 'info', 'debug'],
|
|
505
|
+
default: 'info'
|
|
506
|
+
}
|
|
507
|
+
];
|
|
508
|
+
|
|
509
|
+
const config = await prompter.prompt<AppConfig>(argv, questions);
|
|
510
|
+
|
|
511
|
+
// Write config to file
|
|
512
|
+
fs.writeFileSync('config.json', JSON.stringify(config, null, 2));
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
### CLI with Commander Integration
|
|
516
|
+
|
|
517
|
+
```typescript
|
|
518
|
+
import { CLI, CommandHandler } from 'inquirerer';
|
|
519
|
+
import { Question } from 'inquirerer';
|
|
520
|
+
|
|
521
|
+
const handler: CommandHandler = async (argv, prompter, options) => {
|
|
522
|
+
const questions: Question[] = [
|
|
128
523
|
{
|
|
129
524
|
type: 'text',
|
|
130
|
-
name: '
|
|
131
|
-
message: '
|
|
525
|
+
name: 'name',
|
|
526
|
+
message: 'What is your name?',
|
|
132
527
|
required: true
|
|
133
528
|
},
|
|
134
529
|
{
|
|
135
|
-
type: '
|
|
136
|
-
name: '
|
|
137
|
-
message: '
|
|
138
|
-
|
|
139
|
-
|
|
530
|
+
type: 'number',
|
|
531
|
+
name: 'age',
|
|
532
|
+
message: 'What is your age?',
|
|
533
|
+
validate: (age) => age >= 0 && age <= 120
|
|
534
|
+
}
|
|
535
|
+
];
|
|
536
|
+
|
|
537
|
+
const answers = await prompter.prompt(argv, questions);
|
|
538
|
+
console.log('Hello,', answers.name);
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
const cli = new CLI(handler, {
|
|
542
|
+
version: 'myapp@1.0.0',
|
|
543
|
+
minimistOpts: {
|
|
544
|
+
alias: {
|
|
545
|
+
n: 'name',
|
|
546
|
+
a: 'age',
|
|
547
|
+
v: 'version'
|
|
140
548
|
}
|
|
141
|
-
|
|
549
|
+
}
|
|
550
|
+
});
|
|
142
551
|
|
|
143
|
-
|
|
144
|
-
|
|
552
|
+
await cli.run();
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
### Dynamic Dependencies
|
|
556
|
+
|
|
557
|
+
```typescript
|
|
558
|
+
const questions: Question[] = [
|
|
559
|
+
{
|
|
560
|
+
type: 'checkbox',
|
|
561
|
+
name: 'cloud',
|
|
562
|
+
message: 'Select cloud services',
|
|
563
|
+
options: ['AWS', 'Azure', 'GCP'],
|
|
564
|
+
returnFullResults: true
|
|
565
|
+
},
|
|
566
|
+
{
|
|
567
|
+
type: 'text',
|
|
568
|
+
name: 'awsRegion',
|
|
569
|
+
message: 'AWS Region',
|
|
570
|
+
dependsOn: ['cloud'],
|
|
571
|
+
when: (answers) => {
|
|
572
|
+
const aws = answers.cloud?.find(c => c.name === 'AWS');
|
|
573
|
+
return aws?.selected === true;
|
|
574
|
+
},
|
|
575
|
+
default: 'us-east-1'
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
type: 'text',
|
|
579
|
+
name: 'azureLocation',
|
|
580
|
+
message: 'Azure Location',
|
|
581
|
+
dependsOn: ['cloud'],
|
|
582
|
+
when: (answers) => {
|
|
583
|
+
const azure = answers.cloud?.find(c => c.name === 'Azure');
|
|
584
|
+
return azure?.selected === true;
|
|
585
|
+
},
|
|
586
|
+
default: 'eastus'
|
|
587
|
+
},
|
|
588
|
+
{
|
|
589
|
+
type: 'text',
|
|
590
|
+
name: 'gcpZone',
|
|
591
|
+
message: 'GCP Zone',
|
|
592
|
+
dependsOn: ['cloud'],
|
|
593
|
+
when: (answers) => {
|
|
594
|
+
const gcp = answers.cloud?.find(c => c.name === 'GCP');
|
|
595
|
+
return gcp?.selected === true;
|
|
596
|
+
},
|
|
597
|
+
default: 'us-central1-a'
|
|
598
|
+
}
|
|
599
|
+
];
|
|
145
600
|
|
|
146
|
-
|
|
601
|
+
const config = await prompter.prompt({}, questions);
|
|
147
602
|
```
|
|
148
603
|
|
|
604
|
+
### Custom Validation
|
|
605
|
+
|
|
606
|
+
```typescript
|
|
607
|
+
const questions: Question[] = [
|
|
608
|
+
{
|
|
609
|
+
type: 'text',
|
|
610
|
+
name: 'username',
|
|
611
|
+
message: 'Choose a username',
|
|
612
|
+
required: true,
|
|
613
|
+
pattern: '^[a-zA-Z0-9_]{3,20}$',
|
|
614
|
+
validate: async (username) => {
|
|
615
|
+
// Simulate API call to check availability
|
|
616
|
+
const available = await checkUsernameAvailability(username);
|
|
617
|
+
if (!available) {
|
|
618
|
+
return {
|
|
619
|
+
success: false,
|
|
620
|
+
reason: 'Username is already taken'
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
return { success: true };
|
|
624
|
+
}
|
|
625
|
+
},
|
|
626
|
+
{
|
|
627
|
+
type: 'text',
|
|
628
|
+
name: 'password',
|
|
629
|
+
message: 'Choose a password',
|
|
630
|
+
required: true,
|
|
631
|
+
validate: (password) => {
|
|
632
|
+
if (password.length < 8) {
|
|
633
|
+
return {
|
|
634
|
+
success: false,
|
|
635
|
+
reason: 'Password must be at least 8 characters'
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
if (!/[A-Z]/.test(password)) {
|
|
639
|
+
return {
|
|
640
|
+
success: false,
|
|
641
|
+
reason: 'Password must contain an uppercase letter'
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
if (!/[0-9]/.test(password)) {
|
|
645
|
+
return {
|
|
646
|
+
success: false,
|
|
647
|
+
reason: 'Password must contain a number'
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
return { success: true };
|
|
651
|
+
}
|
|
652
|
+
},
|
|
653
|
+
{
|
|
654
|
+
type: 'text',
|
|
655
|
+
name: 'confirmPassword',
|
|
656
|
+
message: 'Confirm password',
|
|
657
|
+
required: true,
|
|
658
|
+
dependsOn: ['password'],
|
|
659
|
+
validate: (confirm, answers) => {
|
|
660
|
+
if (confirm !== answers.password) {
|
|
661
|
+
return {
|
|
662
|
+
success: false,
|
|
663
|
+
reason: 'Passwords do not match'
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
return { success: true };
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
];
|
|
670
|
+
```
|
|
149
671
|
|
|
150
|
-
##
|
|
672
|
+
## CLI Helper
|
|
673
|
+
|
|
674
|
+
The `CLI` class provides integration with command-line argument parsing:
|
|
151
675
|
|
|
676
|
+
```typescript
|
|
677
|
+
import { CLI, CommandHandler, CLIOptions } from 'inquirerer';
|
|
678
|
+
|
|
679
|
+
const options: Partial<CLIOptions> = {
|
|
680
|
+
version: 'myapp@1.0.0',
|
|
681
|
+
minimistOpts: {
|
|
682
|
+
alias: {
|
|
683
|
+
v: 'version',
|
|
684
|
+
h: 'help'
|
|
685
|
+
},
|
|
686
|
+
boolean: ['help', 'version'],
|
|
687
|
+
string: ['name', 'output']
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
const handler: CommandHandler = async (argv, prompter) => {
|
|
692
|
+
if (argv.help) {
|
|
693
|
+
console.log('Usage: myapp [options]');
|
|
694
|
+
process.exit(0);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
const answers = await prompter.prompt(argv, questions);
|
|
698
|
+
// Handle answers
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
const cli = new CLI(handler, options);
|
|
702
|
+
await cli.run();
|
|
703
|
+
```
|
|
704
|
+
|
|
705
|
+
## Developing
|
|
152
706
|
|
|
153
707
|
When first cloning the repo:
|
|
708
|
+
|
|
709
|
+
```bash
|
|
710
|
+
npm install
|
|
711
|
+
npm run build
|
|
154
712
|
```
|
|
155
|
-
|
|
156
|
-
|
|
713
|
+
|
|
714
|
+
Run in development mode:
|
|
715
|
+
|
|
716
|
+
```bash
|
|
717
|
+
npm run dev
|
|
718
|
+
```
|
|
719
|
+
|
|
720
|
+
Run tests:
|
|
721
|
+
|
|
722
|
+
```bash
|
|
723
|
+
npm test
|
|
724
|
+
npm run test:watch
|
|
157
725
|
```
|
|
158
726
|
|
|
159
727
|
## Disclaimer
|
|
160
728
|
|
|
161
|
-
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED
|
|
729
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
162
730
|
|
|
163
731
|
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|