-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (50 loc) · 1.71 KB
/
index.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {
createPrompt,
useState,
useKeypress,
usePrefix,
isEnterKey,
makeTheme,
useEffect,
useMemo,
} from '@inquirer/core';
const time = () => Math.ceil(new Date().getTime() / 1000);
export default createPrompt((config, done) => {
const defaultAnswer = config.default !== false;
const { transformer = answer => answer ? 'Yes' : 'No' } = config;
const theme = makeTheme(config.theme);
const [status, setStatus] = useState('idle');
const [value, setValue] = useState('');
const prefix = usePrefix({ status, theme });
const target = useMemo(() => time() + config.timeout, [config]);
const [remaining, setRemaining] = useState(target - time());
function submit(value) {
let answer = defaultAnswer;
if (/^(y|yes)/i.test(value)) answer = true;
else if (/^(n|no)/i.test(value)) answer = false;
setValue(transformer(answer));
setStatus('done');
done(answer);
}
useEffect(() => {
const timerId = setInterval(() => {
const timeout = target - time();
if (timeout <= 0) submit();
else setRemaining(timeout);
}, 1000);
return () => clearInterval(timerId);
}, []);
useKeypress((key, rl) => {
if (isEnterKey(key)) submit(value);
else setValue(rl.line);
});
const message = theme.style.message(config.message, status);
if (status === 'done') {
const formattedValue = theme.style.answer(value);
return `${prefix} ${message} ${formattedValue}`;
} else {
const defaultValue = ` ${theme.style.defaultAnswer(defaultAnswer ? 'Y/n' : 'y/N')}`;
const formattedValue = `${config.timeoutTips(remaining)} ${value}`;
return `${prefix} ${message}${defaultValue} ${formattedValue}`;
}
});