live-server
Advanced tools
Comparing version 0.7.0 to 0.7.1
50
index.js
#!/usr/bin/env node | ||
var connect = require('connect'), | ||
var fs = require('fs'), | ||
connect = require('connect'), | ||
colors = require('colors'), | ||
@@ -14,3 +15,3 @@ WebSocket = require('faye-websocket'), | ||
var INJECTED_CODE = require('fs').readFileSync(__dirname + "/injected.html", "utf8"); | ||
var INJECTED_CODE = fs.readFileSync(__dirname + "/injected.html", "utf8"); | ||
@@ -33,2 +34,3 @@ var LiveServer = {}; | ||
var hasNoOrigin = !req.headers.origin; | ||
var doInject = false; | ||
@@ -42,2 +44,11 @@ function directory() { | ||
function file(filepath, stat) { | ||
var x = path.extname(filepath); | ||
if (hasNoOrigin && (x === "" || x == ".html" || x == ".htm" || x == ".xhtml" || x == ".php")) { | ||
// TODO: Sync file read here is not nice, but we need to determine if the html should be injected or not | ||
var contents = fs.readFileSync(filepath, "utf8"); | ||
doInject = contents.indexOf("</body>") > -1; | ||
} | ||
} | ||
function error(err) { | ||
@@ -49,8 +60,6 @@ if (404 == err.status) return next(); | ||
function inject(stream) { | ||
var x = path.extname(reqpath); | ||
if (hasNoOrigin && (x === "" || x == ".html" || x == ".htm" || x == ".xhtml" || x == ".php")) { | ||
if (doInject) { | ||
// We need to modify the length given to browser | ||
var len = INJECTED_CODE.length + res.getHeader('Content-Length'); | ||
res.setHeader('Content-Length', len); | ||
var originalPipe = stream.pipe; | ||
@@ -65,4 +74,5 @@ stream.pipe = function(res) { | ||
.on('error', error) | ||
.on('directory', directory) | ||
.on('file', file) | ||
.on('stream', inject) | ||
.on('directory', directory) | ||
.pipe(res); | ||
@@ -77,3 +87,4 @@ }; | ||
* @param root {string} Path to root directory (default: cwd) | ||
* @param noBrowser | ||
* @param open {string} Subpath to open in browser, use false to suppress launch (default: server root) | ||
* @param logLevel {number} 0 = errors only, 1 = some, 2 = lots | ||
*/ | ||
@@ -85,3 +96,6 @@ LiveServer.start = function(options) { | ||
var root = options.root || process.cwd(); | ||
var noBrowser = options.noBrowser || false; | ||
var logLevel = options.logLevel === undefined ? 2 : options.logLevel; | ||
var openPath = (options.open === undefined || options.open === true) ? | ||
"" : ((options.open === null || options.open === false) ? null : options.open); | ||
if (options.noBrowser) openPath = null; // Backwards compatibility with 0.7.0 | ||
@@ -91,4 +105,5 @@ // Setup a web server | ||
.use(staticServer(root)) // Custom static server | ||
.use(connect.directory(root, { icons: true })) | ||
.use(connect.logger('dev')); | ||
.use(connect.directory(root, { icons: true })); | ||
if (logLevel >= 2) | ||
app.use(connect.logger('dev')); | ||
var server = http.createServer(app).listen(port, host); | ||
@@ -115,6 +130,8 @@ // WebSocket | ||
ws.send('refreshcss'); | ||
console.log("CSS change detected".magenta); | ||
if (logLevel >= 1) | ||
console.log("CSS change detected".magenta); | ||
} else { | ||
ws.send('reload'); | ||
console.log("File change detected".cyan); | ||
if (logLevel >= 1) | ||
console.log("File change detected".cyan); | ||
} | ||
@@ -125,10 +142,11 @@ } | ||
// Output | ||
var browserURL = "http://127.0.0.1:" + port; | ||
console.log(('Serving "' + root + '" at ' + browserURL).green); | ||
var serveURL = "http://127.0.0.1:" + port; | ||
if (logLevel >= 1) | ||
console.log(('Serving "' + root + '" at ' + serveURL).green); | ||
// Launch browser | ||
if(!noBrowser) | ||
open(browserURL); | ||
if (openPath !== null) | ||
open(serveURL + openPath); | ||
}; | ||
module.exports = LiveServer; |
@@ -6,3 +6,4 @@ #!/usr/bin/env node | ||
port: process.env.PORT, | ||
noBrowser: false | ||
open: true, | ||
logLevel: 2 | ||
}; | ||
@@ -19,7 +20,19 @@ | ||
} | ||
} else if (arg == "--no-browser") { | ||
opts.noBrowser = true; | ||
} | ||
else if (arg.indexOf("--open=") > -1) { | ||
var path = arg.substring(7); | ||
if (path.indexOf('/') != 0) { | ||
path = '/' + path; | ||
} | ||
opts.open = path; | ||
process.argv.splice(i, 1); | ||
} | ||
else if (arg == "--no-browser") { | ||
opts.open = false; | ||
process.argv.splice(i, 1); | ||
} else if (arg == "--quiet" || arg == "-q") { | ||
opts.logLevel = 0; | ||
process.argv.splice(i, 1); | ||
} else if (arg == "--help" || arg == "-h") { | ||
console.log('Usage: live-server [-h|--help] [--port=PORT] [--no-browser] [PATH]'); | ||
console.log('Usage: live-server [-h|--help] [-q|--quiet] [--port=PORT] [--open=PATH] [--no-browser] [PATH]'); | ||
process.exit(); | ||
@@ -29,5 +42,6 @@ } | ||
if (process.argv[2]) | ||
if (process.argv[2]) { | ||
process.chdir(process.argv[2]); | ||
} | ||
liveServer.start(opts); |
{ | ||
"name": "live-server", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "simple development http server with live reload capability", | ||
@@ -20,3 +20,3 @@ "keywords": [ | ||
"watchr": "2.3.x", | ||
"faye-websocket": "0.4.x", | ||
"faye-websocket": "0.9.x", | ||
"event-stream" : "latest" | ||
@@ -23,0 +23,0 @@ }, |
@@ -45,5 +45,9 @@ [![view on npm](http://img.shields.io/npm/v/live-server.svg)](https://www.npmjs.org/package/live-server) | ||
Use parameter `--no-browser` to suppress automatic web browser launching. | ||
Additional parameters: | ||
* `--no-browser` - suppress automatic web browser launching | ||
* `--quiet` - suppress logging | ||
* `--open=PATH` - launch browser to PATH instead of server root | ||
Usage from node | ||
@@ -59,3 +63,3 @@ --------------- | ||
root: "/public", // Set root directory that's being server. Defaults to cwd. | ||
noBrowser = true // When true, it won't load your browser by default. | ||
open: false // When false, it won't load your browser by default. | ||
}; | ||
@@ -81,2 +85,8 @@ liveServer.start(params); | ||
* v0.7.1 | ||
- Fix hang caused by trying to inject into fragment html files without `</body>` | ||
- `logLevel` parameter in library to control amount of console spam | ||
- `--quiet` cli option to suppress console spam | ||
- `--open=PATH` cli option to launch browser in specified path instead of root (@richardgoater) | ||
- Library's `noBrowser: true` option is deprecated in favor of `open: false` | ||
* v0.7.0 | ||
@@ -83,0 +93,0 @@ - API BREAKAGE: LiveServer library now takes parameters in an object |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13010
169
139
+ Addedfaye-websocket@0.9.4(transitive)
+ Addedhttp-parser-js@0.5.8(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedwebsocket-driver@0.7.4(transitive)
+ Addedwebsocket-extensions@0.1.4(transitive)
- Removedfaye-websocket@0.4.4(transitive)
Updatedfaye-websocket@0.9.x