-
Couldn't load subscription status.
- Fork 60
Add CoffeeScript support & add synchronous foreground process support... #7
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
33171d6
Add ability to use a custom command to run the express server
rehno-lindeque 0981acf
Stop the server on SIGINT
rehno-lindeque afcae22
Foreground express server waits for done signal
rehno-lindeque 7865a8e
Simplify the termination logic and distinguish between the grunt task…
rehno-lindeque 69fc63a
Merge pull request #1 from circuithub/master
rehno-lindeque 489efdf
Change convention for assigning local this and change port assignment…
rehno-lindeque 364f5ed
Merge branch 'master' of github.com:rehno-lindeque/grunt-express-server
rehno-lindeque 8c41039
Use the correct grunt method for extend and remember to declare the s…
rehno-lindeque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,22 +12,27 @@ module.exports = function(grunt) { | |
| var done = null; | ||
| var server = null; // Store server between live reloads to close/restart express | ||
|
|
||
| var finished = function() { | ||
| var taskFinished = function() { | ||
| if (done) { | ||
| //grunt.log.writeln('Task finished'.green); | ||
| done(); | ||
|
|
||
| done = null; | ||
| } | ||
| }; | ||
| var processFinished = function() { | ||
| //grunt.log.writeln('Process finished'.green); | ||
| server = null; | ||
| taskFinished(); | ||
| }; | ||
|
|
||
| return { | ||
| start: function(options) { | ||
| var _this = this; | ||
| if (server) { | ||
| this.stop(); | ||
|
|
||
| if (grunt.task.current.flags.stop) { | ||
| finished(); | ||
|
|
||
| return; | ||
| } | ||
| }; | ||
|
|
@@ -37,51 +42,46 @@ module.exports = function(grunt) { | |
| done = grunt.task.current.async(); | ||
|
|
||
| // Set PORT for new processes | ||
| // TODO: Shouldn't the port be set on server.env.PORT instead? | ||
| process.env.PORT = options.port; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you know what, you're right! A few lines below it has |
||
|
|
||
| // Spawn the express server | ||
| server = grunt.util.spawn({ | ||
| cmd: (options.cmd || process.argv[0]), | ||
| args: options.args, | ||
| env: process.env, | ||
| fallback: options.fallback | ||
| }, options.error); | ||
|
|
||
| // Set up signal handlers for the child (server) & parent processes | ||
| server.once('exit', processFinished); | ||
| process.once('SIGINT', function(){ _this.stop('SIGINT'); }); | ||
| process.once('SIGTERM', function(){ _this.stop('SIGTERM'); }); | ||
| process.once('exit', this.stop); | ||
|
|
||
| server.stdout.pipe(process.stdout); | ||
| server.stderr.pipe(process.stderr); | ||
|
|
||
| // Delay / response detection for a server running as a background process | ||
| if (options.background) { | ||
| server = grunt.util.spawn({ | ||
| cmd: process.argv[0], | ||
| args: options.args, | ||
| env: process.env, | ||
| fallback: options.fallback | ||
| }, options.error); | ||
|
|
||
| if (options.delay) { | ||
| setTimeout(finished, options.delay); | ||
| setTimeout(taskFinished, options.delay); | ||
| } | ||
|
|
||
| if (options.output) { | ||
| server.stdout.on('data', function(data){ | ||
| var message = "" + data; | ||
| var regex = new RegExp(options.output, "gi"); | ||
| if (message.match(regex)) finished(); | ||
| if (message.match(regex)) taskFinished(); | ||
| }); | ||
| } | ||
|
|
||
| server.stdout.pipe(process.stdout); | ||
| server.stderr.pipe(process.stderr); | ||
| } else { | ||
| // Server is ran in current process | ||
| server = require(options.script); | ||
|
|
||
| finished(); | ||
| } | ||
|
|
||
| process.on('exit', finished); | ||
| process.on('exit', this.stop); | ||
| }, | ||
|
|
||
| stop: function() { | ||
| stop: function(signal) { | ||
| if (server) { | ||
| grunt.log.writeln('Stopping'.red + ' Express server'); | ||
|
|
||
| server.kill('SIGTERM'); | ||
| process.removeAllListeners(); | ||
| server = null; | ||
| server.kill(signal? signal : 'SIGTERM'); | ||
| }; | ||
|
|
||
| finished(); | ||
| } | ||
| }; | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Common convention is to call this
self, at least in grunt & yeoman.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I was wondering what the norm is