Skip to content

Instantly share code, notes, and snippets.

@replete
Created December 28, 2016 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save replete/9ce1a75f03ab331cacb28cfb2b5f6416 to your computer and use it in GitHub Desktop.
Save replete/9ce1a75f03ab331cacb28cfb2b5f6416 to your computer and use it in GitHub Desktop.
Basic task runner - replace gulp with CLI
/**
Beginnings of ES6 node Task runner to replace gulp.
This example uses PostCSS and Pug to process CSS and HTML templates.
*/
// Global dependencies
const fs = require('fs-extra');
const bs = require('browser-sync').create();
const glob = require('glob');
// Local dependencies
const pug = require('pug');
const postCss = require('postcss');
const postCssPlugins = ["precss","postcss-cssnext","postcss-utilities"].map(require);
const generators = {
html: {
src: 'src/*.pug',
make: function(inpath) {
// Set output file path
var outpath = inpath.replace('src','dist').replace('pug','html');
// Save file
fs.outputFile(outpath, pug.renderFile(inpath));
// Reload page
bs.reload();
}
},
styles: {
src: 'src/styles/**/*.postcss',
make: function(inpath) {
var outpath = inpath.replace('src','dist').replace('postcss','css');
postCss(postCssPlugins)
.process(fs.readFile(inpath), { from: inpath, to: outpath })
.then(function(result) {
// Save file
fs.outputFile(outpath, result.css);
// Write sourcemaps
if ( result.map ) fs.outputFile(outpath + '.map', result.map);
// Reload CSS in browser
bs.reload(outpath.split('/').pop());
});
}
}
}
const tasks = {
build: function(){
for (let gen in generators) {
log('build:' + gen);
glob(generators[gen].src).forEach((f) => generators[gen].make(f));
}
},
watch: function() {
let watched = ['styles'];
log('test');
for (let gen in watched) {
gen = watched[gen];
bs.watch(generators[gen].src).on('change', generators[gen].make);
}
tasks.serve();
},
serve: function() {
log('serve');
bs.init({
server: "./dist"
});
}
}
function log(task, text) {
text = text || '';
console.log(`[${task}] ${text}`)
}
// series task runner
process.argv.splice(2).forEach((taskName) => {
if (tasks[taskName]) {
log(taskName, 'Started...');
tasks[taskName]();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment