@inquirer/expand 0.0.20-alpha.0 → 0.0.23-alpha.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/index.d.ts +15 -0
- package/dist/index.js +84 -0
- package/package.json +14 -6
- package/demo.js +0 -88
- package/index.js +0 -99
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AsyncPromptConfig } from '@inquirer/core';
|
|
2
|
+
declare type ExpandConfig = AsyncPromptConfig & {
|
|
3
|
+
choices: {
|
|
4
|
+
key: string;
|
|
5
|
+
name: string;
|
|
6
|
+
value?: string;
|
|
7
|
+
}[];
|
|
8
|
+
default?: string;
|
|
9
|
+
expanded?: boolean;
|
|
10
|
+
};
|
|
11
|
+
declare const _default: (options: ExpandConfig, stdio?: {
|
|
12
|
+
input?: NodeJS.ReadableStream | undefined;
|
|
13
|
+
output?: NodeJS.WritableStream | undefined;
|
|
14
|
+
} | undefined) => Promise<string>;
|
|
15
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { createPrompt, useState, useKeypress, usePrefix, isEnterKey, } from '@inquirer/core';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
const helpChoice = {
|
|
4
|
+
key: 'h',
|
|
5
|
+
name: 'Help, list all options',
|
|
6
|
+
value: undefined,
|
|
7
|
+
};
|
|
8
|
+
export default createPrompt((config, done) => {
|
|
9
|
+
const { choices, default: defaultKey = 'h', expanded: defaultExpandState = false, } = config;
|
|
10
|
+
const [status, setStatus] = useState('pending');
|
|
11
|
+
const [value, setValue] = useState('');
|
|
12
|
+
const [expanded, setExpanded] = useState(defaultExpandState);
|
|
13
|
+
const [errorMsg, setError] = useState(undefined);
|
|
14
|
+
const prefix = usePrefix();
|
|
15
|
+
useKeypress((key, rl) => {
|
|
16
|
+
if (isEnterKey(key)) {
|
|
17
|
+
const answer = (value || defaultKey).toLowerCase();
|
|
18
|
+
if (answer === 'h' && !expanded) {
|
|
19
|
+
setExpanded(true);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
const selectedChoice = choices.find(({ key }) => key === answer);
|
|
23
|
+
if (selectedChoice) {
|
|
24
|
+
const finalValue = selectedChoice.value || selectedChoice.name;
|
|
25
|
+
setValue(finalValue);
|
|
26
|
+
setStatus('done');
|
|
27
|
+
done(finalValue);
|
|
28
|
+
}
|
|
29
|
+
else if (value === '') {
|
|
30
|
+
setError('Please input a value');
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
setError(`"${chalk.red(value)}" isn't an available option`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
setValue(rl.line);
|
|
39
|
+
setError(undefined);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
const message = chalk.bold(config.message);
|
|
43
|
+
if (status === 'done') {
|
|
44
|
+
return `${prefix} ${message} ${chalk.cyan(value)}`;
|
|
45
|
+
}
|
|
46
|
+
const allChoices = expanded ? choices : [...choices, helpChoice];
|
|
47
|
+
// Collapsed display style
|
|
48
|
+
let longChoices = '';
|
|
49
|
+
let shortChoices = allChoices
|
|
50
|
+
.map((choice) => {
|
|
51
|
+
if (choice.key === defaultKey) {
|
|
52
|
+
return choice.key.toUpperCase();
|
|
53
|
+
}
|
|
54
|
+
return choice.key;
|
|
55
|
+
})
|
|
56
|
+
.join('');
|
|
57
|
+
shortChoices = chalk.dim(` (${shortChoices})`);
|
|
58
|
+
// Expanded display style
|
|
59
|
+
if (expanded) {
|
|
60
|
+
shortChoices = '';
|
|
61
|
+
longChoices = allChoices
|
|
62
|
+
.map((choice) => {
|
|
63
|
+
const line = ` ${choice.key}) ${choice.name || choice.value}`;
|
|
64
|
+
if (choice.key === value.toLowerCase()) {
|
|
65
|
+
return chalk.cyan(line);
|
|
66
|
+
}
|
|
67
|
+
return line;
|
|
68
|
+
})
|
|
69
|
+
.join('\n');
|
|
70
|
+
}
|
|
71
|
+
let helpTip = '';
|
|
72
|
+
const currentOption = allChoices.find(({ key }) => key === value.toLowerCase());
|
|
73
|
+
if (currentOption) {
|
|
74
|
+
helpTip = `${chalk.cyan('>>')} ${currentOption.name || currentOption.value}`;
|
|
75
|
+
}
|
|
76
|
+
let error = '';
|
|
77
|
+
if (errorMsg) {
|
|
78
|
+
error = chalk.red(`> ${errorMsg}`);
|
|
79
|
+
}
|
|
80
|
+
return [
|
|
81
|
+
`${prefix} ${message}${shortChoices} ${value}`,
|
|
82
|
+
[longChoices, helpTip, error].filter(Boolean).join('\n'),
|
|
83
|
+
];
|
|
84
|
+
});
|
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/expand",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.23-alpha.0",
|
|
4
5
|
"description": "Inquirer checkbox prompt",
|
|
5
|
-
"main": "index.js",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"typings": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/"
|
|
10
|
+
],
|
|
6
11
|
"repository": "SBoudrias/Inquirer.js",
|
|
7
12
|
"keywords": [
|
|
8
13
|
"cli",
|
|
@@ -15,12 +20,15 @@
|
|
|
15
20
|
"license": "MIT",
|
|
16
21
|
"homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/expand/README.md",
|
|
17
22
|
"dependencies": {
|
|
18
|
-
"@inquirer/core": "^0.0.
|
|
19
|
-
"chalk": "^
|
|
20
|
-
"figures": "^
|
|
23
|
+
"@inquirer/core": "^0.0.23-alpha.0",
|
|
24
|
+
"chalk": "^5.0.1",
|
|
25
|
+
"figures": "^4.0.1"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"tsc": "tsc"
|
|
21
29
|
},
|
|
22
30
|
"publishConfig": {
|
|
23
31
|
"access": "public"
|
|
24
32
|
},
|
|
25
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "a95b68efa6f407cefff63300e7e50da34c6b6693"
|
|
26
34
|
}
|
package/demo.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
const expand = require('.');
|
|
2
|
-
|
|
3
|
-
(async () => {
|
|
4
|
-
let answer;
|
|
5
|
-
|
|
6
|
-
answer = await expand({
|
|
7
|
-
message: 'Conflict on `file.js`:',
|
|
8
|
-
choices: [
|
|
9
|
-
{
|
|
10
|
-
key: 'y',
|
|
11
|
-
name: 'Overwrite',
|
|
12
|
-
value: 'overwrite',
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
key: 'a',
|
|
16
|
-
name: 'Overwrite this one and all next',
|
|
17
|
-
value: 'overwrite_all',
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
key: 'd',
|
|
21
|
-
name: 'Show diff',
|
|
22
|
-
value: 'diff',
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
key: 'x',
|
|
26
|
-
name: 'Abort',
|
|
27
|
-
value: 'abort',
|
|
28
|
-
},
|
|
29
|
-
],
|
|
30
|
-
});
|
|
31
|
-
console.log('Answer:', answer);
|
|
32
|
-
|
|
33
|
-
answer = await expand({
|
|
34
|
-
message: '(With default) Conflict on `file.js`:',
|
|
35
|
-
default: 'y',
|
|
36
|
-
choices: [
|
|
37
|
-
{
|
|
38
|
-
key: 'y',
|
|
39
|
-
name: 'Overwrite',
|
|
40
|
-
value: 'overwrite',
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
key: 'a',
|
|
44
|
-
name: 'Overwrite this one and all next',
|
|
45
|
-
value: 'overwrite_all',
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
key: 'd',
|
|
49
|
-
name: 'Show diff',
|
|
50
|
-
value: 'diff',
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
key: 'x',
|
|
54
|
-
name: 'Abort',
|
|
55
|
-
value: 'abort',
|
|
56
|
-
},
|
|
57
|
-
],
|
|
58
|
-
});
|
|
59
|
-
console.log('Answer:', answer);
|
|
60
|
-
|
|
61
|
-
answer = await expand({
|
|
62
|
-
expanded: true,
|
|
63
|
-
message: '(Auto-expand) Conflict on `file.js`:',
|
|
64
|
-
choices: [
|
|
65
|
-
{
|
|
66
|
-
key: 'y',
|
|
67
|
-
name: 'Overwrite',
|
|
68
|
-
value: 'overwrite',
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
key: 'a',
|
|
72
|
-
name: 'Overwrite this one and all next',
|
|
73
|
-
value: 'overwrite_all',
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
key: 'd',
|
|
77
|
-
name: 'Show diff',
|
|
78
|
-
value: 'diff',
|
|
79
|
-
},
|
|
80
|
-
{
|
|
81
|
-
key: 'x',
|
|
82
|
-
name: 'Abort',
|
|
83
|
-
value: 'abort',
|
|
84
|
-
},
|
|
85
|
-
],
|
|
86
|
-
});
|
|
87
|
-
console.log('Answer:', answer);
|
|
88
|
-
})();
|
package/index.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
const { createPrompt, useState, useKeypress } = require('@inquirer/core/hooks');
|
|
2
|
-
const { usePrefix } = require('@inquirer/core/lib/prefix');
|
|
3
|
-
const { isEnterKey } = require('@inquirer/core/lib/key');
|
|
4
|
-
const chalk = require('chalk');
|
|
5
|
-
|
|
6
|
-
const helpChoice = {
|
|
7
|
-
key: 'h',
|
|
8
|
-
name: 'Help, list all options',
|
|
9
|
-
value: undefined,
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
module.exports = createPrompt((config, done) => {
|
|
13
|
-
const {
|
|
14
|
-
choices,
|
|
15
|
-
default: defaultKey = 'h',
|
|
16
|
-
expanded: defaultExpandState = false,
|
|
17
|
-
} = config;
|
|
18
|
-
const [status, setStatus] = useState('pending');
|
|
19
|
-
const [value, setValue] = useState('');
|
|
20
|
-
const [expanded, setExpanded] = useState(defaultExpandState);
|
|
21
|
-
const [errorMsg, setError] = useState();
|
|
22
|
-
const prefix = usePrefix();
|
|
23
|
-
|
|
24
|
-
useKeypress((key, rl) => {
|
|
25
|
-
if (isEnterKey(key)) {
|
|
26
|
-
const answer = (value || defaultKey).toLowerCase();
|
|
27
|
-
if (answer === 'h' && !expanded) {
|
|
28
|
-
setExpanded(true);
|
|
29
|
-
} else {
|
|
30
|
-
const selectedChoice = choices.find(({ key }) => key === answer);
|
|
31
|
-
if (selectedChoice) {
|
|
32
|
-
const finalValue = selectedChoice.value || selectedChoice.name;
|
|
33
|
-
setValue(finalValue);
|
|
34
|
-
setStatus('done');
|
|
35
|
-
done(finalValue);
|
|
36
|
-
} else if (value === '') {
|
|
37
|
-
setError('Please input a value');
|
|
38
|
-
} else {
|
|
39
|
-
setError(`"${chalk.red(value)}" isn't an available option`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
setValue(rl.line);
|
|
44
|
-
setError(undefined);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
const message = chalk.bold(config.message);
|
|
49
|
-
|
|
50
|
-
if (status === 'done') {
|
|
51
|
-
return `${prefix} ${message} ${chalk.cyan(value)}`;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const allChoices = expanded ? choices : [...choices, helpChoice];
|
|
55
|
-
|
|
56
|
-
// Collapsed display style
|
|
57
|
-
let longChoices = '';
|
|
58
|
-
let shortChoices = allChoices
|
|
59
|
-
.map((choice) => {
|
|
60
|
-
if (choice.key === defaultKey) {
|
|
61
|
-
return choice.key.toUpperCase();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return choice.key;
|
|
65
|
-
})
|
|
66
|
-
.join('');
|
|
67
|
-
shortChoices = chalk.dim(` (${shortChoices})`);
|
|
68
|
-
|
|
69
|
-
// Expanded display style
|
|
70
|
-
if (expanded) {
|
|
71
|
-
shortChoices = '';
|
|
72
|
-
longChoices = allChoices
|
|
73
|
-
.map((choice) => {
|
|
74
|
-
const line = ` ${choice.key}) ${choice.name || choice.value}`;
|
|
75
|
-
if (choice.key === value.toLowerCase()) {
|
|
76
|
-
return chalk.cyan(line);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return line;
|
|
80
|
-
})
|
|
81
|
-
.join('\n');
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
let helpTip = '';
|
|
85
|
-
const currentOption = allChoices.find(({ key }) => key === value.toLowerCase());
|
|
86
|
-
if (currentOption) {
|
|
87
|
-
helpTip = `${chalk.cyan('>>')} ${currentOption.name || currentOption.value}`;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
let error = '';
|
|
91
|
-
if (errorMsg) {
|
|
92
|
-
error = chalk.red(`> ${errorMsg}`);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return [
|
|
96
|
-
`${prefix} ${message}${shortChoices} ${value}`,
|
|
97
|
-
[longChoices, helpTip, error].filter(Boolean).join('\n'),
|
|
98
|
-
];
|
|
99
|
-
});
|