Skip to content

Commit

Permalink
Release 0.4.0
Browse files Browse the repository at this point in the history
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
  • Loading branch information
napcs committed Oct 26, 2015
1 parent c60a718 commit 4cf5f98
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 46 deletions.
39 changes: 32 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,47 @@ 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:

```
<script>
document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] +
':35729/livereload.js?snipver=1"></' + 'script>')
</script>
```

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:

$ npm install -g livereload
$ 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();
Expand Down Expand Up @@ -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.
Expand All @@ -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 <http://feedback.livereload.com/knowledgebase/articles/86220-preview-css-changes-against-a-live-site-then-uplo> 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

Expand Down
31 changes: 22 additions & 9 deletions lib/command.coffee
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand All @@ -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
34 changes: 22 additions & 12 deletions lib/command.js

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

16 changes: 8 additions & 8 deletions lib/livereload.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ class Server
@config.originalPath ?= ''
@config.overrideURL ?= ''

@config.interval ?= 1000

@sockets = []

listen: ->
@debug "LiveReload is waiting for browser to connect."

Expand All @@ -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."

Expand Down Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions lib/livereload.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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" }
]
Expand Down

0 comments on commit 4cf5f98

Please sign in to comment.