gulpjs / undertaker

Task registry that allows composition through series/parallel methods.
MIT License
200 stars 31 forks source link

Edit documentation to register tasks to gulp alongside taker. #36

Closed tmcgee123 closed 9 years ago

tmcgee123 commented 9 years ago

On the "Sharing Functionalities" section of the documentation, it goes through an example on how to collaborate tasks into the main file for usage in default.

Most people will want to be able to run individual gulp tasks from the command line. Since gulp-hub is not up to date with the latest undertaker functionality, we now have to register undertaker tasks instead of gulp tasks to properly share them through the registry.

I've found that by editing the example set function to register the gulp task, it saves a lot of headache and lines of code to register the gulp task at the same time we are setting the taker task (obviously requiring the gulp module in order to do so).

What I did was this:

    ConfigRegistry.prototype.set = function set(name, fn) {
      var task = this._tasks[name] = fn.bind();
      gulp.task(name, fn);
      return task;
    };

We run CI that use SSH commands for gulp (i.e. gulp test, gulp deploy:rebuild, etc.)

Am I doing something wrong, leading me to have to insert this code? Or is this future functionality that is not completed yet?

phated commented 9 years ago

This doesn't make any sense and I don't see what you are trying to achieve that isn't already functional. Gulp is just an instance of undertaker and undertaker forwards all task registration to the custom registry.

tmcgee123 commented 9 years ago

This must be a defect then, because every task I register with undertaker is not usable through the command line. For instance, if I define an undertaker task like so (where each function in gulp.parallel is already defined and returns a stream):

CommonRegistry.prototype.init = function (takerInstance) {
  takerInstance.task('clean', gulp.parallel(cleanBuild, cleanProd, cleanPublic));
};

Then when I run gulp clean from the command line, I get "Task never defined: clean". Does that make more sense?

phated commented 9 years ago

Did you register an instance of the registry with the .registry API?

tmcgee123 commented 9 years ago

Ahh, that just may be it, I am exporting the CommonRegistry at the end of my file.

module.exports = CommonRegistry;

Do I instead?:

var UnderTaker = require('undertaker');
var taker = new UnderTaker();

...

CommonRegistry.prototype.init = function (takerInstance) {
  takerInstance.task('clean', gulp.parallel(cleanBuild, cleanProd, cleanPublic));
 //or could I possibly register an instance here instead?
};

taker.registry(new CommonRegistry());

Thank you very much for the quick replies :D

phated commented 9 years ago

Yes, the last example you posted is how to register your custom registry within undertaker. However, for series/parallel, it is recommended you use the undertaker instance that is passed to init to access those APIs.

If you are using gulp, it works the same way

var gulp = require('gulp');
...

CommonRegistry.prototype.init = function (gulpInst) {
  gulpInst.task('clean', gulpInst.parallel(cleanBuild, cleanProd, cleanPublic));
};

gulp.registry(new CommonRegistry());
tmcgee123 commented 9 years ago

That's it! Sorry for the confusion and thanks a bunch!! :+1:

phated commented 9 years ago

No problem. Do you think there is anything that can be added to the documentation to clear this up? The information about .registry() is in both undertaker and gulp documentation.

tmcgee123 commented 9 years ago

I ended up having to make these changes due to gulp-hub not being updated. Our project utilizes 10 separate JS files (I included some in the example below) that all have many tasks inside of them and then we pull all of the tasks in our Gulpfile.js (now using Undertaker).

It ends up looking something like this:

var UnderTaker = require('undertaker');
var DefaultRegistry = require('undertaker-registry');
var BuildRegistry = require('./gulp/tasks/build.js');
var CleanRegistry = require('./gulp/tasks/clean.js');
var CopyRegistry = require('./gulp/tasks/copy.js');
var DebugRegistry = require('./gulp/tasks/debug.js');
var DocsRegistry = require('./gulp/tasks/docs.js');
...
function ConfigRegistry(config) {
  DefaultRegistry.call(this);
  this.config = config;
}
util.inherits(ConfigRegistry, DefaultRegistry);
ConfigRegistry.prototype.set = function set(name, fn) {
  var task = this._tasks[name] = fn.bind(this.config);
  return task;
};
var taker = new UnderTaker();
taker.registry(new BuildRegistry());
taker.registry(new CleanRegistry());
taker.registry(new CopyRegistry());
taker.registry(new DebugRegistry());
taker.registry(new DocsRegistry());
taker.registry(new DownloadRegistry());
taker.registry(new ConfigRegistry()); 

Whereas with gulp-hub it was more straightforward as it just pulls tasks registered with gulp.task, then registers them directly with gulp.registry(). I was confused on when to use gulp.registry and when to use taker.registry and the sample above was not allowing me to access my tasks via gulp.

For example:

var HubRegistry = require('gulp-hub');
gulp.registry(new HubRegistry(['gulp/tasks/*.js']));

Would it be more clear to register the tasks in the individual files or in the Gulpfile?

Or maybe only use the gulp module in the Gulpfile? Sorry for the long reply, just wanted to fully convey how I came to where I was and possibly shed some light on where I ran into problems trying to sync up with the documentation. Hopefully this gives you some type of insight on what to add to the documentation.

phated commented 9 years ago

You don't want to create an undertaker instance in this file, you want to use the .registry() API available on gulp in your gulpfile. Also, registry's are made to share a bunch of common tasks, not 1 per registry, so I am guessing this is going to have really bad startup performance. gulp-hub has a branch where they support the new API. The custom registry API is an advanced feature and I think you are using it where you probably shouldn't be.

tmcgee123 commented 9 years ago

You are correct, the startup performance is horrible sometimes :+1: Usually on the first run it takes awhile to load all of the JS files. I picked this up from one of my co-workers that left the company and was wondering why that was happening.

I tried the new gulp-hub branch and failed to get it working, but with the new information you've just given me, I think I might be able to get it work.

Thanks again for all of the help!