Skip to content

Commit

Permalink
add watchers and closeAllWatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowwzw committed Oct 16, 2017
1 parent 3f315e3 commit a845947
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"name": "lite-dev-server",
"version": "2.2.5",
"version": "3.2.5",
"description": "This is http file server for develpment. This server supports livereload function and proxy function for your api server.",
"main": "index.js",
"scripts": {
"build": "gulp build",
"test": "ava tests",
"all": "npm run build && npm test",
"prepublish": "npm run all"
"test": "npm run build && ava tests",
"prepublish": "npm test"
},
"repository": {
"type": "git",
Expand Down
13 changes: 10 additions & 3 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const liteDevServer = ({
cb();
};
let wss = null;
const watchers = [];
if (liveReload) {
const EventEmitter = require("events");
const liveReloadEM = new EventEmitter();
Expand All @@ -66,11 +67,11 @@ const liteDevServer = ({
});
console.log(chalk.green(`\nwatchFolders ${watchFolders}`));
watchFolders.forEach(folder => {
fs.watch(`${folder}`, { recursive: true }, () => {
watchers.push(fs.watch(`${folder}`, { recursive: true }, () => {
setTimeout(function () {
liveReloadEM.emit("reload");
}, liveReloadDelay);
});
}));
});
}
if (page404)
Expand All @@ -89,7 +90,7 @@ const liteDevServer = ({
});
if (matchedProxy) {
const pathRewrite = matchedProxy.pathRewrite;
if (pathRewrite && (typeof pathRewrite === 'object')) {
if (pathRewrite && (typeof pathRewrite === "object")) {
url = url.replace(pathRewrite.pattern, pathRewrite.replacement);
}
const options = {
Expand Down Expand Up @@ -171,6 +172,12 @@ const liteDevServer = ({
server.listen(listen);
console.log(chalk.green(`lite-dev-server listening on port ${server.address().port}`));
server.wss = wss;
server.watchers = watchers;
server.closeAllWatchers = () => {
if(watchers && watchers.length){
watchers.forEach( watcher => watcher && watcher.close && watcher.close());
}
};
return server;
};

Expand Down
1 change: 0 additions & 1 deletion tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const server1 = liteDevServer({
const server2 = liteDevServer({
serverName: "server2",
folder: __dirname + "/../tests/static",
watchFolders: [__dirname + "/../tests/static"],
listen: 0,
page404: null,
liveReload: false,
Expand Down

0 comments on commit a845947

Please sign in to comment.