Created
April 19, 2017 11:31
-
-
Save akx/6869f56901e8ed962e6b68cfbf253293 to your computer and use it in GitHub Desktop.
lofi task runner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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