2020-11-04 13:15:38 +00:00
|
|
|
import { relative, dirname, resolve } from 'path'
|
2020-11-10 18:19:24 +00:00
|
|
|
import fse from 'fs-extra'
|
2020-11-01 23:17:44 +00:00
|
|
|
import jiti from 'jiti'
|
2020-11-06 09:51:35 +00:00
|
|
|
import defu from 'defu'
|
2020-11-06 12:55:30 +00:00
|
|
|
import Hookable from 'hookable'
|
2020-11-10 18:19:24 +00:00
|
|
|
import consola from 'consola'
|
2020-11-06 13:46:17 +00:00
|
|
|
import { SLSOptions, UnresolvedPath, SLSTarget, SLSTargetFn, SLSConfig } from './config'
|
2020-11-01 23:17:44 +00:00
|
|
|
|
2020-11-06 09:51:35 +00:00
|
|
|
export function hl (str: string) {
|
|
|
|
return '`' + str + '`'
|
|
|
|
}
|
2020-11-01 23:17:44 +00:00
|
|
|
|
2020-11-05 12:26:00 +00:00
|
|
|
export function prettyPath (p: string, highlight = true) {
|
2020-11-06 09:51:35 +00:00
|
|
|
p = relative(process.cwd(), p)
|
2020-11-01 23:17:44 +00:00
|
|
|
return highlight ? hl(p) : p
|
|
|
|
}
|
|
|
|
|
2020-11-06 09:51:35 +00:00
|
|
|
export function compileTemplate (contents: string) {
|
|
|
|
return (params: Record<string, any>) => contents.replace(/{{ ?(\w+) ?}}/g, (_, match) => params[match] || '')
|
2020-11-03 22:14:32 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 09:51:35 +00:00
|
|
|
export function serializeTemplate (contents: string) {
|
2020-11-03 22:14:32 +00:00
|
|
|
// eslint-disable-next-line no-template-curly-in-string
|
2020-11-05 21:56:40 +00:00
|
|
|
return `export default (params) => \`${contents.replace(/{{ (\w+) }}/g, '${params.$1}')}\``
|
|
|
|
}
|
|
|
|
|
2020-11-06 09:51:35 +00:00
|
|
|
export function jitiImport (dir: string, path: string) {
|
|
|
|
return jiti(dir)(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function tryImport (dir: string, path: string) {
|
|
|
|
try {
|
|
|
|
return jitiImport(dir, path)
|
|
|
|
} catch (_err) { }
|
|
|
|
}
|
2020-11-04 13:15:38 +00:00
|
|
|
|
2020-11-10 18:19:24 +00:00
|
|
|
export async function writeFile (file, contents) {
|
|
|
|
await fse.mkdirp(dirname(file))
|
|
|
|
await fse.writeFile(file, contents, 'utf-8')
|
2020-11-10 22:38:22 +00:00
|
|
|
consola.info('Generated', prettyPath(file))
|
2020-11-10 18:19:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:23:24 +00:00
|
|
|
export function resolvePath (options: SLSOptions, path: UnresolvedPath, resolveBase: string = '') {
|
2020-11-06 09:51:35 +00:00
|
|
|
if (typeof path === 'function') {
|
|
|
|
path = path(options)
|
|
|
|
}
|
|
|
|
|
2020-11-06 13:46:17 +00:00
|
|
|
if (typeof path !== 'string') {
|
|
|
|
throw new TypeError('Invalid path: ' + path)
|
|
|
|
}
|
|
|
|
|
2020-11-06 09:51:35 +00:00
|
|
|
path = compileTemplate(path)(options)
|
|
|
|
|
|
|
|
return resolve(resolveBase, path)
|
2020-11-05 21:23:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 09:12:57 +00:00
|
|
|
export function detectTarget () {
|
|
|
|
if (process.env.NETLIFY) {
|
|
|
|
return 'netlify'
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:05:11 +00:00
|
|
|
if (process.env.NOW_BUILDER) {
|
2020-11-06 09:12:57 +00:00
|
|
|
return 'vercel'
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'node'
|
|
|
|
}
|
|
|
|
|
2020-11-06 13:46:17 +00:00
|
|
|
export function extendTarget (base: SLSTarget, target: SLSTarget): SLSTargetFn {
|
|
|
|
return (config: SLSConfig) => {
|
2020-11-06 12:55:30 +00:00
|
|
|
if (typeof target === 'function') {
|
2020-11-06 13:46:17 +00:00
|
|
|
target = target(config)
|
2020-11-06 12:55:30 +00:00
|
|
|
}
|
|
|
|
if (typeof base === 'function') {
|
2020-11-06 13:46:17 +00:00
|
|
|
base = base(config)
|
2020-11-06 12:55:30 +00:00
|
|
|
}
|
|
|
|
return defu({
|
|
|
|
hooks: Hookable.mergeHooks(base.hooks, target.hooks),
|
|
|
|
nuxtHooks: Hookable.mergeHooks(base.nuxtHooks as any, target.nuxtHooks as any)
|
|
|
|
}, target, base)
|
|
|
|
}
|
2020-11-06 09:51:35 +00:00
|
|
|
}
|