1
1
import { TextPrompt , SelectPrompt , ConfirmPrompt } from "@clack/core" ;
2
2
import { isCancel } from "@clack/prompts" ;
3
3
import logUpdate from "log-update" ;
4
- import { shapes , cancel , space , status , newline } from "./cli" ;
4
+ import { shapes , cancel , space , status , newline , logRaw } from "./cli" ;
5
5
import { blue , dim , gray , brandColor , bold } from "./colors" ;
6
6
7
7
const grayBar = gray ( shapes . bar ) ;
@@ -14,10 +14,12 @@ export type TextOptions = {
14
14
defaultValue : string ;
15
15
helpText ?: string ;
16
16
validate ?: ( value : string ) => string | void ;
17
+ initialValue ?: string ;
17
18
} ;
18
19
19
20
export const textInput = async ( opts : TextOptions ) => {
20
- const { renderSubmitted, question, defaultValue, validate } = opts ;
21
+ const { renderSubmitted, question, defaultValue, validate, initialValue } =
22
+ opts ;
21
23
const helpText = opts . helpText || `` ;
22
24
23
25
const prompt = new TextPrompt ( {
@@ -52,14 +54,21 @@ export const textInput = async (opts: TextOptions) => {
52
54
} ,
53
55
} ) ;
54
56
55
- const value = await prompt . prompt ( ) ;
56
-
57
- if ( isCancel ( value ) ) {
58
- cancel ( "Operation cancelled." ) ;
59
- process . exit ( 0 ) ;
57
+ let value : string ;
58
+ if ( initialValue ) {
59
+ logRaw ( `${ leftT } ${ question } ` ) ;
60
+ logRaw ( `${ grayBar } ${ renderSubmitted ( initialValue ) } \n${ grayBar } ` ) ;
61
+ value = initialValue ;
62
+ } else {
63
+ value = ( await prompt . prompt ( ) ) as string ;
64
+
65
+ if ( isCancel ( value ) ) {
66
+ cancel ( "Operation cancelled." ) ;
67
+ process . exit ( 0 ) ;
68
+ }
60
69
}
61
70
62
- return value as string ;
71
+ return value ;
63
72
} ;
64
73
65
74
export type Option = {
@@ -72,10 +81,11 @@ type SelectOptions = {
72
81
renderSubmitted : ( option : Option ) => string ;
73
82
options : Option [ ] ;
74
83
helpText ?: string ;
84
+ initialValue ?: string ;
75
85
} ;
76
86
77
87
export const selectInput = async ( opts : SelectOptions ) => {
78
- const { question, options, renderSubmitted } = opts ;
88
+ const { question, options, renderSubmitted, initialValue } = opts ;
79
89
const helpText = opts . helpText || `` ;
80
90
81
91
const prompt = new SelectPrompt ( {
@@ -111,11 +121,24 @@ export const selectInput = async (opts: SelectOptions) => {
111
121
} ,
112
122
} ) ;
113
123
114
- const value = await prompt . prompt ( ) ;
115
-
116
- if ( isCancel ( value ) ) {
117
- cancel ( "Operation cancelled." ) ;
118
- process . exit ( 0 ) ;
124
+ let value : string ;
125
+ if ( initialValue ) {
126
+ logRaw ( `${ leftT } ${ question } ` ) ;
127
+ logRaw (
128
+ `${ grayBar } ${ renderSubmitted ( {
129
+ label : initialValue ,
130
+ value : initialValue ,
131
+ } ) } `
132
+ ) ;
133
+ logRaw ( `${ grayBar } ` ) ;
134
+ value = initialValue ;
135
+ } else {
136
+ value = ( await prompt . prompt ( ) ) as string ;
137
+
138
+ if ( isCancel ( value ) ) {
139
+ cancel ( "Operation cancelled." ) ;
140
+ process . exit ( 0 ) ;
141
+ }
119
142
}
120
143
121
144
return value as string ;
@@ -128,11 +151,18 @@ type ConfirmOptions = {
128
151
activeText ?: string ;
129
152
inactiveText ?: string ;
130
153
helpText ?: string ;
154
+ initialValue ?: boolean ;
131
155
} ;
132
156
133
157
export const confirmInput = async ( opts : ConfirmOptions ) => {
134
- const { activeText, inactiveText, question, renderSubmitted, defaultValue } =
135
- opts ;
158
+ const {
159
+ activeText,
160
+ inactiveText,
161
+ question,
162
+ renderSubmitted,
163
+ defaultValue,
164
+ initialValue,
165
+ } = opts ;
136
166
const helpText = opts . helpText || `(y/n)` ;
137
167
138
168
const active = activeText || "Yes" ;
@@ -163,11 +193,20 @@ export const confirmInput = async (opts: ConfirmOptions) => {
163
193
} ,
164
194
} ) ;
165
195
166
- const value = Boolean ( await prompt . prompt ( ) ) ;
196
+ let value : boolean ;
197
+
198
+ if ( initialValue !== undefined ) {
199
+ logRaw ( `${ leftT } ${ question } ` ) ;
200
+ logRaw ( `${ grayBar } ${ renderSubmitted ( initialValue ) } ` ) ;
201
+ logRaw ( `${ grayBar } ` ) ;
202
+ value = initialValue ;
203
+ } else {
204
+ value = Boolean ( await prompt . prompt ( ) ) ;
167
205
168
- if ( isCancel ( value ) ) {
169
- cancel ( "Operation cancelled." ) ;
170
- process . exit ( 0 ) ;
206
+ if ( isCancel ( value ) ) {
207
+ cancel ( "Operation cancelled." ) ;
208
+ process . exit ( 0 ) ;
209
+ }
171
210
}
172
211
173
212
return value ;
0 commit comments