Skip to content

Commit 9a451f5

Browse files
committed
refactor: improve example code with isCancel checks and type corrections
1 parent 5116ef9 commit 9a451f5

File tree

1 file changed

+70
-18
lines changed

1 file changed

+70
-18
lines changed

src/content/docs/guides/examples.mdx

+70-18
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ const name = await text({
1717
placeholder: 'John Doe',
1818
});
1919

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}!`);
2128
```
2229

2330
### Selection Menu
@@ -39,9 +46,7 @@ if (isCancel(framework)) {
3946
process.exit(0);
4047
}
4148

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}`);
4550
```
4651

4752
### Confirmation Dialog
@@ -77,6 +82,11 @@ async function setupProject() {
7782
},
7883
});
7984

85+
if (isCancel(name)) {
86+
console.log('Operation cancelled');
87+
process.exit(0);
88+
}
89+
8090
const type = await select({
8191
message: 'Project type:',
8292
options: [
@@ -93,7 +103,7 @@ async function setupProject() {
93103

94104
// Get additional details based on project type
95105
let framework;
96-
if (String(type) === 'web') {
106+
if (type === 'web') {
97107
framework = await select({
98108
message: 'Choose a framework:',
99109
options: [
@@ -111,7 +121,7 @@ async function setupProject() {
111121

112122
// Confirm setup
113123
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}` : ''}?`,
115125
});
116126

117127
if (shouldProceed) {
@@ -145,11 +155,21 @@ async function setupConfig(): Promise<Config> {
145155
},
146156
});
147157

158+
if (isCancel(port)) {
159+
console.log('Operation cancelled');
160+
process.exit(0);
161+
}
162+
148163
const host = await text({
149164
message: 'Enter host:',
150165
defaultValue: 'localhost',
151166
});
152167

168+
if (isCancel(host)) {
169+
console.log('Operation cancelled');
170+
process.exit(0);
171+
}
172+
153173
const mode = await select({
154174
message: 'Select mode:',
155175
options: [
@@ -180,8 +200,8 @@ async function setupConfig(): Promise<Config> {
180200
return {
181201
port: Number(port),
182202
host,
183-
mode: String(mode) as 'development' | 'production',
184-
features: [String(features)],
203+
mode: mode as 'development' | 'production',
204+
features: [features],
185205
};
186206
}
187207
```
@@ -208,13 +228,19 @@ async function cliTool() {
208228
process.exit(0);
209229
}
210230

211-
if (String(action) === 'exit') break;
231+
if (action === 'exit') break;
212232

213-
switch (String(action)) {
233+
switch (action) {
214234
case 'create': {
215235
const name = await text({
216236
message: 'Enter item name:',
217-
}) as string;
237+
});
238+
239+
if (isCancel(name)) {
240+
console.log('Operation cancelled');
241+
break;
242+
}
243+
218244
console.log(`Created item: ${name}`);
219245
break;
220246
}
@@ -225,10 +251,17 @@ async function cliTool() {
225251
case 'delete': {
226252
const item = await text({
227253
message: 'Enter item to delete:',
228-
}) as string;
254+
});
255+
256+
if (isCancel(item)) {
257+
console.log('Operation cancelled');
258+
break;
259+
}
260+
229261
const shouldDelete = await confirmPrompt({
230262
message: `Delete ${item}?`,
231263
});
264+
232265
if (shouldDelete) {
233266
console.log(`Deleted ${item}`);
234267
}
@@ -248,11 +281,11 @@ import { text, select, isCancel } from '@clack/prompts';
248281
interface UserData {
249282
name: string;
250283
email: string;
251-
age: string;
284+
age: number;
252285
role: string;
253286
}
254287

255-
async function collectUserData(): Promise<UserData> {
288+
async function collectUserData(): Promise<UserData | null> {
256289
const name = await text({
257290
message: 'Full name:',
258291
validate: (value) => {
@@ -262,6 +295,12 @@ async function collectUserData(): Promise<UserData> {
262295
},
263296
});
264297

298+
if (isCancel(name)) {
299+
console.log('Operation cancelled');
300+
process.exit(0);
301+
return null;
302+
}
303+
265304
const email = await text({
266305
message: 'Email address:',
267306
validate: (value) => {
@@ -271,15 +310,27 @@ async function collectUserData(): Promise<UserData> {
271310
},
272311
});
273312

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({
275320
message: 'Age:',
276321
validate: (value) => {
277322
const num = parseInt(value);
278323
if (isNaN(num)) return 'Please enter a valid number';
279324
if (num < 18 || num > 100) return 'Age must be between 18 and 100';
280325
return undefined;
281326
},
282-
})
327+
});
328+
329+
if (isCancel(ageInput)) {
330+
console.log('Operation cancelled');
331+
process.exit(0);
332+
return null;
333+
}
283334

284335
const role = await select({
285336
message: 'Select role:',
@@ -290,15 +341,16 @@ async function collectUserData(): Promise<UserData> {
290341
],
291342
});
292343

293-
if (isCancel(role) || isCancel(name) || isCancel(email) || isCancel(age)) {
344+
if (isCancel(role)) {
294345
console.log('Operation cancelled');
295346
process.exit(0);
347+
return null;
296348
}
297349

298350
return {
299351
name,
300352
email,
301-
age,
353+
age: parseInt(ageInput),
302354
role,
303355
};
304356
}

0 commit comments

Comments
 (0)