index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { createPrompt, useMemo, useState, useKeypress, usePrefix, isEnterKey, makeTheme, Separator, } from '@inquirer/core';
  2. import colors from 'yoctocolors-cjs';
  3. function normalizeChoices(choices) {
  4. return choices.map((choice) => {
  5. if (Separator.isSeparator(choice)) {
  6. return choice;
  7. }
  8. const name = 'name' in choice ? choice.name : String(choice.value);
  9. const value = 'value' in choice ? choice.value : name;
  10. return {
  11. value: value,
  12. name,
  13. key: choice.key.toLowerCase(),
  14. };
  15. });
  16. }
  17. const helpChoice = {
  18. key: 'h',
  19. name: 'Help, list all options',
  20. value: undefined,
  21. };
  22. export default createPrompt((config, done) => {
  23. const { default: defaultKey = 'h' } = config;
  24. const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
  25. const [status, setStatus] = useState('idle');
  26. const [value, setValue] = useState('');
  27. const [expanded, setExpanded] = useState(config.expanded ?? false);
  28. const [errorMsg, setError] = useState();
  29. const theme = makeTheme(config.theme);
  30. const prefix = usePrefix({ theme, status });
  31. useKeypress((event, rl) => {
  32. if (isEnterKey(event)) {
  33. const answer = (value || defaultKey).toLowerCase();
  34. if (answer === 'h' && !expanded) {
  35. setExpanded(true);
  36. }
  37. else {
  38. const selectedChoice = choices.find((choice) => !Separator.isSeparator(choice) && choice.key === answer);
  39. if (selectedChoice) {
  40. setStatus('done');
  41. // Set the value as we might've selected the default one.
  42. setValue(answer);
  43. done(selectedChoice.value);
  44. }
  45. else if (value === '') {
  46. setError('Please input a value');
  47. }
  48. else {
  49. setError(`"${colors.red(value)}" isn't an available option`);
  50. }
  51. }
  52. }
  53. else {
  54. setValue(rl.line);
  55. setError(undefined);
  56. }
  57. });
  58. const message = theme.style.message(config.message, status);
  59. if (status === 'done') {
  60. // If the prompt is done, it's safe to assume there is a selected value.
  61. const selectedChoice = choices.find((choice) => !Separator.isSeparator(choice) && choice.key === value.toLowerCase());
  62. return `${prefix} ${message} ${theme.style.answer(selectedChoice.name)}`;
  63. }
  64. const allChoices = expanded ? choices : [...choices, helpChoice];
  65. // Collapsed display style
  66. let longChoices = '';
  67. let shortChoices = allChoices
  68. .map((choice) => {
  69. if (Separator.isSeparator(choice))
  70. return '';
  71. if (choice.key === defaultKey) {
  72. return choice.key.toUpperCase();
  73. }
  74. return choice.key;
  75. })
  76. .join('');
  77. shortChoices = ` ${theme.style.defaultAnswer(shortChoices)}`;
  78. // Expanded display style
  79. if (expanded) {
  80. shortChoices = '';
  81. longChoices = allChoices
  82. .map((choice) => {
  83. if (Separator.isSeparator(choice)) {
  84. return ` ${choice.separator}`;
  85. }
  86. const line = ` ${choice.key}) ${choice.name}`;
  87. if (choice.key === value.toLowerCase()) {
  88. return theme.style.highlight(line);
  89. }
  90. return line;
  91. })
  92. .join('\n');
  93. }
  94. let helpTip = '';
  95. const currentOption = choices.find((choice) => !Separator.isSeparator(choice) && choice.key === value.toLowerCase());
  96. if (currentOption) {
  97. helpTip = `${colors.cyan('>>')} ${currentOption.name}`;
  98. }
  99. let error = '';
  100. if (errorMsg) {
  101. error = theme.style.error(errorMsg);
  102. }
  103. return [
  104. `${prefix} ${message}${shortChoices} ${value}`,
  105. [longChoices, helpTip, error].filter(Boolean).join('\n'),
  106. ];
  107. });
  108. export { Separator } from '@inquirer/core';