Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit 81e87fa33e9a4c62a3f4c1a292a1813be06a5ad6 @josh josh committed Nov 20, 2010
Showing with 310 additions and 0 deletions.
  1. +6 −0 Cakefile
  2. +20 −0 LICENSE
  3. +36 −0 README.md
  4. +96 −0 livereload.coffee
  5. +124 −0 livereload.js
  6. +18 −0 package.json
  7. +10 −0 server.coffee
@@ -0,0 +1,6 @@
+{print} = require 'sys'
+{spawn} = require 'child_process'
+
+task 'build', 'Build CoffeeScript source files', ->
+ coffee = spawn 'coffee', ['-cw', 'livereload.coffee']
+ coffee.stdout.on 'data', (data) -> print data.toString()
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Joshua Peek
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
+node-livereload
+===============
+
+An implementation of the LiveReload server in Node.js.
+
+Install the browser plugin and read more about the project at:
+
+ https://github.com/mockko/livereload
+
+# Example
+
+ livereload = require('livereload');
+ server = livereload.createServer();
+ server.watch(__dirname + "/public");
+
+Use with a connect server:
+
+ connect = require('connect');
+ connect.createServer(
+ connect.compiler({ src: __dirname + "/public", enable: ['less'] }),
+ connect.staticProvider(__dirname + "/public")
+ ).listen(3000);
+
+ livereload = require('livereload');
+ server = livereload.createServer(exts: ['less']);
+ server.watch(__dirname + "/public");
+
+# Installation
+
+ $ npm install livereload
+
+# License
+
+Copyright (c) 2010 Joshua Peek.
+
+Released under the MIT license. See `LICENSE` for details.
@@ -0,0 +1,96 @@
+fs = require 'fs'
+path = require 'path'
+ws = require 'websocket-server'
+
+version = '1.4'
+defaultPort = 35729
+
+defaultExts = [
+ 'html', 'css', 'js', 'png', 'gif', 'jpg',
+ 'php', 'php5', 'py', 'rb', 'erb'
+]
+
+defaultExclusions = ['.git/', '.svn/', '.hg/']
+
+class Server
+ constructor: (@config) ->
+ @config ?= {}
+
+ @config.version ?= version
+ @config.port ?= defaultPort
+
+ @config.exts ?= []
+ @config.exclusions ?= []
+
+ @config.exts = @config.exts.concat defaultExts
+ @config.exclusions = @config.exclusions.concat defaultExclusions
+
+ @config.applyJSLive ?= false
+ @config.applyCSSLive ?= true
+
+ @server = ws.createServer()
+
+ @server.on 'connection', @onConnection.bind @
+ @server.on 'close', @onClose.bind @
+
+ listen: ->
+ @debug "LiveReload is waiting for browser to connect."
+ @server.listen @config.port
+
+ onConnection: (connection) ->
+ @debug "Browser connected."
+ connection.write "!!ver:#{@config.version}"
+
+ connection.on 'message', (message) =>
+ @debug "Browser URL: #{message}"
+
+ onClose: (connection) ->
+ @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) ->
+ @walkTree dirname, (err, filename) =>
+ throw err if err
+ fs.watchFile filename, (curr, prev) =>
+ if curr.mtime > prev.mtime
+ @refresh filename
+
+ refresh: (path) ->
+ @debug "Refresh: #{path}"
+ @server.broadcast JSON.stringify ['refresh',
+ path: path,
+ apply_js_live: @config.applyJSLive,
+ apply_css_live: @config.applyCSSLive
+ ]
+
+ debug: (str) ->
+ if @config.debug
+ process.binding('stdio').writeError "#{str}\n"
+
+exports.createServer = (args...) ->
+ server = new Server args...
+ server.listen()
+ server

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,18 @@
+{ "name": "livereload"
+, "description": "LiveReload server"
+, "version": "0.1.0"
+, "author": "Joshua Peek"
+, "licenses": [
+ { "type": "MIT"
+ , "url": "https://github.com/josh/node-livereload/blob/master/LICENSE"
+ }]
+, "repository":
+ { "type": "git"
+ , "url": "http://github.com/josh/node-livereload.git"
+ }
+, "main": "./livereload.js"
+, "dependencies": {
+ "websocket-server": ">= 1.1.00"
+ }
+, "engines" : { "node" : ">=0.2.0" }
+}
@@ -0,0 +1,10 @@
+connect = require 'connect'
+
+connect.createServer connect.logger(format: ':method :url'),
+ connect.compiler(src: "#{__dirname}", enable: ['less']),
+ connect.staticProvider("#{__dirname}")
+.listen 3000
+
+livereload = require 'livereload'
+server = livereload.createServer debug: true, exts: ['less']
+server.watch "#{__dirname}"

0 comments on commit 81e87fa

Please sign in to comment.