From 107461d61ca1f2ceaa073a254f396a263163c488 Mon Sep 17 00:00:00 2001 From: Vo Minh Thu Date: Tue, 13 Jul 2021 19:54:26 +0200 Subject: [PATCH] Add a sub-command to render partials. This is supposed to contribute to https://github.com/usebedrock/bedrock/issues/391. --- core/cli/bedrock | 3 +++ core/cli/bedrock-components | 49 +++++++++++++++++++++++++++++++++++++ core/paths.js | 1 + core/tasks/templates.js | 41 +++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100755 core/cli/bedrock-components diff --git a/core/cli/bedrock b/core/cli/bedrock index adf00be..658e351 100755 --- a/core/cli/bedrock +++ b/core/cli/bedrock @@ -18,5 +18,8 @@ program program .command('styleguide', 'Commands related to the styleguide content'); +program + .command('components', 'Commands related to the styleguide components'); + program .parse(process.argv); diff --git a/core/cli/bedrock-components b/core/cli/bedrock-components new file mode 100755 index 0000000..efb49c5 --- /dev/null +++ b/core/cli/bedrock-components @@ -0,0 +1,49 @@ +#! /usr/bin/env node + +'use strict'; + +const program = require('commander'); +const glob = require('glob'); +const gulp = require('gulp'); +const path = require('path'); + +const templates = require('../tasks/templates'); +const paths = require('../paths'); + + +program + .command('list') + .description('List the Pug templates under content/templates/_components') + .action(function () { + // glob.sync() is what gulp.src() uses, but the + console.log(glob.sync(paths.content.templates.allComponents, '**/*.pug')); + }); + +program + .command('build') + .description('Build the component Pug templates') + .action(function () { + gulp.task('templates:compile:styleguide', templates.compile.styleguide); + gulp.series('templates:compile:styleguide')(); + }); + +program + .command('partials') + .description('Build the component Pug templates as partial HTML') + .action(function () { + gulp.task('templates:compile:partials', templates.compile.partials); + gulp.series('templates:compile:partials')(); + }); + +program + .action(() => { + program.help() + }); + +if (process.argv.length === 2) { + program.help(); + process.exit(); +} + +program + .parse(process.argv); diff --git a/core/paths.js b/core/paths.js index bce30d9..4f0de35 100644 --- a/core/paths.js +++ b/core/paths.js @@ -108,6 +108,7 @@ module.exports = { }, styleguide: path.join(distPath, config.styleguide.url), docs: path.join(distPath, config.styleguide.url+'/docs/'), + partials: path.join(distPath, 'styleguide/partials/'), assets: { images: path.join(distPath, 'images/'), fonts: path.join(distPath, 'fonts/'), diff --git a/core/tasks/templates.js b/core/tasks/templates.js index 316b144..6b27231 100644 --- a/core/tasks/templates.js +++ b/core/tasks/templates.js @@ -32,6 +32,25 @@ function getDefaultLocals() { return defaultLocals; } +/* Add the user-defined _mixins/all and the Bedrock-provided icons mixins. + * This is done using the sample.pug wrapper template, also used to render + * the components in the style guide (using the `renderCode` function). + */ +function addMixins() { + return through.obj(function (vinylFile, encoding, callback) { + var outFile = vinylFile.clone(); + + const indentedPugMarkup = + vinylFile.contents.toString().split('\n').map(line => ` ${line}`).join('\n'); + const markupWithLayout = + `extends /core/templates/layouts/sample\n\nblock content\n${indentedPugMarkup}`; + + outFile.contents = new Buffer.from(markupWithLayout); + + callback(null, outFile); + }); +} + module.exports = { clean(done) { del(['./dist/**.html', './dist/modules', './dist'+config.styleguide.url]) @@ -40,6 +59,28 @@ module.exports = { }); }, compile: { + partials(done) { + return gulp.src(paths.content.templates.allComponents) + .pipe(data(function (file) { + return Object.assign({}, getDefaultLocals(), { + filename: path.basename(file.path).replace('pug', 'html'), + pathname: file.path.replace(path.join(process.cwd(), paths.content.templates.path), '').replace('.pug', ''), + }); + })) + .pipe(addMixins()) + .pipe(gulpPug(config.pug)) + .on('error', function (err) { + notifier.notify({ + title: 'Pug error', + message: err.message + }); + gutil.log(gutil.colors.red(err)); + gutil.beep(); + this.emit('end'); + }) + .pipe(prettify(config.prettify)) + .pipe(gulp.dest(paths.dist.partials)); + }, styleguide(done) { const defaultLocals = getDefaultLocals();