2020-11-20 00:16:31 +00:00
|
|
|
import { relative, dirname, resolve } from 'upath'
|
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-20 00:16:31 +00:00
|
|
|
import chalk from 'chalk'
|
|
|
|
import { get } from 'dot-prop'
|
|
|
|
import type { SigmaPreset, SigmaInput } from '../context'
|
2020-11-01 23:17:44 +00:00
|
|
|
|
2020-11-14 13:05:09 +00:00
|
|
|
export const MODULE_DIR = resolve(__dirname, '..')
|
|
|
|
|
2020-11-06 09:51:35 +00:00
|
|
|
export function hl (str: string) {
|
2020-11-20 00:16:31 +00:00
|
|
|
return chalk.cyan(str)
|
2020-11-06 09:51:35 +00:00
|
|
|
}
|
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) {
|
2020-11-20 00:16:31 +00:00
|
|
|
return (params: Record<string, any>) => contents.replace(/{{ ?([\w.]+) ?}}/g, (_, match) => {
|
|
|
|
const val = get(params, match)
|
|
|
|
if (!val) {
|
|
|
|
consola.warn(`cannot resolve template param '${match}' in ${contents.substr(0, 20)}`)
|
|
|
|
}
|
|
|
|
return val as string || `${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-14 13:05:09 +00:00
|
|
|
return `(params) => \`${contents.replace(/{{ (\w+) }}/g, '${params.$1}')}\``
|
2020-11-05 21:56:40 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-01-20 14:37:21 +00:00
|
|
|
export async function writeFile (file, contents, log = false) {
|
2020-11-10 18:19:24 +00:00
|
|
|
await fse.mkdirp(dirname(file))
|
|
|
|
await fse.writeFile(file, contents, 'utf-8')
|
2021-01-20 14:37:21 +00:00
|
|
|
if (log) {
|
|
|
|
consola.info('Generated', prettyPath(file))
|
|
|
|
}
|
2020-11-10 18:19:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 00:16:31 +00:00
|
|
|
export function resolvePath (sigmaContext: SigmaInput, path: string | ((sigmaContext) => string), resolveBase: string = ''): string {
|
2020-11-06 09:51:35 +00:00
|
|
|
if (typeof path === 'function') {
|
2020-11-20 00:16:31 +00:00
|
|
|
path = path(sigmaContext)
|
2020-11-06 09:51:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 13:46:17 +00:00
|
|
|
if (typeof path !== 'string') {
|
|
|
|
throw new TypeError('Invalid path: ' + path)
|
|
|
|
}
|
|
|
|
|
2020-11-20 00:16:31 +00:00
|
|
|
path = compileTemplate(path)(sigmaContext)
|
2020-11-06 09:51:35 +00:00
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 12:48:29 +00:00
|
|
|
export async function isDirectory (path: string) {
|
|
|
|
try {
|
|
|
|
return (await fse.stat(path)).isDirectory()
|
|
|
|
} catch (_err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 00:16:31 +00:00
|
|
|
export function extendPreset (base: SigmaPreset, preset: SigmaPreset): SigmaPreset {
|
|
|
|
return (config: SigmaInput) => {
|
|
|
|
if (typeof preset === 'function') {
|
|
|
|
preset = preset(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({
|
2020-11-20 00:16:31 +00:00
|
|
|
hooks: Hookable.mergeHooks(base.hooks, preset.hooks)
|
|
|
|
}, preset, base)
|
2020-11-06 12:55:30 +00:00
|
|
|
}
|
2020-11-06 09:51:35 +00:00
|
|
|
}
|
2020-11-14 13:05:09 +00:00
|
|
|
|
|
|
|
const _getDependenciesMode = {
|
|
|
|
dev: ['devDependencies'],
|
|
|
|
prod: ['dependencies'],
|
|
|
|
all: ['devDependencies', 'dependencies']
|
|
|
|
}
|
|
|
|
export function getDependencies (dir: string, mode: keyof typeof _getDependenciesMode = 'all') {
|
|
|
|
const fields = _getDependenciesMode[mode]
|
|
|
|
const pkg = require(resolve(dir, 'package.json'))
|
|
|
|
const dependencies = []
|
|
|
|
for (const field of fields) {
|
|
|
|
if (pkg[field]) {
|
|
|
|
for (const name in pkg[field]) {
|
|
|
|
dependencies.push(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dependencies
|
|
|
|
}
|