Skip to content

Commit

Permalink
feat(add): move templates to separate files and add new ones (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianGlowala authored Jan 7, 2025
1 parent dacb6c0 commit af2ea90
Show file tree
Hide file tree
Showing 20 changed files with 367 additions and 165 deletions.
21 changes: 13 additions & 8 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ 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))

export default defineCommand({
meta: {
name: 'add',
Expand All @@ -24,7 +27,7 @@ export default defineCommand({
template: {
type: 'positional',
required: true,
valueHint: Object.keys(templates).join('|'),
valueHint: KEBAB_CASE_TEMPLATE_NAMES.join('|'),
description: `Template type to scaffold`,
},
name: {
Expand All @@ -37,15 +40,9 @@ export default defineCommand({
const cwd = resolve(ctx.args.cwd)

const templateName = ctx.args.template
const template = templates[templateName]
const ext = extname(ctx.args.name)
const name
= ext === '.vue' || ext === '.ts'
? ctx.args.name.replace(ext, '')
: ctx.args.name

// Validate template name
if (!template) {
if (!Object.keys(KEBAB_CASE_TEMPLATE_NAMES).includes(templateName)) {
logger.error(
`Template ${templateName} is not supported. Possible values: ${Object.keys(
templates,
Expand All @@ -55,6 +52,12 @@ export default defineCommand({
}

// Validate options
const ext = extname(ctx.args.name)
const name
= ext === '.vue' || ext === '.ts'
? ctx.args.name.replace(ext, '')
: ctx.args.name

if (!name) {
logger.error('name argument is missing!')
process.exit(1)
Expand All @@ -65,6 +68,8 @@ export default defineCommand({
const config = await kit.loadNuxtConfig({ cwd })

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

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

// Ensure not overriding user code
Expand Down
155 changes: 0 additions & 155 deletions src/utils/templates.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/utils/templates/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Template } from '.'
import { resolve } from 'pathe'
import { applySuffix } from '.'

const httpMethods = [
'connect',
'delete',
'get',
'head',
'options',
'post',
'put',
'trace',
'patch',
]

const api: Template = ({ name, args, nuxtOptions }) => {
return {
path: resolve(nuxtOptions.srcDir, nuxtOptions.serverDir, `api/${name}${applySuffix(args, httpMethods, 'method')}.ts`),
contents: `
export default defineEventHandler(event => {
return 'Hello ${name}'
})
`,
}
}

export { api }
11 changes: 11 additions & 0 deletions src/utils/templates/app-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Template } from '.'
import { resolve } from 'pathe'

const appConfig: Template = ({ nuxtOptions }) => ({
path: resolve(nuxtOptions.srcDir, 'app.config.ts'),
contents: `
export default defineAppConfig({})
`,
})

export { appConfig }
33 changes: 33 additions & 0 deletions src/utils/templates/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { Template } from '.'
import { resolve } from 'pathe'

const app: Template = ({ args, nuxtOptions }) => ({
path: resolve(nuxtOptions.srcDir, 'app.vue'),
contents: args.pages
? `
<script setup lang="ts"></script>
<template>
<div>
<NuxtLayout>
<NuxtPage/>
</NuxtLayout>
</div>
</template>
<style scoped></style>
`
: `
<script setup lang="ts"></script>
<template>
<div>
<h1>Hello World!</h1>
</div>
</template>
<style scoped></style>
`,
})

export { app }
24 changes: 24 additions & 0 deletions src/utils/templates/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Template } from '.'
import { resolve } from 'pathe'
import { applySuffix } from '.'

const component: Template = ({ name, args, nuxtOptions }) => ({
path: resolve(nuxtOptions.srcDir, `components/${name}${applySuffix(
args,
['client', 'server'],
'mode',
)}.vue`),
contents: `
<script setup lang="ts"></script>
<template>
<div>
Component: ${name}
</div>
</template>
<style scoped></style>
`,
})

export { component }
19 changes: 19 additions & 0 deletions src/utils/templates/composable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Template } from '.'
import { resolve } from 'pathe'
import { pascalCase } from 'scule'

const composable: Template = ({ name, nuxtOptions }) => {
const nameWithoutUsePrefix = name.replace(/^use-?/, '')
const nameWithUsePrefix = `use${pascalCase(nameWithoutUsePrefix)}`

return {
path: resolve(nuxtOptions.srcDir, `composables/${name}.ts`),
contents: `
export const ${nameWithUsePrefix} = () => {
return ref()
}
`,
}
}

export { composable }
26 changes: 26 additions & 0 deletions src/utils/templates/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Template } from '.'
import { resolve } from 'pathe'

const error: Template = ({ nuxtOptions }) => ({
path: resolve(nuxtOptions.srcDir, 'error.vue'),
contents: `
<script setup lang="ts">
import type { NuxtError } from '#app'
const props = defineProps({
error: Object as () => NuxtError
})
</script>
<template>
<div>
<h1>{{ error.statusCode }}</h1>
<NuxtLink to="/">Go back home</NuxtLink>
</div>
</template>
<style scoped></style>
`,
})

export { error }
Loading

0 comments on commit af2ea90

Please sign in to comment.