mirror of
https://github.com/boostorg/unordered-ui-bundle.git
synced 2026-01-19 04:42:15 +00:00
- upgrade to Gulp 4 - refactor tasks to use Gulp 4 task system - switch from map-stream to through2.obj - switch from fs to fs-extra - fix Gulp prettier+eslint integration - rename tasks
86 lines
2.5 KiB
JavaScript
86 lines
2.5 KiB
JavaScript
'use strict'
|
|
|
|
const fs = require('fs-extra')
|
|
const handlebars = require('handlebars')
|
|
const { obj: map } = require('through2')
|
|
const path = require('path')
|
|
const requireFromString = require('require-from-string')
|
|
const vfs = require('vinyl-fs')
|
|
const yaml = require('js-yaml')
|
|
|
|
module.exports = (src, dest, siteSrc, siteDest, onComplete) => () =>
|
|
Promise.all([
|
|
loadSampleUiModel(siteSrc),
|
|
compileLayouts(src),
|
|
registerPartials(src),
|
|
registerHelpers(src),
|
|
]).then(([uiModel, layouts]) =>
|
|
vfs
|
|
.src('**/*.html', { base: siteSrc, cwd: siteSrc })
|
|
.pipe(
|
|
map((file, enc, next) => {
|
|
const compiledLayout = layouts[file.stem === '404' ? '404.hbs' : 'default.hbs']
|
|
const siteRootPath = path.relative(path.dirname(file.path), path.resolve(siteSrc))
|
|
uiModel.env = process.env
|
|
uiModel.siteRootPath = siteRootPath
|
|
uiModel.siteRootUrl = path.join(siteRootPath, 'index.html')
|
|
uiModel.uiRootPath = path.join(siteRootPath, '_')
|
|
uiModel.page.contents = file.contents.toString().trim()
|
|
file.contents = Buffer.from(compiledLayout(uiModel))
|
|
next(null, file)
|
|
})
|
|
)
|
|
.pipe(vfs.dest(siteDest))
|
|
.pipe(onComplete ? onComplete() : map((file, enc, next) => next()))
|
|
)
|
|
|
|
function loadSampleUiModel (siteSrc) {
|
|
return fs.readFile(path.join(siteSrc, 'ui-model.yml'), 'utf8').then((contents) => yaml.safeLoad(contents))
|
|
}
|
|
|
|
function registerPartials (src) {
|
|
return new Promise((resolve, reject) =>
|
|
vfs
|
|
.src('partials/*.hbs', { base: src, cwd: src })
|
|
.pipe(
|
|
map((file, enc, next) => {
|
|
handlebars.registerPartial(file.stem, file.contents.toString())
|
|
next()
|
|
})
|
|
)
|
|
.on('error', reject)
|
|
.on('finish', resolve)
|
|
)
|
|
}
|
|
|
|
function registerHelpers (src) {
|
|
return new Promise((resolve, reject) =>
|
|
vfs
|
|
.src('helpers/*.js', { base: src, cwd: src })
|
|
.pipe(
|
|
map((file, enc, next) => {
|
|
handlebars.registerHelper(file.stem, requireFromString(file.contents.toString()))
|
|
next()
|
|
})
|
|
)
|
|
.on('error', reject)
|
|
.on('finish', resolve)
|
|
)
|
|
}
|
|
|
|
function compileLayouts (src) {
|
|
const layouts = {}
|
|
return new Promise((resolve, reject) =>
|
|
vfs
|
|
.src('layouts/*.hbs', { base: src, cwd: src })
|
|
.pipe(
|
|
map((file, enc, next) => {
|
|
layouts[file.basename] = handlebars.compile(file.contents.toString(), { preventIndent: true })
|
|
next()
|
|
})
|
|
)
|
|
.on('error', reject)
|
|
.on('finish', () => resolve(layouts))
|
|
)
|
|
}
|