2022-04-15 15:19:05 +00:00
|
|
|
import { existsSync } from 'node:fs'
|
2021-11-21 16:14:46 +00:00
|
|
|
import { basename, parse, resolve } from 'pathe'
|
|
|
|
import hash from 'hash-sum'
|
2022-08-22 10:12:02 +00:00
|
|
|
import type { NuxtTemplate, ResolvedNuxtTemplate } from '@nuxt/schema'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { tryUseNuxt, useNuxt } from './context'
|
2021-11-21 16:14:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders given template using lodash template during build into the project buildDir
|
|
|
|
*/
|
2022-08-22 10:12:02 +00:00
|
|
|
export function addTemplate (_template: NuxtTemplate<any> | string) {
|
2021-11-21 16:14:46 +00:00
|
|
|
const nuxt = useNuxt()
|
|
|
|
|
2022-08-22 10:12:02 +00:00
|
|
|
// Normalize template
|
2021-11-21 16:14:46 +00:00
|
|
|
const template = normalizeTemplate(_template)
|
|
|
|
|
|
|
|
// Remove any existing template with the same filename
|
|
|
|
nuxt.options.build.templates = nuxt.options.build.templates
|
|
|
|
.filter(p => normalizeTemplate(p).filename !== template.filename)
|
|
|
|
|
|
|
|
// Add to templates array
|
|
|
|
nuxt.options.build.templates.push(template)
|
|
|
|
|
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2023-06-09 21:24:03 +00:00
|
|
|
/**
|
|
|
|
* Renders given types using lodash template during build into the project buildDir
|
|
|
|
* and register them as types.
|
|
|
|
*/
|
|
|
|
export function addTypeTemplate (_template: NuxtTemplate<any>) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
|
|
|
|
const template = addTemplate(_template)
|
|
|
|
|
|
|
|
if (!template.filename.endsWith('.d.ts')) {
|
|
|
|
throw new Error(`Invalid type template. Filename must end with .d.ts : "${template.filename}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add template to types reference
|
|
|
|
nuxt.hook('prepare:types', ({ references }) => {
|
|
|
|
references.push({ path: template.dst })
|
|
|
|
})
|
|
|
|
|
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:14:46 +00:00
|
|
|
/**
|
|
|
|
* Normalize a nuxt template object
|
|
|
|
*/
|
2022-08-22 10:12:02 +00:00
|
|
|
export function normalizeTemplate (template: NuxtTemplate<any> | string): ResolvedNuxtTemplate<any> {
|
2021-11-21 16:14:46 +00:00
|
|
|
if (!template) {
|
|
|
|
throw new Error('Invalid template: ' + JSON.stringify(template))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize
|
|
|
|
if (typeof template === 'string') {
|
|
|
|
template = { src: template }
|
|
|
|
} else {
|
|
|
|
template = { ...template }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use src if provided
|
|
|
|
if (template.src) {
|
|
|
|
if (!existsSync(template.src)) {
|
|
|
|
throw new Error('Template not found: ' + template.src)
|
|
|
|
}
|
|
|
|
if (!template.filename) {
|
|
|
|
const srcPath = parse(template.src)
|
2022-11-16 02:26:35 +00:00
|
|
|
template.filename = (template as any).fileName ||
|
2021-11-21 16:14:46 +00:00
|
|
|
`${basename(srcPath.dir)}.${srcPath.name}.${hash(template.src)}${srcPath.ext}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!template.src && !template.getContents) {
|
|
|
|
throw new Error('Invalid template. Either getContents or src options should be provided: ' + JSON.stringify(template))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!template.filename) {
|
|
|
|
throw new Error('Invalid template. Either filename should be provided: ' + JSON.stringify(template))
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:20:01 +00:00
|
|
|
// Always write declaration files
|
|
|
|
if (template.filename.endsWith('.d.ts')) {
|
|
|
|
template.write = true
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:14:46 +00:00
|
|
|
// Resolve dst
|
|
|
|
if (!template.dst) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
template.dst = resolve(nuxt.options.buildDir, template.filename)
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:12:02 +00:00
|
|
|
return template as ResolvedNuxtTemplate<any>
|
2021-11-21 16:14:46 +00:00
|
|
|
}
|
2022-10-24 08:53:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger rebuilding Nuxt templates
|
|
|
|
*
|
|
|
|
* You can pass a filter within the options to selectively regenerate a subset of templates.
|
|
|
|
*/
|
2023-03-23 00:24:18 +00:00
|
|
|
export async function updateTemplates (options?: { filter?: (template: ResolvedNuxtTemplate<any>) => boolean }) {
|
|
|
|
return await tryUseNuxt()?.hooks.callHook('builder:generateApp', options)
|
2022-10-24 08:53:02 +00:00
|
|
|
}
|