mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 17:07:22 +00:00
dfdd466270
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { relative, dirname, resolve } from 'path'
|
|
import { readFile, writeFile, mkdirp } from 'fs-extra'
|
|
import jiti from 'jiti'
|
|
|
|
const pwd = process.cwd()
|
|
|
|
export const hl = (str: string) => '`' + str + '`'
|
|
|
|
export function prettyPath (p: string, highlight = true) {
|
|
p = relative(pwd, p)
|
|
return highlight ? hl(p) : p
|
|
}
|
|
|
|
export async function loadTemplate (src: string) {
|
|
const contents = await readFile(src, 'utf-8')
|
|
return (params: Record<string, string>) => contents.replace(/{{ (\w+) }}/g, `${params.$1}`)
|
|
}
|
|
|
|
export async function renderTemplate (src: string, dst: string, params: any) {
|
|
const tmpl = await loadTemplate(src)
|
|
const rendered = tmpl(params)
|
|
await mkdirp(dirname(dst))
|
|
await writeFile(dst, rendered)
|
|
}
|
|
|
|
export async function compileTemplateToJS (src: string, dst: string) {
|
|
const contents = await readFile(src, 'utf-8')
|
|
// eslint-disable-next-line no-template-curly-in-string
|
|
const compiled = `export default (params) => \`${contents.replace(/{{ (\w+) }}/g, '${params.$1}')}\``
|
|
await mkdirp(dirname(dst))
|
|
await writeFile(dst, compiled)
|
|
}
|
|
|
|
export const jitiImport = (dir: string, path: string) => jiti(dir)(path)
|
|
export const tryImport = (dir: string, path: string) => { try { return jitiImport(dir, path) } catch (_err) { } }
|
|
|
|
export const LIB_DIR = resolve(__dirname, '../lib')
|
|
export const RUNTIME_DIR = resolve(LIB_DIR, 'runtime')
|