Skip to content

Commit

Permalink
fix(add): correct template name validation (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianGlowala authored Jan 9, 2025
1 parent 6b38473 commit eb3087d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
9 changes: 4 additions & 5 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import process from 'node:process'
import { defineCommand } from 'citty'
import { dirname, extname, resolve } from 'pathe'

import { camelCase, kebabCase } from 'scule'
import { loadKit } from '../utils/kit'
import { logger } from '../utils/logger'
import { templates } from '../utils/templates'
import { cwdArgs, logLevelArgs } from './_shared'

const KEBAB_CASE_TEMPLATE_NAMES = Object.keys(templates).map(template => kebabCase(template))
const templateNames = Object.keys(templates)

export default defineCommand({
meta: {
Expand All @@ -27,7 +26,7 @@ export default defineCommand({
template: {
type: 'positional',
required: true,
valueHint: KEBAB_CASE_TEMPLATE_NAMES.join('|'),
valueHint: templateNames.join('|'),
description: `Template type to scaffold`,
},
name: {
Expand All @@ -42,7 +41,7 @@ export default defineCommand({
const templateName = ctx.args.template

// Validate template name
if (!Object.keys(KEBAB_CASE_TEMPLATE_NAMES).includes(templateName)) {
if (!templateNames.includes(templateName)) {
logger.error(
`Template ${templateName} is not supported. Possible values: ${Object.keys(
templates,
Expand All @@ -68,7 +67,7 @@ export default defineCommand({
const config = await kit.loadNuxtConfig({ cwd })

// Resolve template
const template = templates[camelCase(templateName) as keyof typeof templates]
const template = templates[templateName as keyof typeof templates]

const res = template({ name, args: ctx.args, nuxtOptions: config })

Expand Down
32 changes: 16 additions & 16 deletions src/utils/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ interface Template {
}

const templates = {
api,
app,
appConfig,
component,
composable,
error,
layer,
layout,
middleware,
module,
page,
plugin,
serverMiddleware,
serverPlugin,
serverRoute,
serverUtil,
'api': api,
'app': app,
'app-config': appConfig,
'component': component,
'composable': composable,
'error': error,
'layer': layer,
'layout': layout,
'middleware': middleware,
'module': module,
'page': page,
'plugin': plugin,
'server-middleware': serverMiddleware,
'server-plugin': serverPlugin,
'server-route': serverRoute,
'server-util': serverUtil,
} satisfies Record<string, Template>

// -- internal utils --
Expand Down

0 comments on commit eb3087d

Please sign in to comment.