index.d.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { EventEmitter } from "events";
  2. interface BasePromptOptions {
  3. name: string | (() => string)
  4. type: string | (() => string)
  5. message: string | (() => string) | (() => Promise<string>)
  6. initial?: any
  7. required?: boolean
  8. format?(value: string): string | Promise<string>
  9. result?(value: string): string | Promise<string>
  10. skip?: ((state: object) => boolean | Promise<boolean>) | boolean
  11. validate?(value: string): boolean | Promise<boolean> | string | Promise<string>
  12. onSubmit?(name: string, value: any, prompt: Enquirer.Prompt): boolean | Promise<boolean>
  13. onCancel?(name: string, value: any, prompt: Enquirer.Prompt): boolean | Promise<boolean>
  14. stdin?: NodeJS.ReadStream
  15. stdout?: NodeJS.WriteStream
  16. }
  17. interface Choice {
  18. name: string
  19. message?: string
  20. value?: string
  21. hint?: string
  22. disabled?: boolean | string
  23. }
  24. interface ArrayPromptOptions extends BasePromptOptions {
  25. type:
  26. | 'autocomplete'
  27. | 'editable'
  28. | 'form'
  29. | 'multiselect'
  30. | 'select'
  31. | 'survey'
  32. | 'list'
  33. | 'scale'
  34. choices: string[] | Choice[]
  35. maxChoices?: number
  36. muliple?: boolean
  37. initial?: number
  38. delay?: number
  39. separator?: boolean
  40. sort?: boolean
  41. linebreak?: boolean
  42. edgeLength?: number
  43. align?: 'left' | 'right'
  44. scroll?: boolean
  45. }
  46. interface BooleanPromptOptions extends BasePromptOptions {
  47. type: 'confirm'
  48. initial?: boolean
  49. }
  50. interface StringPromptOptions extends BasePromptOptions {
  51. type: 'input' | 'invisible' | 'list' | 'password' | 'text'
  52. initial?: string
  53. multiline?: boolean
  54. }
  55. interface NumberPromptOptions extends BasePromptOptions {
  56. type: 'numeral'
  57. min?: number
  58. max?: number
  59. delay?: number
  60. float?: boolean
  61. round?: boolean
  62. major?: number
  63. minor?: number
  64. initial?: number
  65. }
  66. interface SnippetPromptOptions extends BasePromptOptions {
  67. type: 'snippet'
  68. newline?: string
  69. template?: string
  70. }
  71. interface SortPromptOptions extends BasePromptOptions {
  72. type: 'sort'
  73. hint?: string
  74. drag?: boolean
  75. numbered?: boolean
  76. }
  77. type PromptOptions =
  78. | BasePromptOptions
  79. | ArrayPromptOptions
  80. | BooleanPromptOptions
  81. | StringPromptOptions
  82. | NumberPromptOptions
  83. | SnippetPromptOptions
  84. | SortPromptOptions
  85. declare class BasePrompt extends EventEmitter {
  86. constructor(options?: PromptOptions);
  87. render(): void;
  88. run(): Promise<any>;
  89. }
  90. declare class Enquirer<T = object> extends EventEmitter {
  91. constructor(options?: object, answers?: T);
  92. /**
  93. * Register a custom prompt type.
  94. *
  95. * @param type
  96. * @param fn `Prompt` class, or a function that returns a `Prompt` class.
  97. */
  98. register(type: string, fn: typeof BasePrompt | (() => typeof BasePrompt)): this;
  99. /**
  100. * Register a custom prompt type.
  101. */
  102. register(type: { [key: string]: typeof BasePrompt | (() => typeof BasePrompt) }): this;
  103. /**
  104. * Prompt function that takes a "question" object or array of question objects,
  105. * and returns an object with responses from the user.
  106. *
  107. * @param questions Options objects for one or more prompts to run.
  108. */
  109. prompt(
  110. questions:
  111. | PromptOptions
  112. | ((this: Enquirer) => PromptOptions)
  113. | (PromptOptions | ((this: Enquirer) => PromptOptions))[]
  114. ): Promise<T>;
  115. /**
  116. * Use an enquirer plugin.
  117. *
  118. * @param plugin Plugin function that takes an instance of Enquirer.
  119. */
  120. use(plugin: (this: this, enquirer: this) => void): this;
  121. }
  122. declare namespace Enquirer {
  123. function prompt<T = object>(
  124. questions:
  125. | PromptOptions
  126. | ((this: Enquirer) => PromptOptions)
  127. | (PromptOptions | ((this: Enquirer) => PromptOptions))[]
  128. ): Promise<T>;
  129. class Prompt extends BasePrompt {}
  130. }
  131. export = Enquirer;