Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(add): add and restructure templates #379

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 0 additions & 139 deletions src/utils/templates.ts

This file was deleted.

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

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

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

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

const app: Template = ({ args }) => ({
path: `error.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 }
23 changes: 23 additions & 0 deletions src/utils/templates/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Template } from '.'
import { applySuffix } from '.'

const component: Template = ({ name, args }) => ({
path: `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 }
18 changes: 18 additions & 0 deletions src/utils/templates/composable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { upperFirst } from 'scule'
import type { Template } from '.'

const composable: Template = ({ name }) => {
const nameWithUsePrefix = name.startsWith('use')
? name
: `use${upperFirst(name)}`
return {
path: `composables/${name}.ts`,
contents: `
export const ${nameWithUsePrefix} = () => {
return ref()
}
`,
}
}

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

const error: Template = () => ({
path: `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 }
56 changes: 56 additions & 0 deletions src/utils/templates/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { api } from './api'
import { app } from './app'
import { component } from './component'
import { composable } from './composable'
import { error } from './error'
import { layout } from './layout'
import { middleware } from './middleware'
import { page } from './page'
import { plugin } from './plugin'
import { serverMiddleware } from './server-middleware'

const templates: Record<string, Template> = {
api,
app,
component,
composable,
error,
layout,
middleware,
page,
plugin,
'server-middleware': serverMiddleware,
}

function applySuffix(
args: TemplateOptions['args'],
suffixes: string[],
unwrapFrom?: string,
): string {
let suffix = ''

// --client
for (const s of suffixes) {
if (args[s]) {
suffix += '.' + s
}
}

// --mode=server
if (unwrapFrom && args[unwrapFrom] && suffixes.includes(args[unwrapFrom])) {
suffix += '.' + args[unwrapFrom]
}

return suffix
}

type TemplateOptions = {
name: string
args: Record<string, any>
}

type Template = {
(options: TemplateOptions): { path: string; contents: string }
}

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

const layout: Template = ({ name }) => ({
path: `layouts/${name}.vue`,
contents: `
<script setup lang="ts"></script>

<template>
<div>
Layout: ${name}
<slot />
</div>
</template>

<style scoped></style>
`,
})

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

const middleware: Template = ({ name, args }) => ({
path: `middleware/${name}${applySuffix(args, ['global'])}.ts`,
contents: `
export default defineNuxtRouteMiddleware((to, from) => {})
`,
})

export { middleware }
18 changes: 18 additions & 0 deletions src/utils/templates/page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Template } from '.'

const page: Template = ({ name }) => ({
path: `pages/${name}.vue`,
contents: `
<script setup lang="ts"></script>

<template>
<div>
Page: ${name}
</div>
</template>

<style scoped></style>
`,
})

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

const plugin: Template = ({ name, args }) => ({
path: `plugins/${name}${applySuffix(args, ['client', 'server'], 'mode')}.ts`,
contents: `
export default defineNuxtPlugin((nuxtApp) => {})
`,
})

export { plugin }
10 changes: 10 additions & 0 deletions src/utils/templates/server-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Template } from '.'

const serverMiddleware: Template = ({ name }) => ({
path: `server/middleware/${name}.ts`,
contents: `
export default defineEventHandler((event) => {})
`,
})

export { serverMiddleware }
Loading