@inquirer/demo 1.1.26 → 2.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/dist/demos/checkbox.d.ts +2 -0
- package/dist/demos/checkbox.js +60 -0
- package/dist/demos/confirm.d.ts +2 -0
- package/dist/demos/confirm.js +24 -0
- package/dist/demos/editor.d.ts +2 -0
- package/dist/demos/editor.js +25 -0
- package/dist/demos/expand.d.ts +2 -0
- package/dist/demos/expand.js +92 -0
- package/dist/demos/input.d.ts +2 -0
- package/dist/demos/input.js +36 -0
- package/dist/demos/loader.d.ts +2 -0
- package/dist/demos/loader.js +21 -0
- package/dist/demos/number.d.ts +2 -0
- package/dist/demos/number.js +23 -0
- package/dist/demos/password.d.ts +2 -0
- package/dist/demos/password.js +18 -0
- package/dist/demos/rawlist.d.ts +2 -0
- package/dist/demos/rawlist.js +62 -0
- package/dist/demos/search.d.ts +2 -0
- package/dist/demos/search.js +71 -0
- package/dist/demos/select.d.ts +2 -0
- package/dist/demos/select.js +98 -0
- package/dist/demos/timeout.d.ts +2 -0
- package/dist/demos/timeout.js +18 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +85 -0
- package/package.json +19 -28
- package/src/index.ts +95 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as url from 'node:url';
|
|
2
|
+
import { checkbox, Separator } from '@inquirer/prompts';
|
|
3
|
+
const demo = async () => {
|
|
4
|
+
let answer;
|
|
5
|
+
answer = await checkbox({
|
|
6
|
+
message: 'Select a package manager',
|
|
7
|
+
choices: [
|
|
8
|
+
{ name: 'npm', value: 'npm' },
|
|
9
|
+
{ name: 'yarn', value: 'yarn' },
|
|
10
|
+
new Separator(),
|
|
11
|
+
{ name: 'jspm', value: 'jspm', disabled: true },
|
|
12
|
+
{
|
|
13
|
+
name: 'pnpm',
|
|
14
|
+
value: 'pnpm',
|
|
15
|
+
disabled: '(pnpm is not available)',
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
});
|
|
19
|
+
console.log('Answer:', answer);
|
|
20
|
+
answer = await checkbox({
|
|
21
|
+
message: 'Select your favorite letters',
|
|
22
|
+
choices: [
|
|
23
|
+
new Separator('== Alphabet (choices cycle as you scroll through) =='),
|
|
24
|
+
{ value: 'A', checked: true },
|
|
25
|
+
{ value: 'B' },
|
|
26
|
+
{ value: 'C', checked: true },
|
|
27
|
+
{ value: 'D' },
|
|
28
|
+
{ value: 'E' },
|
|
29
|
+
{ value: 'F' },
|
|
30
|
+
{ value: 'G' },
|
|
31
|
+
{ value: 'H' },
|
|
32
|
+
{ value: 'I' },
|
|
33
|
+
{ value: 'J' },
|
|
34
|
+
{ value: 'K' },
|
|
35
|
+
{ value: 'L' },
|
|
36
|
+
{ value: 'M' },
|
|
37
|
+
{ value: 'N' },
|
|
38
|
+
{ value: 'O' },
|
|
39
|
+
{ value: 'P' },
|
|
40
|
+
{ value: 'Q' },
|
|
41
|
+
{ value: 'R' },
|
|
42
|
+
{ value: 'S' },
|
|
43
|
+
{ value: 'T' },
|
|
44
|
+
{ value: 'U' },
|
|
45
|
+
{ value: 'V' },
|
|
46
|
+
{ value: 'W' },
|
|
47
|
+
{ value: 'X' },
|
|
48
|
+
{ value: 'Y' },
|
|
49
|
+
{ value: 'Z' },
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
console.log('Answer:', answer);
|
|
53
|
+
};
|
|
54
|
+
if (import.meta.url.startsWith('file:')) {
|
|
55
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
56
|
+
if (process.argv[1] === modulePath) {
|
|
57
|
+
await demo();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export default demo;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as url from 'node:url';
|
|
2
|
+
import { confirm } from '@inquirer/prompts';
|
|
3
|
+
const demo = async () => {
|
|
4
|
+
console.log('Answer:', await confirm({
|
|
5
|
+
message: 'Confirm?',
|
|
6
|
+
}));
|
|
7
|
+
console.log('Answer:', await confirm({
|
|
8
|
+
message: 'Confirm with default to no?',
|
|
9
|
+
default: false,
|
|
10
|
+
}));
|
|
11
|
+
console.log('Answer:', await confirm({
|
|
12
|
+
message: 'Confirm with your custom transformer function?',
|
|
13
|
+
transformer: (answer) => (answer ? '👍' : '👎'),
|
|
14
|
+
}));
|
|
15
|
+
console.log('This next prompt will be cleared on exit');
|
|
16
|
+
console.log('Cleared prompt answer:', await confirm({ message: 'Confirm?' }, { clearPromptOnDone: true }));
|
|
17
|
+
};
|
|
18
|
+
if (import.meta.url.startsWith('file:')) {
|
|
19
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
20
|
+
if (process.argv[1] === modulePath) {
|
|
21
|
+
await demo();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export default demo;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as url from 'node:url';
|
|
2
|
+
import { editor } from '@inquirer/prompts';
|
|
3
|
+
const demo = async () => {
|
|
4
|
+
console.log('Answer:', await editor({
|
|
5
|
+
message: 'Please write a short bio of at least 3 lines.',
|
|
6
|
+
validate(text) {
|
|
7
|
+
if (text.trim().split('\n').length < 3) {
|
|
8
|
+
return 'Must be at least 3 lines.';
|
|
9
|
+
}
|
|
10
|
+
return true;
|
|
11
|
+
},
|
|
12
|
+
}));
|
|
13
|
+
console.log('Answer:', await editor({
|
|
14
|
+
message: 'Automatically opened editor',
|
|
15
|
+
default: '# This prompt was automatically opened. You can write anything:\n\n',
|
|
16
|
+
waitForUserInput: false,
|
|
17
|
+
}));
|
|
18
|
+
};
|
|
19
|
+
if (import.meta.url.startsWith('file:')) {
|
|
20
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
21
|
+
if (process.argv[1] === modulePath) {
|
|
22
|
+
await demo();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export default demo;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import * as url from 'node:url';
|
|
2
|
+
import { expand } from '@inquirer/prompts';
|
|
3
|
+
const demo = async () => {
|
|
4
|
+
let answer;
|
|
5
|
+
answer = await expand({
|
|
6
|
+
message: 'Conflict on `file.js`:',
|
|
7
|
+
choices: [
|
|
8
|
+
{
|
|
9
|
+
key: 'y',
|
|
10
|
+
name: 'Overwrite',
|
|
11
|
+
value: 'overwrite',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
key: 'a',
|
|
15
|
+
name: 'Overwrite this one and all next',
|
|
16
|
+
value: 'overwrite_all',
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
key: 'd',
|
|
20
|
+
name: 'Show diff',
|
|
21
|
+
value: 'diff',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
key: 'x',
|
|
25
|
+
name: 'Abort',
|
|
26
|
+
value: 'abort',
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
});
|
|
30
|
+
console.log('Answer:', answer);
|
|
31
|
+
answer = await expand({
|
|
32
|
+
message: '(With default) Conflict on `file.js`:',
|
|
33
|
+
default: 'y',
|
|
34
|
+
choices: [
|
|
35
|
+
{
|
|
36
|
+
key: 'y',
|
|
37
|
+
name: 'Overwrite',
|
|
38
|
+
value: 'overwrite',
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
key: 'a',
|
|
42
|
+
name: 'Overwrite this one and all next',
|
|
43
|
+
value: 'overwrite_all',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
key: 'd',
|
|
47
|
+
name: 'Show diff',
|
|
48
|
+
value: 'diff',
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
key: 'x',
|
|
52
|
+
name: 'Abort',
|
|
53
|
+
value: 'abort',
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
});
|
|
57
|
+
console.log('Answer:', answer);
|
|
58
|
+
answer = await expand({
|
|
59
|
+
expanded: true,
|
|
60
|
+
message: '(Auto-expand) Conflict on `file.js`:',
|
|
61
|
+
choices: [
|
|
62
|
+
{
|
|
63
|
+
key: 'y',
|
|
64
|
+
name: 'Overwrite',
|
|
65
|
+
value: 'overwrite',
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
key: 'a',
|
|
69
|
+
name: 'Overwrite this one and all next',
|
|
70
|
+
value: 'overwrite_all',
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
key: 'd',
|
|
74
|
+
name: 'Show diff',
|
|
75
|
+
value: 'diff',
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
key: 'x',
|
|
79
|
+
name: 'Abort',
|
|
80
|
+
value: 'abort',
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
});
|
|
84
|
+
console.log('Answer:', answer);
|
|
85
|
+
};
|
|
86
|
+
if (import.meta.url.startsWith('file:')) {
|
|
87
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
88
|
+
if (process.argv[1] === modulePath) {
|
|
89
|
+
await demo();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export default demo;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as url from 'node:url';
|
|
2
|
+
import { styleText } from 'node:util';
|
|
3
|
+
import { input } from '@inquirer/prompts';
|
|
4
|
+
const hexRegEx = /(\d|[a-f])/gim;
|
|
5
|
+
const isHex = (value) => (value.match(hexRegEx) || []).length === value.length &&
|
|
6
|
+
(value.length === 3 || value.length === 6);
|
|
7
|
+
const demo = async () => {
|
|
8
|
+
let answer;
|
|
9
|
+
answer = await input({
|
|
10
|
+
message: "What's your favorite food?",
|
|
11
|
+
default: 'Croissant',
|
|
12
|
+
});
|
|
13
|
+
console.log('Answer:', answer);
|
|
14
|
+
answer = await input({
|
|
15
|
+
message: 'Enter an hex color?',
|
|
16
|
+
transformer(value = '', { isFinal }) {
|
|
17
|
+
return isFinal ? styleText('underline', value) : value;
|
|
18
|
+
},
|
|
19
|
+
validate: (value = '') => isHex(value) || 'Pass a valid hex value',
|
|
20
|
+
});
|
|
21
|
+
console.log('Answer:', answer);
|
|
22
|
+
answer = await input({
|
|
23
|
+
message: '(Slow validation) provide a number:',
|
|
24
|
+
validate: (value) => new Promise((resolve) => {
|
|
25
|
+
setTimeout(() => resolve(!Number.isNaN(Number(value)) || 'You must provide a number'), 3000);
|
|
26
|
+
}),
|
|
27
|
+
});
|
|
28
|
+
console.log('Answer:', answer);
|
|
29
|
+
};
|
|
30
|
+
if (import.meta.url.startsWith('file:')) {
|
|
31
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
32
|
+
if (process.argv[1] === modulePath) {
|
|
33
|
+
await demo();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export default demo;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as url from 'node:url';
|
|
2
|
+
import { createPrompt, useKeypress, usePrefix, isEnterKey } from '@inquirer/core';
|
|
3
|
+
const loader = createPrompt((_config, done) => {
|
|
4
|
+
const prefix = usePrefix({ status: 'loading' });
|
|
5
|
+
useKeypress((key) => {
|
|
6
|
+
if (isEnterKey(key)) {
|
|
7
|
+
done();
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
return `${prefix} Press enter to exit`;
|
|
11
|
+
});
|
|
12
|
+
const demo = async () => {
|
|
13
|
+
await loader({}, { clearPromptOnDone: true });
|
|
14
|
+
};
|
|
15
|
+
if (import.meta.url.startsWith('file:')) {
|
|
16
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
17
|
+
if (process.argv[1] === modulePath) {
|
|
18
|
+
await demo();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export default demo;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as url from 'node:url';
|
|
2
|
+
import { number } from '@inquirer/prompts';
|
|
3
|
+
const demo = async () => {
|
|
4
|
+
console.log('Answer:', await number({
|
|
5
|
+
message: 'Enter your age?',
|
|
6
|
+
}));
|
|
7
|
+
console.log('Answer:', await number({
|
|
8
|
+
message: 'Enter an integer or a decimal number?',
|
|
9
|
+
step: 'any',
|
|
10
|
+
}));
|
|
11
|
+
console.log('Answer:', await number({
|
|
12
|
+
message: 'Enter a number between 5 and 8?',
|
|
13
|
+
min: 5,
|
|
14
|
+
max: 8,
|
|
15
|
+
}));
|
|
16
|
+
};
|
|
17
|
+
if (import.meta.url.startsWith('file:')) {
|
|
18
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
19
|
+
if (process.argv[1] === modulePath) {
|
|
20
|
+
await demo();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export default demo;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as url from 'node:url';
|
|
2
|
+
import { password } from '@inquirer/prompts';
|
|
3
|
+
const demo = async () => {
|
|
4
|
+
console.log('Answer:', await password({
|
|
5
|
+
message: 'Enter a silent password?',
|
|
6
|
+
}));
|
|
7
|
+
console.log('Answer:', await password({
|
|
8
|
+
message: 'Enter a masked password?',
|
|
9
|
+
mask: '*',
|
|
10
|
+
}));
|
|
11
|
+
};
|
|
12
|
+
if (import.meta.url.startsWith('file:')) {
|
|
13
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
14
|
+
if (process.argv[1] === modulePath) {
|
|
15
|
+
await demo();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export default demo;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import * as url from 'node:url';
|
|
2
|
+
import { rawlist, Separator } from '@inquirer/prompts';
|
|
3
|
+
const demo = async () => {
|
|
4
|
+
let answer;
|
|
5
|
+
answer = await rawlist({
|
|
6
|
+
message: '(no keys) Conflict on `file.js`:',
|
|
7
|
+
choices: [
|
|
8
|
+
{
|
|
9
|
+
name: 'Overwrite',
|
|
10
|
+
value: 'overwrite',
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
name: 'Overwrite this one and all next',
|
|
14
|
+
value: 'overwrite_all',
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: 'Show diff',
|
|
18
|
+
value: 'diff',
|
|
19
|
+
},
|
|
20
|
+
new Separator(),
|
|
21
|
+
{
|
|
22
|
+
name: 'Abort',
|
|
23
|
+
value: 'abort',
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
});
|
|
27
|
+
console.log('Answer:', answer);
|
|
28
|
+
answer = await rawlist({
|
|
29
|
+
message: '(with keys) Conflict on `file.js`:',
|
|
30
|
+
choices: [
|
|
31
|
+
{
|
|
32
|
+
key: 'y',
|
|
33
|
+
name: 'Overwrite',
|
|
34
|
+
value: 'overwrite',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
key: 'a',
|
|
38
|
+
name: 'Overwrite this one and all next',
|
|
39
|
+
value: 'overwrite_all',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
key: 'd',
|
|
43
|
+
name: 'Show diff',
|
|
44
|
+
value: 'diff',
|
|
45
|
+
},
|
|
46
|
+
new Separator(),
|
|
47
|
+
{
|
|
48
|
+
key: 'x',
|
|
49
|
+
name: 'Abort',
|
|
50
|
+
value: 'abort',
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
});
|
|
54
|
+
console.log('Answer:', answer);
|
|
55
|
+
};
|
|
56
|
+
if (import.meta.url.startsWith('file:')) {
|
|
57
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
58
|
+
if (process.argv[1] === modulePath) {
|
|
59
|
+
await demo();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export default demo;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import * as url from 'node:url';
|
|
4
|
+
import { search } from '@inquirer/prompts';
|
|
5
|
+
async function fileExists(filepath) {
|
|
6
|
+
return fs.access(filepath).then(() => true, () => false);
|
|
7
|
+
}
|
|
8
|
+
async function isDirectory(path) {
|
|
9
|
+
if (await fileExists(path)) {
|
|
10
|
+
const stats = await fs.stat(path);
|
|
11
|
+
return stats.isDirectory();
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
const root = path.dirname(path.join(url.fileURLToPath(import.meta.url), '../../..'));
|
|
16
|
+
const demo = async () => {
|
|
17
|
+
let answer;
|
|
18
|
+
// Demo: Search results from an API
|
|
19
|
+
answer = await search({
|
|
20
|
+
message: 'Select an npm package',
|
|
21
|
+
source: async (input = 'inquirer', { signal }) => {
|
|
22
|
+
const response = await fetch(`https://registry.npmjs.org/-/v1/search?text=${encodeURIComponent(input)}&size=20`, { signal });
|
|
23
|
+
const data = (await response.json());
|
|
24
|
+
return data.objects.map((pkg) => ({
|
|
25
|
+
name: pkg.package.name,
|
|
26
|
+
value: pkg.package.name,
|
|
27
|
+
description: pkg.package.description,
|
|
28
|
+
}));
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
console.log('Answer:', answer);
|
|
32
|
+
// Demo: Using the search prompt as an autocomplete tool.
|
|
33
|
+
answer = await search({
|
|
34
|
+
message: 'Select a file',
|
|
35
|
+
source: async (term = '') => {
|
|
36
|
+
let dirPath = path.join(root, term);
|
|
37
|
+
while (!(await isDirectory(dirPath)) && dirPath !== root) {
|
|
38
|
+
dirPath = path.dirname(dirPath);
|
|
39
|
+
}
|
|
40
|
+
const files = await fs.readdir(dirPath, { withFileTypes: true });
|
|
41
|
+
return files
|
|
42
|
+
.toSorted((a, b) => {
|
|
43
|
+
if (a.isDirectory() === b.isDirectory()) {
|
|
44
|
+
return a.name.localeCompare(b.name);
|
|
45
|
+
}
|
|
46
|
+
// Sort dir first
|
|
47
|
+
return a.isDirectory() ? -1 : 1;
|
|
48
|
+
})
|
|
49
|
+
.map((file) => ({
|
|
50
|
+
name: path.relative(root, path.join(dirPath, file.name)) +
|
|
51
|
+
(file.isDirectory() ? '/' : ''),
|
|
52
|
+
value: path.join(file.parentPath, file.name),
|
|
53
|
+
}))
|
|
54
|
+
.filter(({ value }) => value.includes(term));
|
|
55
|
+
},
|
|
56
|
+
validate: async (filePath) => {
|
|
57
|
+
if (!(await fileExists(filePath)) || (await isDirectory(filePath))) {
|
|
58
|
+
return 'You must select a file';
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
console.log('Answer:', answer);
|
|
64
|
+
};
|
|
65
|
+
if (import.meta.url.startsWith('file:')) {
|
|
66
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
67
|
+
if (process.argv[1] === modulePath) {
|
|
68
|
+
await demo();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export default demo;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import * as url from 'node:url';
|
|
2
|
+
import { select, Separator } from '@inquirer/prompts';
|
|
3
|
+
import { styleText } from 'node:util';
|
|
4
|
+
const alphabet = [
|
|
5
|
+
{ value: 'A' },
|
|
6
|
+
{ value: 'B' },
|
|
7
|
+
{ value: 'C' },
|
|
8
|
+
{ value: 'D' },
|
|
9
|
+
{ value: 'E' },
|
|
10
|
+
{ value: 'F' },
|
|
11
|
+
{ value: 'G' },
|
|
12
|
+
{ value: 'H' },
|
|
13
|
+
{ value: 'I' },
|
|
14
|
+
{ value: 'J' },
|
|
15
|
+
{ value: 'K' },
|
|
16
|
+
{ value: 'L' },
|
|
17
|
+
{ value: 'M' },
|
|
18
|
+
{ value: 'N' },
|
|
19
|
+
{ value: 'O', description: 'Letter O, not number 0' },
|
|
20
|
+
{ value: 'P' },
|
|
21
|
+
{ value: 'Q' },
|
|
22
|
+
{ value: 'R' },
|
|
23
|
+
{ value: 'S' },
|
|
24
|
+
{ value: 'T' },
|
|
25
|
+
{ value: 'U' },
|
|
26
|
+
{ value: 'V' },
|
|
27
|
+
{ value: 'W' },
|
|
28
|
+
{ value: 'X' },
|
|
29
|
+
{ value: 'Y' },
|
|
30
|
+
{ value: 'Z' },
|
|
31
|
+
];
|
|
32
|
+
const demo = async () => {
|
|
33
|
+
let answer;
|
|
34
|
+
answer = await select({
|
|
35
|
+
message: 'Select a package manager',
|
|
36
|
+
choices: [
|
|
37
|
+
{
|
|
38
|
+
name: 'npm',
|
|
39
|
+
value: 'npm',
|
|
40
|
+
description: 'npm is the most popular package manager',
|
|
41
|
+
},
|
|
42
|
+
{ name: 'yarn', value: 'yarn', description: 'yarn is an awesome package manager' },
|
|
43
|
+
new Separator(),
|
|
44
|
+
{ name: 'jspm', value: 'jspm', disabled: true },
|
|
45
|
+
{
|
|
46
|
+
name: 'pnpm',
|
|
47
|
+
value: 'pnpm',
|
|
48
|
+
disabled: '(pnpm is not available)',
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
console.log('Answer:', answer);
|
|
53
|
+
answer = await select({
|
|
54
|
+
message: 'Select your favorite letter',
|
|
55
|
+
choices: [
|
|
56
|
+
new Separator('== Alphabet (choices cycle as you scroll through) =='),
|
|
57
|
+
...alphabet,
|
|
58
|
+
],
|
|
59
|
+
});
|
|
60
|
+
console.log('Answer:', answer);
|
|
61
|
+
answer = await select({
|
|
62
|
+
message: 'Select your favorite letter (example without loop)',
|
|
63
|
+
choices: [
|
|
64
|
+
new Separator('== Alphabet (choices cycle as you scroll through) =='),
|
|
65
|
+
...alphabet,
|
|
66
|
+
],
|
|
67
|
+
loop: false,
|
|
68
|
+
});
|
|
69
|
+
console.log('Answer:', answer);
|
|
70
|
+
answer = await select({
|
|
71
|
+
message: 'Select a recipe',
|
|
72
|
+
choices: [
|
|
73
|
+
{
|
|
74
|
+
name: `${styleText('bold', 'Spaghetti Carbonara')}\n Eggs, Pecorino Romano, Pancetta\n 30 minutes`,
|
|
75
|
+
short: 'Spaghetti Carbonara',
|
|
76
|
+
value: 'carbonara',
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: `${styleText('bold', 'Margherita Pizza')}\n Tomatoes, Mozzarella, Basil\n 45 minutes`,
|
|
80
|
+
short: 'Margherita Pizza',
|
|
81
|
+
value: 'pizza',
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: `${styleText('bold', 'Caesar Salad')}\n Romaine, Croutons, Parmesan\n 15 minutes`,
|
|
85
|
+
short: 'Caesar Salad',
|
|
86
|
+
value: 'salad',
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
console.log('Answer:', answer);
|
|
91
|
+
};
|
|
92
|
+
if (import.meta.url.startsWith('file:')) {
|
|
93
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
94
|
+
if (process.argv[1] === modulePath) {
|
|
95
|
+
await demo();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
export default demo;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as url from 'node:url';
|
|
2
|
+
import { input } from '@inquirer/prompts';
|
|
3
|
+
async function demo() {
|
|
4
|
+
const answer = await input({ message: 'Enter a value (timing out in 5 seconds)' }, { signal: AbortSignal.timeout(5000) }).catch((error) => {
|
|
5
|
+
if (error instanceof Error && error.name === 'AbortPromptError') {
|
|
6
|
+
return 'Default value';
|
|
7
|
+
}
|
|
8
|
+
throw error;
|
|
9
|
+
});
|
|
10
|
+
console.log('Answer:', answer);
|
|
11
|
+
}
|
|
12
|
+
if (import.meta.url.startsWith('file:')) {
|
|
13
|
+
const modulePath = url.fileURLToPath(import.meta.url);
|
|
14
|
+
if (process.argv[1] === modulePath) {
|
|
15
|
+
await demo();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export default demo;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { select } from '@inquirer/prompts';
|
|
3
|
+
import { styleText } from 'node:util';
|
|
4
|
+
import figures from '@inquirer/figures';
|
|
5
|
+
import checkboxDemo from "./demos/checkbox.js";
|
|
6
|
+
import confirmDemo from "./demos/confirm.js";
|
|
7
|
+
import editorDemo from "./demos/editor.js";
|
|
8
|
+
import expandDemo from "./demos/expand.js";
|
|
9
|
+
import inputDemo from "./demos/input.js";
|
|
10
|
+
import loaderDemo from "./demos/loader.js";
|
|
11
|
+
import numberDemo from "./demos/number.js";
|
|
12
|
+
import passwordDemo from "./demos/password.js";
|
|
13
|
+
import rawlistDemo from "./demos/rawlist.js";
|
|
14
|
+
import searchDemo from "./demos/search.js";
|
|
15
|
+
import selectDemo from "./demos/select.js";
|
|
16
|
+
import timeoutDemo from "./demos/timeout.js";
|
|
17
|
+
const demos = {
|
|
18
|
+
checkbox: checkboxDemo,
|
|
19
|
+
confirm: confirmDemo,
|
|
20
|
+
editor: editorDemo,
|
|
21
|
+
expand: expandDemo,
|
|
22
|
+
input: inputDemo,
|
|
23
|
+
loader: loaderDemo,
|
|
24
|
+
number: numberDemo,
|
|
25
|
+
password: passwordDemo,
|
|
26
|
+
rawlist: rawlistDemo,
|
|
27
|
+
search: searchDemo,
|
|
28
|
+
select: selectDemo,
|
|
29
|
+
timeout: timeoutDemo,
|
|
30
|
+
};
|
|
31
|
+
async function askNextDemo() {
|
|
32
|
+
let selectedDemo = await select({
|
|
33
|
+
message: 'Which prompt demo do you want to run?',
|
|
34
|
+
choices: [
|
|
35
|
+
{ name: 'Input', value: 'input' },
|
|
36
|
+
{ name: 'Password', value: 'password' },
|
|
37
|
+
{ name: 'Confirm', value: 'confirm' },
|
|
38
|
+
{ name: 'Select', value: 'select' },
|
|
39
|
+
{ name: 'Checkbox', value: 'checkbox' },
|
|
40
|
+
{ name: 'Search', value: 'search' },
|
|
41
|
+
{ name: 'Expand', value: 'expand' },
|
|
42
|
+
{ name: 'Rawlist', value: 'rawlist' },
|
|
43
|
+
{ name: 'Editor', value: 'editor' },
|
|
44
|
+
{ name: 'Number', value: 'number' },
|
|
45
|
+
{ name: 'Advanced demos', value: 'advanced' },
|
|
46
|
+
{ name: "Exit (I'm done)", value: 'exit' },
|
|
47
|
+
],
|
|
48
|
+
theme: {
|
|
49
|
+
prefix: {
|
|
50
|
+
done: styleText('magenta', figures.play),
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
if (selectedDemo === 'advanced') {
|
|
55
|
+
selectedDemo = await select({
|
|
56
|
+
message: 'Which demo do you want to run?',
|
|
57
|
+
choices: [
|
|
58
|
+
{ name: 'Default value after timeout', value: 'timeout' },
|
|
59
|
+
{ name: 'Loader', value: 'loader' },
|
|
60
|
+
{ name: 'Go back', value: 'back' },
|
|
61
|
+
],
|
|
62
|
+
}, {
|
|
63
|
+
clearPromptOnDone: true,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (selectedDemo === 'back') {
|
|
67
|
+
return askNextDemo();
|
|
68
|
+
}
|
|
69
|
+
return selectedDemo;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
let nextDemo = await askNextDemo();
|
|
73
|
+
while (nextDemo !== 'exit') {
|
|
74
|
+
await demos[nextDemo]();
|
|
75
|
+
nextDemo = await askNextDemo();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (error instanceof Error && error.name === 'ExitPromptError') {
|
|
80
|
+
// noop; silence this error
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/demo",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Inquirer demos",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"answer",
|
|
@@ -52,50 +52,41 @@
|
|
|
52
52
|
"type": "module",
|
|
53
53
|
"exports": {
|
|
54
54
|
"./package.json": "./package.json",
|
|
55
|
-
".":
|
|
56
|
-
"import": {
|
|
57
|
-
"types": "./dist/esm/index.d.ts",
|
|
58
|
-
"default": "./dist/esm/index.js"
|
|
59
|
-
}
|
|
60
|
-
}
|
|
55
|
+
".": "./src/index.ts"
|
|
61
56
|
},
|
|
62
|
-
"module": "./dist/esm/index.js",
|
|
63
57
|
"bin": {
|
|
64
|
-
"inquirer-demo": "./
|
|
58
|
+
"inquirer-demo": "./src/index.ts"
|
|
65
59
|
},
|
|
66
60
|
"files": [
|
|
67
61
|
"dist"
|
|
68
62
|
],
|
|
69
63
|
"scripts": {
|
|
70
|
-
"tsc": "
|
|
64
|
+
"tsc": "tsc && chmod +x ./dist/index.js"
|
|
71
65
|
},
|
|
72
66
|
"dependencies": {
|
|
73
|
-
"@inquirer/core": "^
|
|
74
|
-
"@inquirer/figures": "^
|
|
75
|
-
"@inquirer/prompts": "^
|
|
76
|
-
"yoctocolors-cjs": "^2.1.3"
|
|
67
|
+
"@inquirer/core": "^11.0.0",
|
|
68
|
+
"@inquirer/figures": "^2.0.0",
|
|
69
|
+
"@inquirer/prompts": "^8.0.0"
|
|
77
70
|
},
|
|
78
71
|
"devDependencies": {
|
|
79
72
|
"@repo/tsconfig": "0.0.0",
|
|
80
|
-
"@types/node": "^24.10.
|
|
81
|
-
"
|
|
73
|
+
"@types/node": "^24.10.1",
|
|
74
|
+
"typescript": "^5.9.3"
|
|
82
75
|
},
|
|
83
76
|
"engines": {
|
|
84
|
-
"node": ">=
|
|
77
|
+
"node": ">=23.5.0 || ^22.13.0 || ^21.7.0 || ^20.12.0"
|
|
85
78
|
},
|
|
86
79
|
"publishConfig": {
|
|
87
|
-
"access": "public"
|
|
88
|
-
},
|
|
89
|
-
"tshy": {
|
|
90
|
-
"dialects": [
|
|
91
|
-
"esm"
|
|
92
|
-
],
|
|
93
|
-
"exclude": [
|
|
94
|
-
"src/**/*.test.ts"
|
|
95
|
-
],
|
|
80
|
+
"access": "public",
|
|
96
81
|
"exports": {
|
|
97
82
|
"./package.json": "./package.json",
|
|
98
|
-
".":
|
|
83
|
+
".": {
|
|
84
|
+
"types": "./dist/index.d.ts",
|
|
85
|
+
"default": "./dist/index.js"
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"bin": {
|
|
89
|
+
"inquirer-demo": "./dist/index.js"
|
|
99
90
|
}
|
|
100
91
|
},
|
|
101
92
|
"peerDependencies": {
|
|
@@ -106,5 +97,5 @@
|
|
|
106
97
|
"optional": true
|
|
107
98
|
}
|
|
108
99
|
},
|
|
109
|
-
"gitHead": "
|
|
100
|
+
"gitHead": "676685d33374a30340c1b9f0831c7eae2b2357dd"
|
|
110
101
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { select } from '@inquirer/prompts';
|
|
4
|
+
import { styleText } from 'node:util';
|
|
5
|
+
import figures from '@inquirer/figures';
|
|
6
|
+
import checkboxDemo from './demos/checkbox.ts';
|
|
7
|
+
import confirmDemo from './demos/confirm.ts';
|
|
8
|
+
import editorDemo from './demos/editor.ts';
|
|
9
|
+
import expandDemo from './demos/expand.ts';
|
|
10
|
+
import inputDemo from './demos/input.ts';
|
|
11
|
+
import loaderDemo from './demos/loader.ts';
|
|
12
|
+
import numberDemo from './demos/number.ts';
|
|
13
|
+
import passwordDemo from './demos/password.ts';
|
|
14
|
+
import rawlistDemo from './demos/rawlist.ts';
|
|
15
|
+
import searchDemo from './demos/search.ts';
|
|
16
|
+
import selectDemo from './demos/select.ts';
|
|
17
|
+
import timeoutDemo from './demos/timeout.ts';
|
|
18
|
+
|
|
19
|
+
const demos = {
|
|
20
|
+
checkbox: checkboxDemo,
|
|
21
|
+
confirm: confirmDemo,
|
|
22
|
+
editor: editorDemo,
|
|
23
|
+
expand: expandDemo,
|
|
24
|
+
input: inputDemo,
|
|
25
|
+
loader: loaderDemo,
|
|
26
|
+
number: numberDemo,
|
|
27
|
+
password: passwordDemo,
|
|
28
|
+
rawlist: rawlistDemo,
|
|
29
|
+
search: searchDemo,
|
|
30
|
+
select: selectDemo,
|
|
31
|
+
timeout: timeoutDemo,
|
|
32
|
+
} as const;
|
|
33
|
+
|
|
34
|
+
type Demos = keyof typeof demos | 'advanced' | 'back' | 'exit';
|
|
35
|
+
|
|
36
|
+
async function askNextDemo() {
|
|
37
|
+
let selectedDemo: Demos = await select({
|
|
38
|
+
message: 'Which prompt demo do you want to run?',
|
|
39
|
+
choices: [
|
|
40
|
+
{ name: 'Input', value: 'input' },
|
|
41
|
+
{ name: 'Password', value: 'password' },
|
|
42
|
+
{ name: 'Confirm', value: 'confirm' },
|
|
43
|
+
{ name: 'Select', value: 'select' },
|
|
44
|
+
{ name: 'Checkbox', value: 'checkbox' },
|
|
45
|
+
{ name: 'Search', value: 'search' },
|
|
46
|
+
{ name: 'Expand', value: 'expand' },
|
|
47
|
+
{ name: 'Rawlist', value: 'rawlist' },
|
|
48
|
+
{ name: 'Editor', value: 'editor' },
|
|
49
|
+
{ name: 'Number', value: 'number' },
|
|
50
|
+
{ name: 'Advanced demos', value: 'advanced' },
|
|
51
|
+
{ name: "Exit (I'm done)", value: 'exit' },
|
|
52
|
+
],
|
|
53
|
+
theme: {
|
|
54
|
+
prefix: {
|
|
55
|
+
done: styleText('magenta', figures.play),
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
if (selectedDemo === 'advanced') {
|
|
61
|
+
selectedDemo = await select(
|
|
62
|
+
{
|
|
63
|
+
message: 'Which demo do you want to run?',
|
|
64
|
+
choices: [
|
|
65
|
+
{ name: 'Default value after timeout', value: 'timeout' },
|
|
66
|
+
{ name: 'Loader', value: 'loader' },
|
|
67
|
+
{ name: 'Go back', value: 'back' },
|
|
68
|
+
],
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
clearPromptOnDone: true,
|
|
72
|
+
},
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (selectedDemo === 'back') {
|
|
77
|
+
return askNextDemo();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return selectedDemo;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
let nextDemo = await askNextDemo();
|
|
85
|
+
while (nextDemo !== 'exit') {
|
|
86
|
+
await demos[nextDemo]();
|
|
87
|
+
nextDemo = await askNextDemo();
|
|
88
|
+
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (error instanceof Error && error.name === 'ExitPromptError') {
|
|
91
|
+
// noop; silence this error
|
|
92
|
+
} else {
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|