{"size": 3099, "ext": "js", "max_stars_repo_path": "Gen 3/mwc-nfv-hackathon/wizard/node_modules/gulp-hub/lib/hub-registry.js", "max_stars_repo_name": "vmware/bellhop", "max_stars_repo_stars_event_min_datetime": "2018-12-19T15:31:56.000Z", "max_stars_repo_stars_event_max_datetime": "2018-12-19T15:31:56.000Z", "max_issues_repo_path": "Gen 3/mwc-nfv-hackathon/wizard/node_modules/gulp-hub/lib/hub-registry.js", "max_issues_repo_name": "vmware/bellhop", "max_issues_repo_issues_event_min_datetime": "2018-07-13T14:13:05.000Z", "max_issues_repo_issues_event_max_datetime": "2018-12-17T05:56:50.000Z", "max_forks_repo_path": "Gen 3/mwc-nfv-hackathon/wizard/node_modules/gulp-hub/lib/hub-registry.js", "max_forks_repo_name": "vmware/bellhop", "max_forks_repo_forks_event_min_datetime": "2018-07-16T11:39:39.000Z", "max_forks_repo_forks_event_max_datetime": "2018-07-16T11:39:39.000Z"}
{"max_stars_count": 1, "max_issues_count": 58, "max_forks_count": 1, "avg_line_length": 26.4871794872, "max_line_length": 116, "alphanum_fraction": 0.7015166183}
/*
 * gulp-hub
 * https://github.com/frankwallis/gulp-hub
 *
 * Copyright (c) 2014 Frank Wallis
 * Licensed under the MIT license.
 */
var path = require('path');
var util = require('util');

var _each = require('lodash.foreach');
var gutil = require('gulp-util');

var ForwardRegistry = require('undertaker-forward-reference');

var resolveGlob = require('./resolve-glob');
var loadSubfile = require('./load-subfile');

function HubRegistry(pattern) {
	if (!(this instanceof HubRegistry))
		return new HubRegistry(pattern);

	ForwardRegistry.call(this);

	this._registry = {};
	this._callsite = this.getCallSite();

	// resolve the list of gulpfiles
	this._gulpfiles = resolveGlob(pattern, path.dirname(this._callsite));

	// setup an internal subfile in case tasks are added from the existing registry
	this._registry[this._callsite] = new ForwardRegistry();
}

util.inherits(HubRegistry, ForwardRegistry);

/**
 * Get the directory of the file which gulp-hub was called from
 * @returns string callsite - the file containing the function which called the function which called this function.
 */
HubRegistry.prototype.getCallSite = function() {
	var callsite = require('callsite');
	var stack = callsite();
	return stack[2].getFileName();
}

/**
 * Registry API
 * Initalise the registry
 */
HubRegistry.prototype.init = function (gulp) {
	// load the subfiles into the registry map
	var self = this;
	_each(this._gulpfiles, function(gulpfile) {
		gutil.log('Loading', gutil.colors.yellow(gulpfile));
		self._registry[gulpfile] = loadSubfile(gulp, gulpfile);
	});

	var master = {};
	var sandboxedGulp = new gulp.Gulp();
//gutil.log('here')
	// assign new, unique names to each of the subtasks,
	// this means recreating all the subtasks using gulp.task
	_each(this._registry, function(subregistry, subfileName) {
		var subtasks = subregistry.tasks();

		_each(subtasks, function(subtask, name) {
			var subname = subfileName + '-' + name;

			// gutil.log(subtask);
			// gutil.log(subtask.unwrap());

			// create the gulp task
			// sandboxedGulp.task(subname, subtask.unwrap());

			// // retrieve the taskWrapper
			// var newtask = sandboxedGulp.registry().get(subname).unwrap();

			var newtask = subtask.unwrap();
			newtask.displayName = subname;
			
			// add it to the list of functions the master task will call
			if (master[name])
				master[name].push(newtask);
			else
				master[name] = [ newtask ];
		});
	});

	sandboxedGulp = new gulp.Gulp();

	// generate the master tasks
	_each(master, function(subtasks, name) {
		if (subtasks.length == 1)
			sandboxedGulp.task(name, subtasks[0]);
		else
			sandboxedGulp.task(name, gulp.series(subtasks));
	})

	this._tasks = sandboxedGulp.registry().tasks();
};

/**
 * Registry API
 * Register a task
 *
 * @param {string} name - the name of the task
 * @param {function} fn - the task function
 */
HubRegistry.prototype.set = function (name, fn) {
	// TODO: what if this collides with a master task?
	ForwardRegistry.prototype.set.call(this, name, fn);
	this._registry[this._callsite].set(name, fn);
};

module.exports = HubRegistry;
