stefanpenner/ember-cli — pull request #2265 by csantero (2014-10-08 23:57:16+00:00)
Title: Restart express server and trigger Livereload when a file under /server changes.

### Rationale

When working with http-mocks I get tired of having to tab to my terminal and restart `ember serve` every time I change a file.

### Summary of changes

This change introduces a development server restart mechanism. I created a new task called server-watcher, which wraps sane in a similar way to how watcher wraps broccoli-sane-watcher. The serve task creates an instance of server-watcher, and passes it to express-server. The express-server task is also now passed to the livereload-server task so it can be informed of server restarts.

Any file change/addition/deletion underneath the top-level `/server` directory will cause the following steps to be performed:

1. Schedules a reload in 100 ms, in case multiple files are changed in rapid succession.
2. Waits 100 ms
3. Destroys all sockets to the http server
4. Closes the http server
5. Invalidates the node require cache for the changed files
6. Creates a new express app
7. Reprocesses app and addon middlewares
8. Starts a new http server with the new express app
9. Triggers Live-Reload

### Concerns

* I'm not sure step 5 is a broad enough purge. A possible scenario for failure here is if, given files A and B underneath `/server`, file A requires file B and then saves a copy of that exported module. If file B then changes, the logic in file A will still be depending on the old version of file B. The solution would be to delete the require cache for every file underneath `/server`.
* Exposing the express app instance to addons now that we intend for it to be disposed often makes me afraid of memory leaks.
* Sane seems to be returning both an `add` and a `change` event on file creation, and that's bubbling through to the UI as two separate messages.
* This probably needs some decent integration tests. The unit tests were easy to write, but I'm not sure which files are a good example to work off for making a more comprehensive test.