Last active
August 29, 2015 14:25
-
-
Save jbgutierrez/0cfcf93995db69740884 to your computer and use it in GitHub Desktop.
Hire/fire a webpack watcher after detecting new files in the file system
This file contains 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
#!/usr/bin/env coffee | |
os = require 'os' | |
chokidar = require './nosync/node_modules/chokidar' | |
spawn = require('child_process').spawn | |
watcher = null | |
debounce = (fn, delay=100) -> | |
timer = null | |
-> | |
context = this | |
args = arguments | |
clearTimeout timer | |
timer = setTimeout((-> | |
fn.apply context, args | |
), delay) | |
start = -> | |
console.log "hiring new watcher" | |
cmd = 'webpack' | |
cmd += '.cmd' if ~['win32', 'win64'].indexOf os.platform() | |
watcher = spawn cmd, ['--watch', '--watch-polling'], stdio: 'inherit' | |
watcher.on 'close', (code, signal) -> | |
console.log "child process terminated due to receipt of signal #{signal}" | |
start() | |
restart = | |
debounce -> | |
if watcher | |
console.log "firing #{watcher.pid}" | |
watcher.kill() | |
else | |
start() | |
chokidar.watch('modules/versions').on 'add', (path) -> | |
console.log "file #{path} has been added." | |
restart() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment