index.js 4.4 KB

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