jakejs / jake

JavaScript build tool, similar to Make or Rake. Built to work with Node.js.
http://jakejs.com
Apache License 2.0
1.97k stars 190 forks source link

Proposal: grouped namespaces/tasks #330

Closed navneetgarg123 closed 7 years ago

navneetgarg123 commented 7 years ago

Allow for environment specific task loading

define a group function that that takes

  1. single group name (string) or a list of group names
  2. a function to be executed if currently running in the context of the provided group
  3. If no group is specified for a namespace or task, assume it is in the default group

Example Usage:

// Jakefile
group('test', function () {
    var testrunner = require('sometestrunner');
    desc('run tests');
    task('test', function () {
        console.log('test task');
        testrunner.execute();
    });
});

group('production', function () {
    desc('production task');
    task('prod', function () {
        console.log('prod task');
    });
});

desc('print hello');
task('hello', function () {
    console.log('hello world');
});
# from the command line
$ jake -T
jake hello        print hello
jake prod         production task
jake test         run tests

$ jake -T --group test
jake test         run tests

$ jake -T --group production
jake prod         production task

Inspired by ruby - Bundler/Rake

mde commented 7 years ago

I like the idea of being able to run context-specific tasks, but it seems like there is a ton of overlap between this proposal, and Jake namespaces:

http://jakejs.com/docs#tasks_namespaces

How is this different from namespaces?

navneetgarg123 commented 7 years ago

The use case I found it useful would be with development/production environments. I want to use jake as my entry point build tool/task runner for everything regardless of environment. I'd rather not even have my test namespace exist or load when in a production or staging environment because that's going to use dependencies that aren't installed (and error out as a result).

navneetgarg123 commented 7 years ago

maybe environment would make more sense than group. I was envisioning something like

environment('production', function () {
   // require non-dev dependencies
    desc('start production server');
    task('start', function () {
        // start production server
    });
});

environment(['development', 'test'], function () {
    // require development/test deps
    namespace('test', function () {
       // test related tasks
    });
});

The issue with namespaces is that the namespaces (and tasks for that matter) get loaded no matter what - which prevents me from using jake in production.

mde commented 7 years ago

Rather than trying to add this as a feature into the framework (given how similar it is conceptually to namespaces), it might make more sense to make use of the fact that Jake is really just all executable Javascript. A Jakefile is simply pulled into the runtime via ordinary require -- you could easily accomplish what you're wanting via an environment-specific require in your Jakefile:

'use strict';
let env = process.env.NODE_ENV;

if (env) {
  require('./' + env + '_jakefile.js');
}
else {
  console.log('Please set NODE_ENV');
}

Just run it something like NODE_ENV=production jake -T (You would need a production_jakefile.js file in the directory where you run the command.)

navneetgarg123 commented 7 years ago

That implementation works if you put your tasks directly in the Jakefile, however it becomes slightly more unwieldy if you utilize the jakelib directory to split out the tasks. It's definitely possible to setup the requiring of the subsequent jakefile and the necessary directory/directories but I guess I was considering more of a convention to do the environment specific requiring.

It's starting to look like the environment function might be something that could live external to jake actually.

mde commented 7 years ago

Right, the jakelib approach eagerly does a require of everything in that directory. Yes, just wrapping the lazy require in a library might be the optimal approach here. Shall I go ahead and close this issue?

navneetgarg123 commented 7 years ago

Yes please. Sorry I thought I'd responded to this already.