Skip to content

Commit

Permalink
feat(root-tasks): load tasks from root gulp file also
Browse files Browse the repository at this point in the history
  • Loading branch information
frankwallis committed Jan 10, 2015
1 parent 3ef30a9 commit f77dc8b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 32 deletions.
15 changes: 6 additions & 9 deletions examples/gulpfile.js
Expand Up @@ -8,14 +8,11 @@ var gulp = require('gulp');
var gutil = require('gulp-util');
var hub = require('../lib/index.js');

// gulp.task('compile', function(cb) {
// console.log('compiling example');
// cb();
// });
gulp.task('compile', function(cb) {
gutil.log('compiling example');
cb();
});

hub(['project1/gulpfile.js', 'proj*/gulpfile.js']);
gulp.task('default', [ 'compile' ]);

gulp.task('local', function(cb) {
console.log('this is a local task');
cb();
});
hub(['project1/gulpfile.js', 'proj*/gulpfile.js', 'gulpfile.js']);
5 changes: 1 addition & 4 deletions lib/get-subfiles.js
Expand Up @@ -38,10 +38,7 @@ module.exports = function(filelist) {
subfile.absolutePath = path.resolve(file);

// a subfile's unique name is just its relative path
// if (path.basename(subfile.relativePath) === 'gulpfile.js')
// subfile.uniqueName = path.dirname(subfile.relativePath);
// else
subfile.uniqueName = subfile.relativePath;
subfile.uniqueName = subfile.relativePath;

subfiles.push(subfile);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -31,7 +31,7 @@ function loadFiles(pattern, rootDir) {
// find all the gulpfiles - needs to happen synchronously so we create the tasks before gulp runs
var filelist = resolveGlob(pattern, rootDir)
.map(function(filename) {
return path.join(rootDir, filename);
return path.relative(process.cwd(), path.join(rootDir, filename));
});

var subfiles = getSubfiles(filelist);
Expand Down
48 changes: 33 additions & 15 deletions lib/load-subfile.js
Expand Up @@ -22,42 +22,60 @@ module.exports = function(subfile, tasks) {

// keep reference to gulp.task function
var originalTaskFn = gulp.Gulp.prototype.task;
var usingLocal = true;

// redirect gulp.task to call our function instead in case they are using our instance of gulp
gulp.Gulp.prototype.task = function (name, parm1, parm2) {
addSubtask(subfile, tasks, name, parm1, parm2);
usingLocal = false;
};

// load the file
require(subfile.absolutePath);

// if that gulpfile used its own local gulp installation
// then we need to transfer the tasks out of that into ours
var submodule = module.children[module.children.length -1];
addLocalGulpTasks(subfile, submodule, tasks);
if (usingLocal) {
var submodule = findModule(function(mod) {
return (mod.id === subfile.absolutePath);
});

if (submodule)
addLocalGulpTasks(subfile, submodule, tasks);
}

// restore gulp.task function
gulp.Gulp.prototype.task = originalTaskFn;
};

function findModule(pred, parent) {
parent = parent || module;

for (i = 0; i < parent.children.length; i ++) {
if (pred(parent.children[i]))
return parent.children[i];
}

return parent.parent ? findModule(pred, parent.parent) : undefined;
}

// I see trouble ahead...
function addLocalGulpTasks(subfile, submodule, tasks) {

submodule.children.forEach(function(mod) {
var gulpMod = findModule(function(mod) {
return (path.basename(path.dirname(mod.id)) === 'gulp');
}, submodule);

// find the gulp module
if (path.basename(path.dirname(mod.id)) === 'gulp') {
var localInst = gulpMod.exports;

// get the gulp instance
var localInst = mod.exports;
// copy all the tasks over
for (var name in localInst.tasks) {
if (localInst.tasks.hasOwnProperty(name)) {
var task = localInst.tasks[name];

// copy all the tasks over
for (var name in localInst.tasks) {
if (localInst.tasks.hasOwnProperty(name)) {
var task = localInst.tasks[name];
addSubtask(subfile, tasks, task.name, task.dep, task.fn);
}
if (!task.__hubadded) {
task.__hubadded = true;
addSubtask(subfile, tasks, task.name, task.dep, task.fn);
}
}
});
}
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "gulp-hub",
"description": "A gulp plugin to run tasks from multiple gulpfiles",
"version": "0.6.8",
"version": "0.7.0",
"homepage": "https://github.com/frankwallis/gulp-hub",
"author": {
"name": "Frank Wallis",
Expand Down
3 changes: 1 addition & 2 deletions test/index-spec.js
Expand Up @@ -58,9 +58,8 @@ describe( 'index', function () {
'./get-subfiles': spy
} );
hub( 'test-pattern' );
var absolutePath = path.join(__dirname, 'resolve-glob-return');
spy.calledOnce.should.be.true;
spy.calledWith( [absolutePath] ).should.be.true;
spy.calledWith( ['test/resolve-glob-return'] ).should.be.true;
} );

it( 'logs each file it loads, path in yellow', function () {
Expand Down

0 comments on commit f77dc8b

Please sign in to comment.