1
1
import { stdin , stdout } from 'node:process' ;
2
2
import readline , { type Key , type ReadLine } from 'node:readline' ;
3
- import { Readable , Writable } from 'node:stream' ;
3
+ import type { Readable , Writable } from 'node:stream' ;
4
4
import { WriteStream } from 'node:tty' ;
5
5
import { cursor , erase } from 'sisteransi' ;
6
6
import wrap from 'wrap-ansi' ;
@@ -10,10 +10,10 @@ import { ALIASES, CANCEL_SYMBOL, diffLines, hasAliasKey, KEYS, setRawMode } from
10
10
import type { ClackEvents , ClackState , InferSetType } from '../types' ;
11
11
12
12
export interface PromptOptions < Self extends Prompt > {
13
- render ( this : Omit < Self , 'prompt' > ) : string | void ;
13
+ render ( this : Omit < Self , 'prompt' > ) : string | undefined ;
14
14
placeholder ?: string ;
15
15
initialValue ?: any ;
16
- validate ?: ( ( value : any ) => string | void ) | undefined ;
16
+ validate ?: ( ( value : any ) => string | undefined ) | undefined ;
17
17
input ?: Readable ;
18
18
output ?: Writable ;
19
19
debug ?: boolean ;
@@ -25,7 +25,7 @@ export default class Prompt {
25
25
26
26
private rl ! : ReadLine ;
27
27
private opts : Omit < PromptOptions < Prompt > , 'render' | 'input' | 'output' > ;
28
- private _render : ( context : Omit < Prompt , 'prompt' > ) => string | void ;
28
+ private _render : ( context : Omit < Prompt , 'prompt' > ) => string | undefined ;
29
29
private _track = false ;
30
30
private _prevFrame = '' ;
31
31
private _subscribers = new Map < string , { cb : ( ...args : any ) => any ; once ?: boolean } [ ] > ( ) ;
@@ -35,7 +35,7 @@ export default class Prompt {
35
35
public error = '' ;
36
36
public value : any ;
37
37
38
- constructor ( options : PromptOptions < Prompt > , trackValue : boolean = true ) {
38
+ constructor ( options : PromptOptions < Prompt > , trackValue = true ) {
39
39
const { input = stdin , output = stdout , render, ...opts } = options ;
40
40
41
41
this . opts = opts ;
@@ -110,37 +110,37 @@ export default class Prompt {
110
110
}
111
111
112
112
public prompt ( ) {
113
- const sink = new WriteStream ( 0 ) ;
114
- sink . _write = ( chunk , encoding , done ) => {
115
- if ( this . _track ) {
116
- this . value = this . rl . line . replace ( / \t / g, '' ) ;
117
- this . _cursor = this . rl . cursor ;
118
- this . emit ( 'value' , this . value ) ;
119
- }
120
- done ( ) ;
121
- } ;
122
- this . input . pipe ( sink ) ;
113
+ return new Promise < string | symbol > ( ( resolve , reject ) => {
114
+ const sink = new WriteStream ( 0 ) ;
115
+ sink . _write = ( chunk , encoding , done ) => {
116
+ if ( this . _track ) {
117
+ this . value = this . rl . line . replace ( / \t / g, '' ) ;
118
+ this . _cursor = this . rl . cursor ;
119
+ this . emit ( 'value' , this . value ) ;
120
+ }
121
+ done ( ) ;
122
+ } ;
123
+ this . input . pipe ( sink ) ;
123
124
124
- this . rl = readline . createInterface ( {
125
- input : this . input ,
126
- output : sink ,
127
- tabSize : 2 ,
128
- prompt : '' ,
129
- escapeCodeTimeout : 50 ,
130
- } ) ;
131
- readline . emitKeypressEvents ( this . input , this . rl ) ;
132
- this . rl . prompt ( ) ;
133
- if ( this . opts . initialValue !== undefined && this . _track ) {
134
- this . rl . write ( this . opts . initialValue ) ;
135
- }
125
+ this . rl = readline . createInterface ( {
126
+ input : this . input ,
127
+ output : sink ,
128
+ tabSize : 2 ,
129
+ prompt : '' ,
130
+ escapeCodeTimeout : 50 ,
131
+ } ) ;
132
+ readline . emitKeypressEvents ( this . input , this . rl ) ;
133
+ this . rl . prompt ( ) ;
134
+ if ( this . opts . initialValue !== undefined && this . _track ) {
135
+ this . rl . write ( this . opts . initialValue ) ;
136
+ }
136
137
137
- this . input . on ( 'keypress' , this . onKeypress ) ;
138
- setRawMode ( this . input , true ) ;
139
- this . output . on ( 'resize' , this . render ) ;
138
+ this . input . on ( 'keypress' , this . onKeypress ) ;
139
+ setRawMode ( this . input , true ) ;
140
+ this . output . on ( 'resize' , this . render ) ;
140
141
141
- this . render ( ) ;
142
+ this . render ( ) ;
142
143
143
- return new Promise < string | symbol > ( ( resolve , reject ) => {
144
144
this . once ( 'submit' , ( ) => {
145
145
this . output . write ( cursor . show ) ;
146
146
this . output . off ( 'resize' , this . render ) ;
@@ -193,7 +193,7 @@ export default class Prompt {
193
193
}
194
194
}
195
195
196
- if ( hasAliasKey ( [ key ?. name , key ?. sequence ] , 'cancel' ) ) {
196
+ if ( hasAliasKey ( [ char , key ?. name , key ?. sequence ] , 'cancel' ) ) {
197
197
this . state = 'cancel' ;
198
198
}
199
199
if ( this . state === 'submit' || this . state === 'cancel' ) {
@@ -241,7 +241,8 @@ export default class Prompt {
241
241
this . output . write ( cursor . move ( 0 , lines . length - diffLine - 1 ) ) ;
242
242
return ;
243
243
// If many lines have changed, rerender everything past the first line
244
- } else if ( diff && diff ?. length > 1 ) {
244
+ }
245
+ if ( diff && diff ?. length > 1 ) {
245
246
const diffLine = diff [ 0 ] ;
246
247
this . output . write ( cursor . move ( 0 , diffLine ) ) ;
247
248
this . output . write ( erase . down ( ) ) ;
0 commit comments