Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function read (flags = {}) {
// do not cache and clesar require cache, because of incremental builds
const explorer = cosmiconfig('uiengine', { cache: false, rcExtensions: true })
const configPath = resolvePath(process.cwd(), flags.config || 'uiengine.config.js')
try {
invalidateRequireCache(configPath)
const result = await explorer.load(configPath)
if (result) {
return _read(result.filepath, result.config, flags)
} else {
throw new UiengineInputError(`No configuration found. Please specify it in "${configPath}".`)
}
} catch (err) {
throw new UiengineInputError('Could not read UIengine configuration!', err)
}
}
async function read (flags = {}) {
// do not cache and clesar require cache, because of incremental builds
const explorer = cosmiconfig('uiengine', { cache: false, rcExtensions: true })
const configPath = resolvePath(process.cwd(), flags.config || 'uiengine.config.js')
try {
invalidateRequireCache(configPath)
const result = await explorer.load(configPath)
if (result) {
return _read(result.filepath, result.config, flags)
} else {
throw new UiengineInputError(`No configuration found. Please specify it in "${configPath}".`)
}
} catch (err) {
throw new UiengineInputError('Could not read UIengine configuration!', err)
}
}
async function generatePageWithTemplate (state, pageId) {
debug2(state, `Builder.generatePageWithTemplate(${pageId}):start`)
const { pages, config } = state
const { name, target, themes, version } = config
const page = pages[pageId]
if (!page) {
throw new UiengineInputError(`Page "${pageId}" does not exist or has not been fetched yet.`)
}
if (page.template || page.fragment) {
const template = page.template || config.template
const { id, context, fragment } = page
await withThemes(themes, async themeId => {
let { rendered, foot } = await render(state, template, context, themeId, pageId)
const content = fragment
? (await render(state, fragment, context, themeId, pageId)).rendered
: rendered
rendered = replaceTemplateComments(rendered, {
class: `uie-page uie-page--${dasherize(id)}`,
title: `${page.title} • ${name} (${version})`,
theme: themeId,
async function render (state, template, data, themeId, identifier) {
debug4(state, `Builder.render(${template}, ${themeId}, ${identifier}):start`)
const { templates } = state.config.source
if (!templates) throw new UiengineInputError('Templates source directory must be defined!')
const templatePath = join(templates, template)
let rendered
try {
rendered = await Connector.render(state, templatePath, data, themeId, identifier)
} catch (err) {
const message = [`${identifier} could not be generated!`]
if (state.config.debug) message.push(markSample(JSON.stringify(data, null, 2)))
throw new UiengineInputError(message, err)
}
debug4(state, `Builder.render(${template}, ${themeId}, ${identifier}):end`)
return rendered
async function render (state, template, data, themeId, identifier) {
debug4(state, `Builder.render(${template}, ${themeId}, ${identifier}):start`)
const { templates } = state.config.source
if (!templates) throw new UiengineInputError('Templates source directory must be defined!')
const templatePath = join(templates, template)
let rendered
try {
rendered = await Connector.render(state, templatePath, data, themeId, identifier)
} catch (err) {
const message = [`${identifier} could not be generated!`]
if (state.config.debug) message.push(markSample(JSON.stringify(data, null, 2)))
throw new UiengineInputError(message, err)
}
debug4(state, `Builder.render(${template}, ${themeId}, ${identifier}):end`)
return rendered
}
async function render (state, templatePath, data = {}, themeId, identifier) {
const ext = extension(templatePath)
const { render: renderFn } = getModule(state, ext, templatePath)
if (typeof renderFn === 'function') {
const options = getOptions(state, ext, { themeId })
const renderId = `${themeId}/${identifier}`
const rendered = await renderFn(options, templatePath, data, renderId)
return typeof rendered === 'string' ? { rendered } : rendered
} else {
throw new UiengineInputError(`The "${ext}" adapter does not support rendering.`)
}
}
const getModule = ({ config: { adapters } }, ext, filePath) => {
const { module } = adapters[ext] || {}
if (!module) {
throw new UiengineInputError(`Cannot handle ${filePath}: No "${ext}" adapter configured.`)
}
try {
return require(module)
} catch (err) {
throw new UiengineInputError(`Cannot load "${ext}" adapter.`, err)
}
}