From 0bf26c624714da3a68d8d021549618e6004e085b Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Sun, 24 Feb 2013 15:46:34 -0600 Subject: [PATCH] Initial import & release --- .gitignore | 3 ++ .jshintrc | 14 +++++++++ Gruntfile.js | 57 +++++++++++++++++++++++++++++++++++ LICENSE-MIT | 22 ++++++++++++++ README.md | 66 +++++++++++++++++++++++++++++++++++++++-- package.json | 44 +++++++++++++++++++++++++++ tasks/express-server.js | 49 ++++++++++++++++++++++++++++++ test/server.js | 5 ++++ 8 files changed, 257 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 .jshintrc create mode 100644 Gruntfile.js create mode 100644 LICENSE-MIT create mode 100644 package.json create mode 100644 tasks/express-server.js create mode 100644 test/server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b785247 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +npm-debug.log +tmp diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..6b4c1a9 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,14 @@ +{ + "curly": true, + "eqeqeq": true, + "immed": true, + "latedef": true, + "newcap": true, + "noarg": true, + "sub": true, + "undef": true, + "boss": true, + "eqnull": true, + "node": true, + "es5": true +} diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..8820e46 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,57 @@ +/* + * grunt-express-server + * https://github.com/ericclemmons/grunt-express-server + * + * Copyright (c) 2013 Eric Clemmons + * Licensed under the MIT license. + */ + +'use strict'; + +module.exports = function(grunt) { + + // Project configuration. + grunt.initConfig({ + jshint: { + all: [ + 'Gruntfile.js', + 'tasks/*.js', + '<%= nodeunit.tests %>', + ], + options: { + jshintrc: '.jshintrc', + }, + }, + + // Before generating any new files, remove any previously-created files. + clean: { + tests: ['tmp'], + }, + + // Unit tests. + nodeunit: { + tests: ['test/*_test.js'], + }, + + server: { + script: './test/server.js' + } + + }); + + // Actually load this plugin's task(s). + grunt.loadTasks('tasks'); + + // These plugins provide necessary tasks. + grunt.loadNpmTasks('grunt-contrib-jshint'); + grunt.loadNpmTasks('grunt-contrib-clean'); + grunt.loadNpmTasks('grunt-contrib-nodeunit'); + + // Whenever the "test" task is run, first clean the "tmp" dir, then run this + // plugin's task(s), then test the result. + grunt.registerTask('test', ['clean', 'express-server', 'nodeunit']); + + // By default, lint and run all tests. + grunt.registerTask('default', ['jshint', 'test']); + +}; diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..1b934ee --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,22 @@ +Copyright (c) 2013 Eric Clemmons + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index caf1c2c..b5e361b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,64 @@ -grunt-express-server -==================== +# grunt-express-server -Grunt task for running an Express Server that works great with LiveReload + Watch/Regarde \ No newline at end of file +> Grunt task for running an Express Server that works great with LiveReload + Watch/Regarde + +## Getting Started +This plugin requires Grunt `~0.4.0` + +If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command: + +```shell +npm install grunt-express-server --save-dev +``` + +One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript: + +```js +grunt.loadNpmTasks('grunt-express-server'); +``` + +## The "server" task + +### Setup + +In your project's Gruntfile, simply add a `script` property to your existing `server` object. + +```js +grunt.initConfig({ + server: { + script: 'path/to/server.js' + } +}) +``` + +### Usage + +Assuming you're using [grunt-contrib-livereload][https://github.com/gruntjs/grunt-contrib-livereload], +I recommend using the followingin your project's Gruntfile: + +```js +grunt.initConfig({ + watch: { + server: { + files: '<%= dirs.server + files.all %>', + tasks: [ 'jshint', 'express-server', 'livereload' ] + } + } +}); + +grunt.registerTask('server', [ 'express-server', 'livereload', 'watch' ]) +``` + +This will let you override `grunt server` with a LiveReload-able Express Server. +Finally, you can make changes to your API and watch the JSON change in your browser! + + +## Contributing + +In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/). + +## Release History + +### v0.1.0 + +- Initial import from [Genesis Skeleton](https://github.com/ericclemmons/genesis-skeleton) & release diff --git a/package.json b/package.json new file mode 100644 index 0000000..556a0ea --- /dev/null +++ b/package.json @@ -0,0 +1,44 @@ +{ + "name": "grunt-express-server", + "description": "Grunt task for running an Express Server that works great with LiveReload + Watch/Regarde", + "version": "0.1.0", + "homepage": "https://github.com/ericclemmons/grunt-express-server", + "author": { + "name": "Eric Clemmons", + "email": "eric@smarterspam.com" + }, + "repository": { + "type": "git", + "url": "git://github.com/ericclemmons/grunt-express-server.git" + }, + "bugs": { + "url": "https://github.com/ericclemmons/grunt-express-server/issues" + }, + "licenses": [ + { + "type": "MIT", + "url": "https://github.com/ericclemmons/grunt-express-server/blob/master/LICENSE-MIT" + } + ], + "main": "Gruntfile.js", + "engines": { + "node": ">= 0.8.0" + }, + "scripts": { + "test": "grunt test" + }, + "devDependencies": { + "grunt-contrib-jshint": "~0.1.1", + "grunt-contrib-clean": "~0.4.0", + "grunt-contrib-nodeunit": "~0.1.2", + "grunt": "~0.4.0" + }, + "peerDependencies": { + "grunt": "~0.4.0" + }, + "keywords": [ + "gruntplugin", + "express", + "server" + ] +} diff --git a/tasks/express-server.js b/tasks/express-server.js new file mode 100644 index 0000000..9dd7d37 --- /dev/null +++ b/tasks/express-server.js @@ -0,0 +1,49 @@ +/* + * grunt-express-server + * https://github.com/ericclemmons/grunt-express-server + * + * Copyright (c) 2013 Eric Clemmons + * Licensed under the MIT license. + */ + +'use strict'; + +var path = require('path'); +var server = null; // Store server between live reloads to close/restart express + +module.exports = function(grunt) { + + grunt.registerTask('express-server', 'Start an express web server', function() { + + var done = this.async(); + + if (server) { + console.log("Killing existing Express server"); + + server.kill('SIGTERM'); + server = null; + } + + server = grunt.util.spawn({ + cmd: process.argv[0], + args: [ grunt.config.get('server.script') ], + fallback: function() { + // Prevent EADDRINUSE from breaking Grunt + } + }, function(err, result, code) { + // Nothing to do, but callback has to exist + }); + + server.stdout.on('data', function() { + if (done) { + done(); + } + + done = null; + }); + + server.stdout.pipe(process.stdout); + server.stderr.pipe(process.stdout); + }); + +}; diff --git a/test/server.js b/test/server.js new file mode 100644 index 0000000..5884bda --- /dev/null +++ b/test/server.js @@ -0,0 +1,5 @@ +/** + * Test Server + */ + +module.exports = function() {};