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

Allow "connect-like" object to be passed to grunt-express as the "server" #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

benhutchins
Copy link

Allow "connect-like" object to be passed to grunt-express as the "server" rather than just a string.

This allows for grunt to pass configuration to the server, such as things from grunt.option or whatever it desires, and then build out the server and pass back the ready-object.

My use case for this is that I am building a grunt plugin which performs client-side unit testing. This plugin consumes grunt-express to start a localhost web server which generates the HTML pages for the tests. I want to pass in the list of dependencies for each test file to the web server from Grunt, or at least the paths where the test files live which are configured in the user's Gruntfile.js - currently there is no way for me to pass data to the server.js from grunt-express.

An example of how to use this:

Gruntfile.js

var path = require('path');

module.exports = function(grunt) {
  grunt.initConfig({
    testEnvironment: {
      // define the root of your project files
      root: path.join(__dirname, 'src'),
      // define the pattern to identify your unit tests
      pattern: '**/*.unittests.js',
      // define your global dependencies, relative to your root project
      deps: [
        '3rdparty/jquery.js'
      ]
    }
  });
};

testEnvironment.js - grunt task consuming grunt-express

var path = require('path');

module.exports = function (grunt) {

  grunt.registerTask('testEnvironment', function () {
    var options = this.options({
      deps: []
    });

    var config = {
      // find all the test files matching the options
      tests: findTests(options),
      deps: options.deps
    };

    grunt.config.set('express:testEnvironment', {
      options: {
        port: 3000,
        server: require(path.join(__dirname, 'lib', 'server.js'))(config)
      }
    });

    grunt.task.run('express:testEnvironment');
  });
};

server.js

var express = require('express')
var app = express();

module.exports = function (config) {
    // do stuff now
    return app;
};

Currently there would be no way to pass configuration to the server prior to it starting. This allows that to happen. There are existing hooks to pass in middleware from grunt, but no way to do more advanced configuration. This allows the system to be a lot more robust.

@benhutchins
Copy link
Author

I've revised my pull request. I was copying my changes from my node_modules/grunt-express to my grunt-express checkout. Attempted to run the unit tests within the project but they timed out, and Travis is failing, so was didn't test my copy. I originally had the var server = options.server; if (options.serer) { ... - must have missed that line when I copied it, I've revised it to within an else statement for my commit.

benhutchins added a commit to vistaprint/grunt-js-test that referenced this pull request Aug 7, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants