Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = createPrompt((config, done) => {
const [status, setStatus] = useState('pending');
const [defaultValue, setDefaultValue] = useState(config.default);
const [errorMsg, setError] = useState();
const [value, setValue] = useState('');
const isLoading = status === 'loading';
const prefix = usePrefix(isLoading);
useKeypress(async (key, rl) => {
// Ignore keypress while our prompt is doing other processing.
if (status !== 'pending') {
return;
}
if (isEnterKey(key)) {
const answer = value || defaultValue || '';
setStatus('loading');
const isValid = await config.validate(answer);
if (isValid === true) {
setValue(answer);
module.exports = createPrompt((config, done) => {
const [status, setStatus] = useState('pending');
const [defaultValue, setDefaultValue] = useState(config.default);
const [errorMsg, setError] = useState();
const [value, setValue] = useState('');
const isLoading = status === 'loading';
const prefix = usePrefix(isLoading);
useKeypress(async (key, rl) => {
// Ignore keypress while our prompt is doing other processing.
if (status !== 'pending') {
return;
}
if (isEnterKey(key)) {
const answer = value || defaultValue || '';
setStatus('loading');
module.exports = createPrompt((config, done) => {
const [status, setStatus] = useState('pending');
const [defaultValue, setDefaultValue] = useState(config.default);
const [errorMsg, setError] = useState();
const [value, setValue] = useState('');
const isLoading = status === 'loading';
const prefix = usePrefix(isLoading);
useKeypress(async (key, rl) => {
// Ignore keypress while our prompt is doing other processing.
if (status !== 'pending') {
return;
}
if (isEnterKey(key)) {
const answer = value || defaultValue || '';
setStatus('loading');
const isValid = await config.validate(answer);
const { createPrompt, useState, useKeypress } = require('@inquirer/core/hooks');
const { usePrefix } = require('@inquirer/core/lib/prefix');
const { isEnterKey, isBackspaceKey } = require('@inquirer/core/lib/key');
const chalk = require('chalk');
module.exports = createPrompt((config, done) => {
const [status, setStatus] = useState('pending');
const [defaultValue, setDefaultValue] = useState(config.default);
const [errorMsg, setError] = useState();
const [value, setValue] = useState('');
const isLoading = status === 'loading';
const prefix = usePrefix(isLoading);
useKeypress(async (key, rl) => {
// Ignore keypress while our prompt is doing other processing.
if (status !== 'pending') {
return;
}
if (isEnterKey(key)) {
const answer = value || defaultValue || '';
useKeypress(async (key, rl) => {
// Ignore keypress while our prompt is doing other processing.
if (status !== 'pending') {
return;
}
if (isEnterKey(key)) {
const answer = value || defaultValue || '';
setStatus('loading');
const isValid = await config.validate(answer);
if (isValid === true) {
setValue(answer);
setStatus('done');
done(answer);
} else {
// TODO: Can we keep the value after validation failure?
// `rl.line = value` works but it looses the cursor position.
setValue('');
setError(isValid || 'You must provide a valid value');
setStatus('pending');
}
} else if (isBackspaceKey(key) && !value) {
setDefaultValue(undefined);
useKeypress(key => {
if (isEnterKey(key)) {
setStatus('done');
done(choices[cursorPosition].value);
} else if (isUpKey(key) || isDownKey(key)) {
let newCursorPosition = cursorPosition;
const offset = isUpKey(key) ? -1 : 1;
let selectedOption;
while (!selectedOption || selectedOption.disabled) {
newCursorPosition =
(newCursorPosition + offset + choices.length) % choices.length;
selectedOption = choices[newCursorPosition];
}
setCursorPos(newCursorPosition);
} else if (isNumberKey(key)) {
// Adjust index to start at 1
useKeypress(key => {
if (isEnterKey(key)) {
setStatus('done');
done(choices[cursorPosition].value);
} else if (isUpKey(key) || isDownKey(key)) {
let newCursorPosition = cursorPosition;
const offset = isUpKey(key) ? -1 : 1;
let selectedOption;
while (!selectedOption || selectedOption.disabled) {
newCursorPosition =
(newCursorPosition + offset + choices.length) % choices.length;
selectedOption = choices[newCursorPosition];
}
setCursorPos(newCursorPosition);
} else if (isNumberKey(key)) {
// Adjust index to start at 1
const newCursorPosition = Number(key.name) - 1;
// Abort if the choice doesn't exists or if disabled
useKeypress(key => {
if (isEnterKey(key)) {
setStatus('done');
done(choices[cursorPosition].value);
} else if (isUpKey(key) || isDownKey(key)) {
let newCursorPosition = cursorPosition;
const offset = isUpKey(key) ? -1 : 1;
let selectedOption;
while (!selectedOption || selectedOption.disabled) {
newCursorPosition =
(newCursorPosition + offset + choices.length) % choices.length;
selectedOption = choices[newCursorPosition];
}
setCursorPos(newCursorPosition);
} else if (isNumberKey(key)) {
// Adjust index to start at 1
const newCursorPosition = Number(key.name) - 1;
// Abort if the choice doesn't exists or if disabled
if (!choices[newCursorPosition] || choices[newCursorPosition].disabled) {
return;
module.exports = createPrompt((config, done) => {
const [status, setStatus] = useState('pending');
const [defaultValue, setDefaultValue] = useState(config.default);
const [errorMsg, setError] = useState();
const [value, setValue] = useState('');
const isLoading = status === 'loading';
const prefix = usePrefix(isLoading);
useKeypress(async (key, rl) => {
// Ignore keypress while our prompt is doing other processing.
if (status !== 'pending') {
return;
}
if (isEnterKey(key)) {
const answer = value || defaultValue || '';
setStatus('loading');
const isValid = await config.validate(answer);
if (isValid === true) {
setValue(answer);
setStatus('done');
done(answer);
} else {
})
});
} else if (key.name === 'a') {
const selectAll = Boolean(choices.find(choice => !choice.checked));
setState({
choices: choices.map(choice =>
Object.assign({}, choice, { checked: selectAll })
)
});
} else if (key.name === 'i') {
setState({
choices: choices.map(choice =>
Object.assign({}, choice, { checked: !choice.checked })
)
});
} else if (isNumberKey(key)) {
// Adjust index to start at 1
const position = Number(key.name) - 1;
// Abort if the choice doesn't exists or if disabled
if (!choices[position] || choices[position].disabled) {
return;
}
setState({
cursorPosition: position,
choices: choices.map((choice, i) => {
if (i === position) {
return Object.assign({}, choice, { checked: !choice.checked });
}
return choice;