@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
35 lines (30 loc) • 1.18 kB
JavaScript
const Mustache = require('mustache')
const cds = require('../cds')
const { copy, read, write, exists, readdir, path, isdir } = cds.utils
async function renderAndCopy(from, into, replacements, overwrite = cds.cli.options.force) {
const dirents = await readdir(from)
await Promise.all(dirents.map(async dirent => {
const fileNew = Mustache.render(dirent, replacements)
const src = path.join(from, dirent)
const dest = path.join(into, fileNew)
if (isdir(src)) {
return renderAndCopy(path.join(from, dirent), dest, replacements, overwrite)
} else if (path.extname(dirent) === '.hbs') {
const destinationPath = dest.replace('.hbs', '')
if (overwrite || !exists(destinationPath)) {
const content = await render(src, replacements)
await write(destinationPath, content, { spaces: 2 })
}
} else {
return copy(src, dest)
}
}))
}
async function render(src, templateValues) {
const content = await read(src, 'utf-8')
return Mustache.render(content, templateValues)
}
module.exports = {
render,
renderAndCopy
}