Skip to content

Commit

Permalink
Add a sub-command to render partials.
Browse files Browse the repository at this point in the history
This is supposed to contribute to
usebedrock#391.
  • Loading branch information
thusc committed Jul 14, 2021
1 parent addc20c commit d51be49
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/cli/bedrock
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ program
program
.command('styleguide', 'Commands related to the styleguide content');

program
.command('components', 'Commands related to the styleguide components');

program
.parse(process.argv);
49 changes: 49 additions & 0 deletions core/cli/bedrock-components
Original file line number Diff line number Diff line change
@@ -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);

This comment has been minimized.

Copy link
@Wolfr

Wolfr Jul 14, 2021

How does this file know about the defined gulp tasks?

This comment has been minimized.

Copy link
@thusc

thusc Jul 14, 2021

Author Owner

This line actually "defines" it as far as I understand, and the right hand side templates.compile.styleguide is simply required at line 10 above. That being said, I think the content of Gulpfile could be moved inside core so that there is no need to redefine it, and just calling it would be enough (the line gulp.series()() below).

This comment has been minimized.

Copy link
@Wolfr

Wolfr Jul 14, 2021

Right OK - I read this a bit too fast, indeed, this is the initial definition.

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);
1 change: 1 addition & 0 deletions core/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ module.exports = {
},
styleguide: path.join(distPath, 'styleguide/'),
docs: path.join(distPath, 'styleguide/docs/'),
partials: path.join(distPath, 'styleguide/partials/'),
assets: {
images: path.join(distPath, 'images/'),
fonts: path.join(distPath, 'fonts/'),
Expand Down
41 changes: 41 additions & 0 deletions core/tasks/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,54 @@ 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/styleguide']).then(function () {
done();
});
},
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();

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"run-sequence": "^2.2.1"
},
"dependencies": {
"commander": "^2.20.3"
"commander": "^2.20.3",
"through2": "^4.0.2"
}
}

0 comments on commit d51be49

Please sign in to comment.