@@ -17,7 +17,14 @@ const name = await text({
17
17
placeholder: ' John Doe' ,
18
18
});
19
19
20
- console .log (` Hello, ${isCancel (name )}! ` );
20
+ // isCancel is a TypeScript type guard that checks if the result is a symbol
21
+ // If true, TypeScript knows name is a symbol in this block
22
+ if (isCancel (name )) {
23
+ console .log (' Operation cancelled' );
24
+ process .exit (0 );
25
+ }
26
+
27
+ console .log (` Hello, ${name }! ` );
21
28
```
22
29
23
30
### Selection Menu
@@ -39,9 +46,7 @@ if (isCancel(framework)) {
39
46
process .exit (0 );
40
47
}
41
48
42
- // Convert to string before using in template literal
43
- const frameworkStr = isCancel (framework );
44
- console .log (` You selected ${frameworkStr } ` );
49
+ console .log (` You selected ${framework } ` );
45
50
```
46
51
47
52
### Confirmation Dialog
@@ -77,6 +82,11 @@ async function setupProject() {
77
82
},
78
83
});
79
84
85
+ if (isCancel (name )) {
86
+ console .log (' Operation cancelled' );
87
+ process .exit (0 );
88
+ }
89
+
80
90
const type = await select ({
81
91
message: ' Project type:' ,
82
92
options: [
@@ -93,7 +103,7 @@ async function setupProject() {
93
103
94
104
// Get additional details based on project type
95
105
let framework;
96
- if (String ( type ) === ' web' ) {
106
+ if (type === ' web' ) {
97
107
framework = await select ({
98
108
message: ' Choose a framework:' ,
99
109
options: [
@@ -111,7 +121,7 @@ async function setupProject() {
111
121
112
122
// Confirm setup
113
123
const shouldProceed = await confirm ({
114
- message: ` Create ${isCancel ( type ) } project "${isCancel ( name ) }"${framework ? ` with ${isCancel ( framework ) } ` : ' ' }? ` ,
124
+ message: ` Create ${type } project "${name }"${framework ? ` with ${framework } ` : ' ' }? ` ,
115
125
});
116
126
117
127
if (shouldProceed ) {
@@ -145,11 +155,21 @@ async function setupConfig(): Promise<Config> {
145
155
},
146
156
});
147
157
158
+ if (isCancel (port )) {
159
+ console .log (' Operation cancelled' );
160
+ process .exit (0 );
161
+ }
162
+
148
163
const host = await text ({
149
164
message: ' Enter host:' ,
150
165
defaultValue: ' localhost' ,
151
166
});
152
167
168
+ if (isCancel (host )) {
169
+ console .log (' Operation cancelled' );
170
+ process .exit (0 );
171
+ }
172
+
153
173
const mode = await select ({
154
174
message: ' Select mode:' ,
155
175
options: [
@@ -180,8 +200,8 @@ async function setupConfig(): Promise<Config> {
180
200
return {
181
201
port: Number (port ),
182
202
host ,
183
- mode: String ( mode ) as ' development' | ' production' ,
184
- features: [String ( features ) ],
203
+ mode: mode as ' development' | ' production' ,
204
+ features: [features ],
185
205
};
186
206
}
187
207
```
@@ -208,13 +228,19 @@ async function cliTool() {
208
228
process .exit (0 );
209
229
}
210
230
211
- if (String ( action ) === ' exit' ) break ;
231
+ if (action === ' exit' ) break ;
212
232
213
- switch (String ( action ) ) {
233
+ switch (action ) {
214
234
case ' create' : {
215
235
const name = await text ({
216
236
message: ' Enter item name:' ,
217
- }) as string ;
237
+ });
238
+
239
+ if (isCancel (name )) {
240
+ console .log (' Operation cancelled' );
241
+ break ;
242
+ }
243
+
218
244
console .log (` Created item: ${name } ` );
219
245
break ;
220
246
}
@@ -225,10 +251,17 @@ async function cliTool() {
225
251
case ' delete' : {
226
252
const item = await text ({
227
253
message: ' Enter item to delete:' ,
228
- }) as string ;
254
+ });
255
+
256
+ if (isCancel (item )) {
257
+ console .log (' Operation cancelled' );
258
+ break ;
259
+ }
260
+
229
261
const shouldDelete = await confirmPrompt ({
230
262
message: ` Delete ${item }? ` ,
231
263
});
264
+
232
265
if (shouldDelete ) {
233
266
console .log (` Deleted ${item } ` );
234
267
}
@@ -248,11 +281,11 @@ import { text, select, isCancel } from '@clack/prompts';
248
281
interface UserData {
249
282
name: string ;
250
283
email: string ;
251
- age: string ;
284
+ age: number ;
252
285
role: string ;
253
286
}
254
287
255
- async function collectUserData(): Promise <UserData > {
288
+ async function collectUserData(): Promise <UserData | null > {
256
289
const name = await text ({
257
290
message: ' Full name:' ,
258
291
validate : (value ) => {
@@ -262,6 +295,12 @@ async function collectUserData(): Promise<UserData> {
262
295
},
263
296
});
264
297
298
+ if (isCancel (name )) {
299
+ console .log (' Operation cancelled' );
300
+ process .exit (0 );
301
+ return null ;
302
+ }
303
+
265
304
const email = await text ({
266
305
message: ' Email address:' ,
267
306
validate : (value ) => {
@@ -271,15 +310,27 @@ async function collectUserData(): Promise<UserData> {
271
310
},
272
311
});
273
312
274
- const age = await text ({
313
+ if (isCancel (email )) {
314
+ console .log (' Operation cancelled' );
315
+ process .exit (0 );
316
+ return null ;
317
+ }
318
+
319
+ const ageInput = await text ({
275
320
message: ' Age:' ,
276
321
validate : (value ) => {
277
322
const num = parseInt (value );
278
323
if (isNaN (num )) return ' Please enter a valid number' ;
279
324
if (num < 18 || num > 100 ) return ' Age must be between 18 and 100' ;
280
325
return undefined ;
281
326
},
282
- })
327
+ });
328
+
329
+ if (isCancel (ageInput )) {
330
+ console .log (' Operation cancelled' );
331
+ process .exit (0 );
332
+ return null ;
333
+ }
283
334
284
335
const role = await select ({
285
336
message: ' Select role:' ,
@@ -290,15 +341,16 @@ async function collectUserData(): Promise<UserData> {
290
341
],
291
342
});
292
343
293
- if (isCancel (role ) || isCancel ( name ) || isCancel ( email ) || isCancel ( age ) ) {
344
+ if (isCancel (role )) {
294
345
console .log (' Operation cancelled' );
295
346
process .exit (0 );
347
+ return null ;
296
348
}
297
349
298
350
return {
299
351
name ,
300
352
email ,
301
- age ,
353
+ age: parseInt ( ageInput ) ,
302
354
role ,
303
355
};
304
356
}
0 commit comments