From 4cf5f98b31cad2ce3e1338564bd197a0edfdedf4 Mon Sep 17 00:00:00 2001 From: Brian Hogan Date: Mon, 26 Oct 2015 15:25:15 -0500 Subject: [PATCH] Release 0.4.0 Prepare release 0.4.0 * Use `ws` library * Use `chokidar` library instead of interval polling. * Remove `-i` / `--interval` option * Better explain URL option * Add exclusions to command line API --- README.md | 39 ++++++++++++++++++++++++++++++++------- lib/command.coffee | 31 ++++++++++++++++++++++--------- lib/command.js | 34 ++++++++++++++++++++++------------ lib/livereload.coffee | 16 ++++++++-------- lib/livereload.js | 21 ++++++++++++--------- package.json | 2 +- 6 files changed, 97 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index adf0fe5..79eaf9c 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,33 @@ An implementation of the LiveReload server in Node.js. It's an alternative to th # Example Usage -First, install the LiveReload browser plugins by visiting [http://help.livereload.com/kb/general-use/browser-extensions](http://help.livereload.com/kb/general-use/browser-extensions). +You can use this by either adding a snippet of code to the bottom of your HTML pages **or** install the Browser Extensions. + +## Method 1: Add browser extension + +Install the LiveReload browser plugins by visiting [http://help.livereload.com/kb/general-use/browser-extensions](http://help.livereload.com/kb/general-use/browser-extensions). + +Only Google Chrome supports viewing `file:///` URLS, and you have to specifically enable it. If you are using other browsers and want to use `file:///` URLs, add the JS code to the page as shown in the next section. + +## Method 2: Add code to page + +Add this code: + +``` + +``` + +Note: If you are using a different port other than `35729` you will +need to change the above script. + +# Running LiveReload + +You can run LiveReload two ways: + +## Option 1: Command line To use livereload from the command line: @@ -14,11 +40,13 @@ To use livereload from the command line: $ livereload [path] -Or to use the api within a project: +## Option 2: From within your own project + +To use the api within a project: $ npm install livereload -Then, simply create a server and fire it up. +Then, create a server and fire it up. livereload = require('livereload'); server = livereload.createServer(); @@ -70,7 +98,6 @@ When `/User/Workspace/test/css/style.css` modified, the stylesheet will be reloa The commandline options are * `-p` or `--port` to specify the listening port -* `-i` or `--interval` to specify the listening interval in milliseconds. Default is 1000. * `-d` or `--debug` to show debug messages when the browser reloads. Specify the path when using the options. @@ -93,11 +120,9 @@ The `createServer()` method supports a few basic options, passed as a JavaScript * `applyImgLive` tells LiveReload to reload image files in the background instead of refreshing the page. The default for this is `true`. Namely for these extensions: jpg, jpeg, png, gif * `exclusions` lets you specify files to ignore. By default, this includes `.git/`, `.svn/`, and `.hg/` * `originalPath` Set URL you use for development, e.g 'http:/domain.com', then LiveReload will proxy this url to local path. -* `overrideURL` override the stylesheet href with your set. +* `overrideURL` lets you specify a different host for CSS files. This lets you edit local CSS files but view a live site. See for details. -# Limitations -Right now this is extremely simple. It relies on polling so there's a delay in refreshing the browser. It could be faster. # License diff --git a/lib/command.coffee b/lib/command.coffee index 10676df..700bc50 100644 --- a/lib/command.coffee +++ b/lib/command.coffee @@ -1,10 +1,20 @@ runner = -> - + pjson = require('../package.json') + version = pjson.version livereload = require './livereload' resolve = require('path').resolve opts = require 'opts' debug = false; opts.parse [ + { + short: "v" + long: "version" + description: "Show the version" + required: false + callback: -> + console.log version + process.exit(1) + } { short: "p" long: "port" @@ -13,11 +23,11 @@ runner = -> required: false } { - short: "i" - long: "interval" - description: "Specify the interval" + short: "x" + long: "exclusions" + description: "Exclude files by specifying an array of regular expressions. Will be appended to default value which is [/\\.git\//, /\\.svn\//, /\\.hg\//]", + required: false, value: true - required: false } { short: "d" @@ -29,14 +39,17 @@ runner = -> ].reverse(), true port = opts.get('port') || 35729 - interval = opts.get('interval') || 1000 + exclusions = opts.get('exclusions') || [] - server = livereload.createServer({port: port, interval: interval, debug: debug}) + server = livereload.createServer({ + port: port + debug: debug + exclusions: exclusions + }) path = resolve(process.argv[2] || '.') - console.log "Starting LiveReload for #{path} on port #{port}." + console.log "Starting LiveReload v#{version} for #{path} on port #{port}." server.watch(path) - console.log "Polling for changes every #{interval}ms." module.exports = run: runner diff --git a/lib/command.js b/lib/command.js index 1eed0dd..5776625 100644 --- a/lib/command.js +++ b/lib/command.js @@ -3,24 +3,35 @@ var runner; runner = function() { - var debug, interval, livereload, opts, path, port, resolve, server; + var debug, exclusions, livereload, opts, path, pjson, port, resolve, server, version; + pjson = require('../package.json'); + version = pjson.version; livereload = require('./livereload'); resolve = require('path').resolve; opts = require('opts'); debug = false; opts.parse([ { + short: "v", + long: "version", + description: "Show the version", + required: false, + callback: function() { + console.log(version); + return process.exit(1); + } + }, { short: "p", long: "port", description: "Specify the port", value: true, required: false }, { - short: "i", - long: "interval", - description: "Specify the interval", - value: true, - required: false + short: "x", + long: "exclusions", + description: "Exclude files by specifying an array of regular expressions. Will be appended to default value which is [/\\.git\//, /\\.svn\//, /\\.hg\//]", + required: false, + value: true }, { short: "d", long: "debug", @@ -32,16 +43,15 @@ } ].reverse(), true); port = opts.get('port') || 35729; - interval = opts.get('interval') || 1000; + exclusions = opts.get('exclusions') || []; server = livereload.createServer({ port: port, - interval: interval, - debug: debug + debug: debug, + exclusions: exclusions }); path = resolve(process.argv[2] || '.'); - console.log("Starting LiveReload for " + path + " on port " + port + "."); - server.watch(path); - return console.log("Polling for changes every " + interval + "ms."); + console.log("Starting LiveReload v" + version + " for " + path + " on port " + port + "."); + return server.watch(path); }; module.exports = { diff --git a/lib/livereload.coffee b/lib/livereload.coffee index b3e0e62..42c0028 100755 --- a/lib/livereload.coffee +++ b/lib/livereload.coffee @@ -36,10 +36,6 @@ class Server @config.originalPath ?= '' @config.overrideURL ?= '' - @config.interval ?= 1000 - - @sockets = [] - listen: -> @debug "LiveReload is waiting for browser to connect." @@ -52,19 +48,21 @@ class Server @server.on 'connection', @onConnection.bind @ @server.on 'close', @onClose.bind @ - onConnection: (socket) -> @debug "Browser connected." + socket.send "!!ver:#{@config.version}" socket.on 'message', (message) => if (@config.debug) @debug "Browser URL: #{message}" + + # FIXME: This doesn't seem to be firing either. socket.on 'error', (err) => @debug "Error in client socket: #{err}" - @sockets.push socket + # FIXME: This does not seem to be firing onClose: (socket) -> @debug "Browser disconnected." @@ -93,8 +91,10 @@ class Server override_url: this.config.overrideURL ] - for socket in @sockets - socket.send data + for socket in @server.clients + socket.send data, (error) => + if error + @debug error debug: (str) -> if @config.debug diff --git a/lib/livereload.js b/lib/livereload.js index c4f4b80..4639dd0 100644 --- a/lib/livereload.js +++ b/lib/livereload.js @@ -26,7 +26,7 @@ Server = (function() { function Server(config1) { - var base, base1, base2, base3, base4, base5, base6, base7, base8, base9; + var base, base1, base2, base3, base4, base5, base6, base7, base8; this.config = config1; if (this.config == null) { this.config = {}; @@ -60,9 +60,6 @@ if ((base8 = this.config).overrideURL == null) { base8.overrideURL = ''; } - if ((base9 = this.config).interval == null) { - base9.interval = 1000; - } this.sockets = []; } @@ -92,16 +89,16 @@ } }; })(this)); - socket.on('error', (function(_this) { + return socket.on('error', (function(_this) { return function(err) { return _this.debug("Error in client socket: " + err); }; })(this)); - return this.sockets.push(socket); }; Server.prototype.onClose = function(socket) { - return this.debug("Browser disconnected."); + this.debug("Browser disconnected."); + return this.sockets.splice(socket, 1); }; Server.prototype.watch = function(paths) { @@ -155,11 +152,17 @@ override_url: this.config.overrideURL } ]); - ref = this.sockets; + ref = this.server.clients; results = []; for (i = 0, len = ref.length; i < len; i++) { socket = ref[i]; - results.push(socket.send(data)); + results.push(socket.send(data, (function(_this) { + return function(error) { + if (error) { + return _this.debug(error); + } + }; + })(this))); } return results; }; diff --git a/package.json b/package.json index 3011e1f..926d49f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "livereload" , "description": "LiveReload server" -, "version": "0.3.7" +, "version": "0.4.0" , "contributors": [ { "name": "Brian P. Hogan", "email": "brianhogan@napcs.com" } ]