Skip to content

Commit

Permalink
Merge branch 'fracmak-chokidar'
Browse files Browse the repository at this point in the history
  • Loading branch information
napcs committed Oct 26, 2015
2 parents 87572cd + 771f744 commit c60a718
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 103 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ You can also use this with a Connect server:

Watching multiple paths:

Passing an array of paths will allow you to watch multiple directories. All directories have the same configuration options.
Passing an array of paths or glob patterns will allow you to watch multiple directories. All directories have the same configuration options.

```js
server.watch([__dirname + "/js", __dirname + "/css"]);
Expand Down
55 changes: 18 additions & 37 deletions lib/livereload.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ws = require 'ws'
http = require 'http'
https = require 'https'
url = require 'url'
chokidar = require 'chokidar'

protocol_version = '1.6'
defaultPort = 35729
Expand Down Expand Up @@ -67,44 +68,24 @@ class Server
onClose: (socket) ->
@debug "Browser disconnected."

walkTree: (dirname, callback) ->
exts = @config.exts
exclusions = @config.exclusions

walk = (dirname) ->
fs.readdir dirname, (err, files) ->
if err then return callback err

files.forEach (file) ->
filename = path.join dirname, file

for exclusion in exclusions
return if filename.match exclusion

fs.stat filename, (err, stats) ->
if !err and stats.isDirectory()
walk filename
else
for ext in exts when filename.match "\\.#{ext}$"
callback err, filename
break

walk dirname, callback

watch: (dirname) ->
dirname = [dirname] if typeof dirname is "string"

dirname.forEach (dir) =>
@walkTree dir, (err, filename) =>
throw err if err
fs.watchFile filename, {interval: @config.interval}, (curr, prev) =>
@refresh filename if curr.mtime > prev.mtime

refresh: (path) ->
if (@config.debug)
@debug "Refresh: #{path}"
watch: (paths) ->
@watcher = chokidar.watch paths, {ignoreInitial: true, ignored: @config.exclusions}
@watcher.on 'add', (path) => @filterRefresh path
@watcher.on 'change', (path) => @filterRefresh path
@watcher.on 'unlink', (path) => @filterRefresh path

filterRefresh: (filepath) ->
exts = @config.exts
fileext = path.extname filepath
.substring 1
for ext in exts when ext == fileext
@refresh filepath
break

refresh: (filepath) ->
@debug "Refresh: #{filepath}"
data = JSON.stringify ['refresh',
path: path,
path: filepath,
apply_js_live: @config.applyJSLive,
apply_css_live: @config.applyCSSLive,
apply_img_live: @config.applyImgLive,
Expand Down
105 changes: 40 additions & 65 deletions lib/livereload.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
, "bin": { "livereload": "./bin/livereload.js" }
, "main": "./lib/livereload.js"
, "dependencies": {
"chokidar": "^1.1.0",
"opts": ">= 1.2.0",
"ws": "^0.8.0"
}
Expand Down

0 comments on commit c60a718

Please sign in to comment.