index.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { createPrompt, useMemo, useState, useKeypress, usePrefix, isEnterKey, Separator, makeTheme, } from '@inquirer/core';
  2. import colors from 'yoctocolors-cjs';
  3. const numberRegex = /\d+/;
  4. function isSelectableChoice(choice) {
  5. return choice != null && !Separator.isSeparator(choice);
  6. }
  7. function normalizeChoices(choices) {
  8. let index = 0;
  9. return choices.map((choice) => {
  10. if (Separator.isSeparator(choice))
  11. return choice;
  12. index += 1;
  13. if (typeof choice === 'string') {
  14. return {
  15. value: choice,
  16. name: choice,
  17. short: choice,
  18. key: String(index),
  19. };
  20. }
  21. const name = choice.name ?? String(choice.value);
  22. return {
  23. value: choice.value,
  24. name,
  25. short: choice.short ?? name,
  26. key: choice.key ?? String(index),
  27. };
  28. });
  29. }
  30. export default createPrompt((config, done) => {
  31. const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
  32. const [status, setStatus] = useState('idle');
  33. const [value, setValue] = useState('');
  34. const [errorMsg, setError] = useState();
  35. const theme = makeTheme(config.theme);
  36. const prefix = usePrefix({ status, theme });
  37. useKeypress((key, rl) => {
  38. if (isEnterKey(key)) {
  39. let selectedChoice;
  40. if (numberRegex.test(value)) {
  41. const answer = Number.parseInt(value, 10) - 1;
  42. selectedChoice = choices.filter(isSelectableChoice)[answer];
  43. }
  44. else {
  45. selectedChoice = choices.find((choice) => isSelectableChoice(choice) && choice.key === value);
  46. }
  47. if (isSelectableChoice(selectedChoice)) {
  48. setValue(selectedChoice.short);
  49. setStatus('done');
  50. done(selectedChoice.value);
  51. }
  52. else if (value === '') {
  53. setError('Please input a value');
  54. }
  55. else {
  56. setError(`"${colors.red(value)}" isn't an available option`);
  57. }
  58. }
  59. else {
  60. setValue(rl.line);
  61. setError(undefined);
  62. }
  63. });
  64. const message = theme.style.message(config.message, status);
  65. if (status === 'done') {
  66. return `${prefix} ${message} ${theme.style.answer(value)}`;
  67. }
  68. const choicesStr = choices
  69. .map((choice) => {
  70. if (Separator.isSeparator(choice)) {
  71. return ` ${choice.separator}`;
  72. }
  73. const line = ` ${choice.key}) ${choice.name}`;
  74. if (choice.key === value.toLowerCase()) {
  75. return theme.style.highlight(line);
  76. }
  77. return line;
  78. })
  79. .join('\n');
  80. let error = '';
  81. if (errorMsg) {
  82. error = theme.style.error(errorMsg);
  83. }
  84. return [
  85. `${prefix} ${message} ${value}`,
  86. [choicesStr, error].filter(Boolean).join('\n'),
  87. ];
  88. });
  89. export { Separator } from '@inquirer/core';