Skip to content

Commit 20c19d9

Browse files
committed
feat(@clack/prompts): builder example
1 parent 07e91d2 commit 20c19d9

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

examples/builder.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as p from '@clack/prompts';
2+
3+
(async () => {
4+
const results = await p
5+
.builder()
6+
.add('path', () =>
7+
p.text({
8+
message: 'Where should we create your project?',
9+
placeholder: './sparkling-solid',
10+
validate: (value) => {
11+
if (!value) return 'Please enter a path.';
12+
if (value[0] !== '.') return 'Please enter a relative path.';
13+
},
14+
})
15+
)
16+
.add('password', () =>
17+
p.password({
18+
message: 'Provide a password',
19+
validate: (value) => {
20+
if (!value) return 'Please enter a password.';
21+
if (value.length < 5) return 'Password should have at least 5 characters.';
22+
},
23+
})
24+
)
25+
.add('type', ({ results }) =>
26+
p.select({
27+
message: `Pick a project type within "${results.path}"`,
28+
initialValue: 'ts',
29+
maxItems: 5,
30+
options: [
31+
{ value: 'ts', label: 'TypeScript' },
32+
{ value: 'js', label: 'JavaScript' },
33+
{ value: 'rust', label: 'Rust' },
34+
{ value: 'go', label: 'Go' },
35+
{ value: 'python', label: 'Python' },
36+
{ value: 'coffee', label: 'CoffeeScript', hint: 'oh no' },
37+
],
38+
})
39+
)
40+
.add('tools', () =>
41+
p.multiselect({
42+
message: 'Select additional tools.',
43+
initialValues: ['prettier', 'eslint'],
44+
options: [
45+
{ value: 'prettier', label: 'Prettier', hint: 'recommended' },
46+
{ value: 'eslint', label: 'ESLint', hint: 'recommended' },
47+
{ value: 'stylelint', label: 'Stylelint' },
48+
{ value: 'gh-action', label: 'GitHub Action' },
49+
],
50+
})
51+
)
52+
.add('install', ({ results }) =>
53+
p.confirm({
54+
message: 'Install dependencies?',
55+
initialValue: false,
56+
})
57+
)
58+
.run();
59+
})();

examples/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"scripts": {
1212
"basic": "jiti ./basic.ts",
1313
"spinner": "jiti ./spinner.ts",
14-
"changesets": "jiti ./changesets.ts"
14+
"changesets": "jiti ./changesets.ts",
15+
"builder": "jiti ./builder.ts"
1516
},
1617
"devDependencies": {
1718
"jiti": "^1.17.0"

0 commit comments

Comments
 (0)