Skip to content

Commit 86fa138

Browse files
yangshunsxzz
andauthored
refactor(create-tsdown): consolidate template options to have single source of truth (#561)
Co-authored-by: Kevin Deng <[email protected]>
1 parent ff946bc commit 86fa138

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

packages/create-tsdown/src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import process from 'node:process'
22
import { log } from '@clack/prompts'
33
import { cac } from 'cac'
44
import { version } from '../package.json'
5-
import { create, type Options } from './index'
5+
import { create, templateOptions, type Options } from './index'
66

77
const cli = cac('create-tsdown')
88
cli.help().version(version)
@@ -14,7 +14,7 @@ cli
1414
})
1515
.option(
1616
'-t, --template <template>',
17-
'Available templates: default, minimal, vue, react, solid',
17+
`Available templates: ${templateOptions.map((option) => option.value).join(', ')}`,
1818
{ default: 'default' },
1919
)
2020
.action((path: string | undefined, options: Options) => create(path, options))

packages/create-tsdown/src/index.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@ import {
1212
import { downloadTemplate } from 'giget'
1313
import { getUserAgent } from 'package-manager-detector'
1414

15+
export const templateOptions = [
16+
{ value: 'default', label: 'Default' },
17+
{ value: 'minimal', label: 'Minimal' },
18+
{ value: 'vue', label: 'Vue' },
19+
{ value: 'react', label: 'React' },
20+
{ value: 'solid', label: 'Solid' },
21+
] as const
22+
23+
type TemplateOption = (typeof templateOptions)[number]['value']
24+
1525
export interface Options {
16-
template?: 'default' | 'minimal' | 'vue' | 'react' | 'solid'
26+
template?: TemplateOption
1727
path?: string
1828
}
1929

@@ -74,21 +84,16 @@ export async function resolveOptions(
7484

7585
let template: Options['template'] | symbol = options.template
7686
if (template) {
77-
if (!['default', 'minimal', 'vue', 'react', 'solid'].includes(template)) {
87+
const templateOptionsValues = templateOptions.map((option) => option.value)
88+
if (!templateOptionsValues.includes(template)) {
7889
throw new Error(
79-
`Invalid template "${template}". Available templates: default, vue, react, solid`,
90+
`Invalid template "${template}". Available templates: ${templateOptionsValues.join(', ')}`,
8091
)
8192
}
8293
} else {
8394
template = await select({
8495
message: 'Which template do you want to use?',
85-
options: [
86-
{ value: 'default', label: 'Default' },
87-
{ value: 'minimal', label: 'Minimal' },
88-
{ value: 'vue', label: 'Vue' },
89-
{ value: 'react', label: 'React' },
90-
{ value: 'solid', label: 'Solid' },
91-
],
96+
options: [...templateOptions],
9297
initialValue: 'default',
9398
})
9499

0 commit comments

Comments
 (0)