index.d.ts 3.7 KB

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