Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #276 | Added --manual-hook option #333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Command line parameters:
* `--ignorePattern=RGXP` - Regular expression of files to ignore (ie `.*\.jade`) (**DEPRECATED** in favor of `--ignore`)
* `--no-css-inject` - reload page on CSS change, rather than injecting changed CSS
* `--middleware=PATH` - path to .js file exporting a middleware function to add; can be a name without path nor extension to reference bundled middlewares in `middleware` folder
* `--manual-hook=ROUTE?type=reload|refreshcss` - listens for reload requests, allowing triggering manual reloads
* `--entry-file=PATH` - serve this file (server root relative) in place of missing files (useful for single page apps)
* `--mount=ROUTE:PATH` - serve the paths contents under the defined route (multiple definitions possible)
* `--spa` - translate requests from /abc to /#/abc (handy for Single Page Apps)
Expand Down Expand Up @@ -88,7 +89,8 @@ var params = {
wait: 1000, // Waits for all changes, before reloading. Defaults to 0 sec.
mount: [['/components', './node_modules']], // Mount a directory to a route.
logLevel: 2, // 0 = errors only, 1 = some, 2 = lots
middleware: [function(req, res, next) { next(); }] // Takes an array of Connect-compatible middleware that are injected into the server middleware stack
middleware: [function(req, res, next) { next(); }], // Takes an array of Connect-compatible middleware that are injected into the server middleware stack
manualHook: "/manual-reload", // optional route for listening manual reload requests
};
liveServer.start(params);
```
Expand Down
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ function entryPoint(staticHandler, file) {
* @param wait {number} Server will wait for all changes, before reloading
* @param htpasswd {string} Path to htpasswd file to enable HTTP Basic authentication
* @param middleware {array} Append middleware to stack, e.g. [function(req, res, next) { next(); }].
* @param manualHook {string} URL form listening manual hooks
*/
LiveServer.start = function(options) {
options = options || {};
Expand All @@ -149,6 +150,7 @@ LiveServer.start = function(options) {
var https = options.https || null;
var proxy = options.proxy || [];
var middleware = options.middleware || [];
var manualHook = options.manualHook || null;
var noCssInject = options.noCssInject;
var httpsModule = options.httpsModule;

Expand Down Expand Up @@ -192,6 +194,31 @@ LiveServer.start = function(options) {
app.use(mw);
});

if (manualHook) {
app.use(function manualReloadMiddleware (req, res, next) {
var reqUrl = url.parse(req.url, true)

if( manualHook !== reqUrl.pathname ) return next()

var refreshCss = reqUrl.query.type === "refreshcss"

if (LiveServer.logLevel >= 1) {
console.log( refreshCss
? "manual CSS change".magenta
: "manual reload".cyan
);
}

clients.forEach(function(ws) {
if (ws)
ws.send(refreshCss ? "refreshcss" : "reload");
});

res.statusCode = 201
res.end('')
});
}

// Use http-auth if configured
if (htpasswd !== null) {
var auth = require('http-auth');
Expand Down
6 changes: 5 additions & 1 deletion live-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ for (var i = process.argv.length - 1; i >= 2; --i) {
opts.middleware.push(arg.substring(13));
process.argv.splice(i, 1);
}
else if (arg.indexOf("--manual-hook=") > -1) {
opts.manualReload = arg.substring(14);
process.argv.splice(i, 1);
}
else if (arg === "--help" || arg === "-h") {
console.log('Usage: live-server [-v|--version] [-h|--help] [-q|--quiet] [--port=PORT] [--host=HOST] [--open=PATH] [--no-browser] [--browser=BROWSER] [--ignore=PATH] [--ignorePattern=RGXP] [--no-css-inject] [--entry-file=PATH] [--spa] [--mount=ROUTE:PATH] [--wait=MILLISECONDS] [--htpasswd=PATH] [--cors] [--https=PATH] [--https-module=MODULE_NAME] [--proxy=PATH] [PATH]');
console.log('Usage: live-server [-v|--version] [-h|--help] [-q|--quiet] [--port=PORT] [--host=HOST] [--open=PATH] [--no-browser] [--browser=BROWSER] [--ignore=PATH] [--ignorePattern=RGXP] [--no-css-inject] [--entry-file=PATH] [--spa] [--manual-hook=ROUTE] [--mount=ROUTE:PATH] [--wait=MILLISECONDS] [--htpasswd=PATH] [--cors] [--https=PATH] [--https-module=MODULE_NAME] [--proxy=PATH] [PATH]');
process.exit();
}
else if (arg === "--test") {
Expand Down
21 changes: 21 additions & 0 deletions test/manual-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var request = require('supertest');
var path = require('path');
var liveServer1 = require('..').start({
root: path.join(__dirname, 'data'),
port: 0,
open: false,
manualHook: '/manual-reload'
});

describe('manual-hook tests', function() {
it("should respond with static server status code", function(done) {
request(liveServer1)
.get('/')
.expect(200, done);
});
it("should respond with middleware function's status code", function(done) {
request(liveServer1)
.get('/manual-reload')
.expect(201, done);
});
});