File size: 1,234 Bytes
9ada4bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { createPrompt, useState, useKeypress, isEnterKey, usePrefix, makeTheme, } from '@inquirer/core';
export default createPrompt((config, done) => {
    const { transformer = (answer) => (answer ? 'yes' : 'no') } = config;
    const [status, setStatus] = useState('pending');
    const [value, setValue] = useState('');
    const theme = makeTheme(config.theme);
    const prefix = usePrefix({ theme });
    useKeypress((key, rl) => {
        if (isEnterKey(key)) {
            let answer = config.default !== false;
            if (/^(y|yes)/i.test(value))
                answer = true;
            else if (/^(n|no)/i.test(value))
                answer = false;
            setValue(transformer(answer));
            setStatus('done');
            done(answer);
        }
        else {
            setValue(rl.line);
        }
    });
    let formattedValue = value;
    let defaultValue = '';
    if (status === 'done') {
        formattedValue = theme.style.answer(value);
    }
    else {
        defaultValue = ` ${theme.style.defaultAnswer(config.default === false ? 'y/N' : 'Y/n')}`;
    }
    const message = theme.style.message(config.message);
    return `${prefix} ${message}${defaultValue} ${formattedValue}`;
});