index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. const inquirer = require("inquirer");
  3. const log = require("npmlog");
  4. exports.promptConfirmation = promptConfirmation;
  5. exports.promptSelectOne = promptSelectOne;
  6. exports.promptTextInput = promptTextInput;
  7. /**
  8. * Prompt for confirmation
  9. * @param {string} message
  10. * @returns {Promise<boolean>}
  11. */
  12. function promptConfirmation(message) {
  13. log.pause();
  14. return inquirer
  15. .prompt([
  16. {
  17. type: "expand",
  18. name: "confirm",
  19. message,
  20. default: 2, // default to help in order to avoid clicking straight through
  21. choices: [
  22. { key: "y", name: "Yes", value: true },
  23. { key: "n", name: "No", value: false },
  24. ],
  25. },
  26. ])
  27. .then((answers) => {
  28. log.resume();
  29. return answers.confirm;
  30. });
  31. }
  32. /**
  33. * Prompt for selection
  34. * @param {string} message
  35. * @param {{ choices: import("inquirer").ListChoiceOptions[] } & Pick<import("inquirer").Question, 'filter' | 'validate'>} [options]
  36. * @returns {Promise<string>}
  37. */
  38. function promptSelectOne(message, { choices, filter, validate } = {}) {
  39. log.pause();
  40. return inquirer
  41. .prompt([
  42. {
  43. type: "list",
  44. name: "prompt",
  45. message,
  46. choices,
  47. pageSize: choices.length,
  48. filter,
  49. validate,
  50. },
  51. ])
  52. .then((answers) => {
  53. log.resume();
  54. return answers.prompt;
  55. });
  56. }
  57. /**
  58. * Prompt for input
  59. * @param {string} message
  60. * @param {Pick<import("inquirer").Question, 'filter' | 'validate'>} [options]
  61. * @returns {Promise<string>}
  62. */
  63. function promptTextInput(message, { filter, validate } = {}) {
  64. log.pause();
  65. return inquirer
  66. .prompt([
  67. {
  68. type: "input",
  69. name: "input",
  70. message,
  71. filter,
  72. validate,
  73. },
  74. ])
  75. .then((answers) => {
  76. log.resume();
  77. return answers.input;
  78. });
  79. }