index.js 3.4 KB

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