@inquirer/input 0.0.19-alpha.0 → 0.0.22-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 +12 -0
- package/dist/index.js +57 -0
- package/package.json +13 -5
- package/demo.js +0 -47
- package/index.js +0 -64
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AsyncPromptConfig } from '@inquirer/core';
|
|
2
|
+
export declare type InputConfig = AsyncPromptConfig & {
|
|
3
|
+
default?: string;
|
|
4
|
+
transformer?: (value: string, { isFinal }: {
|
|
5
|
+
isFinal: boolean;
|
|
6
|
+
}) => string;
|
|
7
|
+
};
|
|
8
|
+
declare const _default: (options: InputConfig, stdio?: {
|
|
9
|
+
input?: NodeJS.ReadableStream | undefined;
|
|
10
|
+
output?: NodeJS.WritableStream | undefined;
|
|
11
|
+
} | undefined) => Promise<string>;
|
|
12
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { createPrompt, useState, useKeypress, usePrefix, isEnterKey, isBackspaceKey, } from '@inquirer/core';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
export default createPrompt((config, done) => {
|
|
4
|
+
const [status, setStatus] = useState('pending');
|
|
5
|
+
const [defaultValue, setDefaultValue] = useState(config.default);
|
|
6
|
+
const [errorMsg, setError] = useState(undefined);
|
|
7
|
+
const [value, setValue] = useState('');
|
|
8
|
+
const isLoading = status === 'loading';
|
|
9
|
+
const prefix = usePrefix(isLoading);
|
|
10
|
+
useKeypress(async (key, rl) => {
|
|
11
|
+
// Ignore keypress while our prompt is doing other processing.
|
|
12
|
+
if (status !== 'pending') {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (isEnterKey(key)) {
|
|
16
|
+
const answer = value || defaultValue || '';
|
|
17
|
+
setStatus('loading');
|
|
18
|
+
const isValid = await config.validate(answer);
|
|
19
|
+
if (isValid === true) {
|
|
20
|
+
setValue(answer);
|
|
21
|
+
setStatus('done');
|
|
22
|
+
done(answer);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
// TODO: Can we keep the value after validation failure?
|
|
26
|
+
// `rl.line = value` works but it looses the cursor position.
|
|
27
|
+
setValue('');
|
|
28
|
+
setError(isValid || 'You must provide a valid value');
|
|
29
|
+
setStatus('pending');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else if (isBackspaceKey(key) && !value) {
|
|
33
|
+
setDefaultValue(undefined);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
setValue(rl.line);
|
|
37
|
+
setError(undefined);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
const message = chalk.bold(config.message);
|
|
41
|
+
let formattedValue = value;
|
|
42
|
+
if (typeof config.transformer === 'function') {
|
|
43
|
+
formattedValue = config.transformer(value, { isFinal: status === 'done' });
|
|
44
|
+
}
|
|
45
|
+
if (status === 'done') {
|
|
46
|
+
formattedValue = chalk.cyan(formattedValue);
|
|
47
|
+
}
|
|
48
|
+
let defaultStr = '';
|
|
49
|
+
if (defaultValue && status !== 'done' && !value) {
|
|
50
|
+
defaultStr = chalk.dim(` (${defaultValue})`);
|
|
51
|
+
}
|
|
52
|
+
let error = '';
|
|
53
|
+
if (errorMsg) {
|
|
54
|
+
error = chalk.red(`> ${errorMsg}`);
|
|
55
|
+
}
|
|
56
|
+
return [`${prefix} ${message}${defaultStr} ${formattedValue}`, error];
|
|
57
|
+
});
|
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/input",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.22-alpha.0",
|
|
4
5
|
"description": "Inquirer input text 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",
|
|
@@ -14,11 +19,14 @@
|
|
|
14
19
|
"license": "MIT",
|
|
15
20
|
"homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/input/README.md",
|
|
16
21
|
"dependencies": {
|
|
17
|
-
"@inquirer/core": "^0.0.
|
|
18
|
-
"chalk": "^
|
|
22
|
+
"@inquirer/core": "^0.0.22-alpha.0",
|
|
23
|
+
"chalk": "^5.0.1"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"tsc": "tsc"
|
|
19
27
|
},
|
|
20
28
|
"publishConfig": {
|
|
21
29
|
"access": "public"
|
|
22
30
|
},
|
|
23
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "1d5e7e3d15c86851f2e5df2bb9f55084ba231c54"
|
|
24
32
|
}
|
package/demo.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
const chalk = require('chalk');
|
|
2
|
-
const input = require('.');
|
|
3
|
-
|
|
4
|
-
const hexRegEx = /([0-9]|[a-f])/gim;
|
|
5
|
-
const isHex = (value) =>
|
|
6
|
-
(value.match(hexRegEx) || []).length === value.length &&
|
|
7
|
-
(value.length === 3 || value.length === 6);
|
|
8
|
-
|
|
9
|
-
(async () => {
|
|
10
|
-
let answer;
|
|
11
|
-
|
|
12
|
-
answer = await input({
|
|
13
|
-
message: 'Enter an hex color?',
|
|
14
|
-
transformer: (value = '', { isFinal }) => {
|
|
15
|
-
const color = chalk.hex(isHex(value) ? value : 'fff');
|
|
16
|
-
return isFinal ? color.underline(value) : color(value);
|
|
17
|
-
},
|
|
18
|
-
validate: (value = '') => isHex(value) || 'Pass a valid hex value',
|
|
19
|
-
});
|
|
20
|
-
console.log('Answer:', answer);
|
|
21
|
-
|
|
22
|
-
answer = await input({
|
|
23
|
-
message: '(Slow validation) provide a number:',
|
|
24
|
-
validate: (value) =>
|
|
25
|
-
new Promise((resolve) => {
|
|
26
|
-
setTimeout(
|
|
27
|
-
() => resolve(!Number.isNaN(Number(value)) || 'You must provide a number'),
|
|
28
|
-
3000
|
|
29
|
-
);
|
|
30
|
-
}),
|
|
31
|
-
});
|
|
32
|
-
console.log('Answer:', answer);
|
|
33
|
-
|
|
34
|
-
answer = await input({
|
|
35
|
-
message: () =>
|
|
36
|
-
new Promise((resolve) => {
|
|
37
|
-
setTimeout(() => resolve('(Slow message) Input any value:'), 3000);
|
|
38
|
-
}),
|
|
39
|
-
});
|
|
40
|
-
console.log('Answer:', answer);
|
|
41
|
-
|
|
42
|
-
answer = await input({
|
|
43
|
-
message: "What's your favorite food?",
|
|
44
|
-
default: 'Croissant',
|
|
45
|
-
});
|
|
46
|
-
console.log('Answer:', answer);
|
|
47
|
-
})();
|
package/index.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
const { createPrompt, useState, useKeypress } = require('@inquirer/core/hooks');
|
|
2
|
-
const { usePrefix } = require('@inquirer/core/lib/prefix');
|
|
3
|
-
const { isEnterKey, isBackspaceKey } = require('@inquirer/core/lib/key');
|
|
4
|
-
const chalk = require('chalk');
|
|
5
|
-
|
|
6
|
-
module.exports = createPrompt((config, done) => {
|
|
7
|
-
const [status, setStatus] = useState('pending');
|
|
8
|
-
const [defaultValue, setDefaultValue] = useState(config.default);
|
|
9
|
-
const [errorMsg, setError] = useState();
|
|
10
|
-
const [value, setValue] = useState('');
|
|
11
|
-
|
|
12
|
-
const isLoading = status === 'loading';
|
|
13
|
-
const prefix = usePrefix(isLoading);
|
|
14
|
-
|
|
15
|
-
useKeypress(async (key, rl) => {
|
|
16
|
-
// Ignore keypress while our prompt is doing other processing.
|
|
17
|
-
if (status !== 'pending') {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (isEnterKey(key)) {
|
|
22
|
-
const answer = value || defaultValue || '';
|
|
23
|
-
setStatus('loading');
|
|
24
|
-
const isValid = await config.validate(answer);
|
|
25
|
-
if (isValid === true) {
|
|
26
|
-
setValue(answer);
|
|
27
|
-
setStatus('done');
|
|
28
|
-
done(answer);
|
|
29
|
-
} else {
|
|
30
|
-
// TODO: Can we keep the value after validation failure?
|
|
31
|
-
// `rl.line = value` works but it looses the cursor position.
|
|
32
|
-
setValue('');
|
|
33
|
-
setError(isValid || 'You must provide a valid value');
|
|
34
|
-
setStatus('pending');
|
|
35
|
-
}
|
|
36
|
-
} else if (isBackspaceKey(key) && !value) {
|
|
37
|
-
setDefaultValue(undefined);
|
|
38
|
-
} else {
|
|
39
|
-
setValue(rl.line);
|
|
40
|
-
setError(undefined);
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
const message = chalk.bold(config.message);
|
|
45
|
-
let formattedValue = value;
|
|
46
|
-
if (typeof config.transformer === 'function') {
|
|
47
|
-
formattedValue = config.transformer(value, { isFinal: status === 'done' });
|
|
48
|
-
}
|
|
49
|
-
if (status === 'done') {
|
|
50
|
-
formattedValue = chalk.cyan(formattedValue);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
let defaultStr = '';
|
|
54
|
-
if (defaultValue && status !== 'done' && !value) {
|
|
55
|
-
defaultStr = chalk.dim(` (${defaultValue})`);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
let error = '';
|
|
59
|
-
if (errorMsg) {
|
|
60
|
-
error = chalk.red(`> ${errorMsg}`);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return [`${prefix} ${message}${defaultStr} ${formattedValue}`, error];
|
|
64
|
-
});
|