Skip to content

Instantly share code, notes, and snippets.

@akx
Created April 19, 2017 11:31
Show Gist options
  • Select an option

  • Save akx/6869f56901e8ed962e6b68cfbf253293 to your computer and use it in GitHub Desktop.

Select an option

Save akx/6869f56901e8ed962e6b68cfbf253293 to your computer and use it in GitHub Desktop.
lofi task runner
/* eslint-disable no-console, arrow-body-style, quote-props, no-param-reassign */
const chokidar = require('chokidar');
const compileCSS = require('./css');
const cp = require('child_process');
const debounce = require('lodash/debounce');
const fs = require('fs');
const path = require('path');
const Promise = require('bluebird');
function run(cmd, args = []) {
return new Promise((resolve, reject) => {
const child = cp.spawn(cmd, args, {shell: true});
child.on('close', (code) => {
if (code === 0) return resolve(child);
return reject(child);
});
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
});
}
function copyFAFonts() {
return run('cp', ['-v', './node_modules/font-awesome/fonts/*webfont*', 'fonts/']);
}
function compileWebpack() {
return run('./node_modules/.bin/webpack', []);
}
function watch(opts) {
const debouncedCompileCSS = debounce(compileCSS, 200);
chokidar.watch('**/*.less').on('change', () => debouncedCompileCSS(opts));
}
const taskDefinitions = {
'css': compileCSS,
'webpack': compileWebpack,
'copy-fa-fonts': copyFAFonts,
};
function main(args) {
const opts = {};
args = args.filter((arg) => {
const m = /^-+(.+)$/.exec(arg);
if (m) {
opts[m[1]] = true;
}
return !m;
});
let selectedTasks = args;
if (!selectedTasks.length) selectedTasks = Array.from(Object.keys(taskDefinitions));
const taskFns = selectedTasks.map((task) => taskDefinitions[task](opts));
Promise.all(taskFns).then(() => {
if (opts.watch) {
console.log('starting watch');
watch(opts);
}
});
}
if (!module.parent) {
main(process.argv.slice(2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment