2022-03-25 14:56:36 +00:00
|
|
|
import { upperFirst } from 'scule'
|
|
|
|
|
2022-08-23 20:44:25 +00:00
|
|
|
interface TemplateOptions {
|
|
|
|
name: string,
|
|
|
|
args: Record<string, any>
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:56:36 +00:00
|
|
|
interface Template {
|
2022-08-23 20:44:25 +00:00
|
|
|
(options: TemplateOptions): { path: string, contents: string }
|
2022-03-25 14:56:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:44:25 +00:00
|
|
|
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`,
|
2022-03-25 14:56:36 +00:00
|
|
|
contents: `
|
2022-04-07 21:25:39 +00:00
|
|
|
export default defineEventHandler((event) => {
|
2022-03-25 14:56:36 +00:00
|
|
|
return 'Hello ${name}'
|
|
|
|
})
|
|
|
|
`
|
|
|
|
})
|
|
|
|
|
2022-08-23 20:44:25 +00:00
|
|
|
const plugin: Template = ({ name, args }) => ({
|
|
|
|
path: `plugins/${name}${applySuffix(args, ['client', 'server'], 'mode')}.ts`,
|
2022-03-25 14:56:36 +00:00
|
|
|
contents: `
|
|
|
|
export default defineNuxtPlugin((nuxtApp) => {})
|
|
|
|
`
|
|
|
|
})
|
|
|
|
|
2022-08-23 20:44:25 +00:00
|
|
|
const component: Template = ({ name, args }) => ({
|
|
|
|
path: `components/${name}${applySuffix(args, ['client', 'server'], 'mode')}.vue`,
|
2022-03-25 14:56:36 +00:00
|
|
|
contents: `
|
|
|
|
<script lang="ts" setup></script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
Component: ${name}
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style scoped></style>
|
|
|
|
`
|
|
|
|
})
|
|
|
|
|
|
|
|
const composable: Template = ({ name }) => {
|
|
|
|
const nameWithUsePrefix = name.startsWith('use') ? name : `use${upperFirst(name)}`
|
|
|
|
return {
|
|
|
|
path: `composables/${name}.ts`,
|
|
|
|
contents: `
|
|
|
|
export const ${nameWithUsePrefix} = () => {
|
|
|
|
return ref()
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:44:25 +00:00
|
|
|
const middleware: Template = ({ name, args }) => ({
|
|
|
|
path: `middleware/${name}${applySuffix(args, ['global'])}.ts`,
|
2022-03-25 14:56:36 +00:00
|
|
|
contents: `
|
|
|
|
export default defineNuxtRouteMiddleware((to, from) => {})
|
|
|
|
`
|
|
|
|
})
|
|
|
|
|
|
|
|
const layout: Template = ({ name }) => ({
|
|
|
|
path: `layouts/${name}.vue`,
|
|
|
|
contents: `
|
|
|
|
<script lang="ts" setup></script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
Layout: ${name}
|
|
|
|
<slot />
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style scoped></style>
|
|
|
|
`
|
|
|
|
})
|
|
|
|
|
|
|
|
const page: Template = ({ name }) => ({
|
|
|
|
path: `pages/${name}.vue`,
|
|
|
|
contents: `
|
|
|
|
<script lang="ts" setup></script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
Page: foo
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style scoped></style>
|
|
|
|
`
|
|
|
|
})
|
|
|
|
|
|
|
|
export const templates = {
|
|
|
|
api,
|
|
|
|
plugin,
|
|
|
|
component,
|
|
|
|
composable,
|
|
|
|
middleware,
|
|
|
|
layout,
|
|
|
|
page
|
|
|
|
} as Record<string, Template>
|
2022-08-23 20:44:25 +00:00
|
|
|
|
|
|
|
// -- internal utils --
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|