Skip to content

Instantly share code, notes, and snippets.

@jbgutierrez
Last active August 29, 2015 14: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 jbgutierrez/0cfcf93995db69740884 to your computer and use it in GitHub Desktop.
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
#!/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