2020-11-04 13:15:38 +00:00
|
|
|
import { relative, dirname, resolve } from 'path'
|
2020-11-03 22:14:32 +00:00
|
|
|
import { readFile, writeFile, mkdirp } from 'fs-extra'
|
2020-11-01 23:17:44 +00:00
|
|
|
import jiti from 'jiti'
|
|
|
|
|
2020-11-02 14:42:27 +00:00
|
|
|
const pwd = process.cwd()
|
|
|
|
|
2020-11-01 23:17:44 +00:00
|
|
|
export const hl = str => '`' + str + '`'
|
|
|
|
|
2020-11-03 22:14:32 +00:00
|
|
|
export function prettyPath (p, highlight = true) {
|
2020-11-02 14:42:27 +00:00
|
|
|
p = relative(pwd, p)
|
2020-11-01 23:17:44 +00:00
|
|
|
return highlight ? hl(p) : p
|
|
|
|
}
|
|
|
|
|
2020-11-03 22:14:32 +00:00
|
|
|
export async function loadTemplate (src) {
|
|
|
|
const contents = await readFile(src, 'utf-8')
|
|
|
|
return params => contents.replace(/{{ (\w+) }}/g, `${params.$1}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function renderTemplate (src, 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)
|
|
|
|
}
|
|
|
|
|
2020-11-04 13:15:38 +00:00
|
|
|
export const jitiImport = (dir, path) => jiti(dir)(path)
|
|
|
|
export const tryImport = (dir, path) => { try { return jitiImport(dir, path) } catch (_err) { } }
|
|
|
|
|
|
|
|
export const LIB_DIR = resolve(__dirname, '../lib')
|
|
|
|
export const RUNTIME_DIR = resolve(LIB_DIR, 'runtime')
|