<commit_before>var es = require('event-stream');
var ngDep = require('ng-dependencies');
var extend = require('extend');
var gutil = require('gulp-util');
var Buffer = require('buffer').Buffer;
var mainBowerFiles = require('main-bower-files');
var fs = require('fs');
var path = require('path');
var PluginError = gutil.PluginError;
var File = gutil.File;

const PLUGIN_NAME = 'gulp-require-angular';
const ANGULAR_MODULE = 'ng';

function output(a) {
	var out = '';
	a.forEach(function (v) {
		out += "require('" + v + "');\n";
	});
	return out;
}
/*
console.log('Current directory: ' + process.cwd());
var bowerFiles = mainBowerFiles();
console.log('bowerFiles', bowerFiles);
bowerFiles.forEach(function (file) {
	var relative = path.relative('./src', file);
	//console.log(relative);
	var contents = fs.readFileSync(file);
	//console.log(ngDep(contents));
});
*/
function findRequiredModules(allModules, mainModule) {
	// recursive function, dive into the dependencies
	function dive(name) {
		// check if this module has already been 
		// processed in case of circular dependency
		if (dive[name]) {
			return;
		}
		dive[name] = true;
		dive.required.push(name);

		var deps = allModules[name] || [];
		deps.forEach(function (item) {
			dive(item);
		});
	}
	dive.required = [];
	dive(mainModule);
	return dive.required;
}

function pushDistinct(array, item) {
	if (array.indexOf(item) === -1) {
		array.push(item);
	}
	return array;
}

module.exports = function (mainModule, opts) {
	if (!mainModule) throw new PluginError(PLUGIN_NAME, 'Missing mainModule argument');
	var files = [], allModules = {},
		options = extend({
			filename: PLUGIN_NAME + '.generated.js',
			rebase: './',
			relativeTo: './src',
			bower: true,
			errorOnMissingModules: false
		}, opts);

	return es.through(function write(file) {
		if (file.isStream()) return this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported'));
		var deps;
		try {
			deps = ngDep(file.contents);
		} catch (err) {
			return this.emit('error', new PluginError(PLUGIN_NAME, 'Error in parsing: "' + file.relative + '", ' + err.message));
		}

		// build up listing of all modules 
		extend(allModules, deps.modules);

		// build up listing of file paths and deps
		files.push({
			file: path.relative(options.relativeTo, file.base + file.relative).replace(/\\/g, '/'),
			deps: deps
		});
	},
	function end() {
		var modules,
			defines = [],
			references = [],
			missing = [],
			finalSet;

		// return only modules are in the dependency tree of mainModule
		modules = findRequiredModules(allModules, mainModule);

		// for each module, find the file in which it is
		// defined, and any file in which it is referenced
		modules.forEach(function (module) {
			if (module === ANGULAR_MODULE) {
				return;
			}
			var isFound = false;
			files.forEach(function (file) {
				if (file.deps.modules[module]) {
					if (!isFound) {
						pushDistinct(defines, options.rebase + file.file);
						isFound = true;
					}
				} else if (file.deps.dependencies.indexOf(module) > -1) {
					pushDistinct(references, options.rebase + file.file);
				}
			});
			// if the module definition was not found in
			// any file, push to missing array
			if (!isFound) {
				pushDistinct(missing, module);
			}
		});<commit_msg>Added option to search bower components for modules<commit_after>var es = require('event-stream');
var ngDep = require('ng-dependencies');
var extend = require('extend');
var gutil = require('gulp-util');
var Buffer = require('buffer').Buffer;
var mainBowerFiles = require('main-bower-files');
var fs = require('fs');
var path = require('path');
var PluginError = gutil.PluginError;
var File = gutil.File;

const PLUGIN_NAME = 'gulp-require-angular';
const ANGULAR_MODULE = 'ng';

function output(a) {
	var out = '';
	a.forEach(function (v) {
		out += "require('" + v + "');\n";
	});
	return out;
}

function findRequiredModules(allModules, mainModule) {
	// recursive function, dive into the dependencies
	function dive(name) {
		// check if this module has already been 
		// processed in case of circular dependency
		if (dive[name]) {
			return;
		}
		dive[name] = true;
		dive.required.push(name);

		var deps = allModules[name] || [];
		deps.forEach(function (item) {
			dive(item);
		});
	}
	dive.required = [];
	dive(mainModule);
	return dive.required;
}

function pushDistinct(array, item) {
	if (array.indexOf(item) === -1) {
		array.push(item);
	}
	return array;
}

module.exports = function (mainModule, opts) {
	if (!mainModule) throw new PluginError(PLUGIN_NAME, 'Missing mainModule argument');
	var files = [], allModules = {},
		options = extend({
			filename: PLUGIN_NAME + '.generated.js',
			rebase: './',
			relativeTo: './src',
			bower: false,
			errorOnMissingModules: false
		}, opts);

	function findModules(file) {

		var deps, relative;
		try {
			deps = ngDep(file.contents);
		} catch (err) {
			return this.emit('error', new PluginError(PLUGIN_NAME, 'Error in parsing: "' + file.relative + '", ' + err.message));
		}

		// build up listing of all modules 
		extend(allModules, deps.modules);

		// build up listing of file paths and deps
		if (file.base) {
			relative = path.relative(options.relativeTo, file.base + file.relative)
		} else {
			relative = file.relative;
		}

		files.push({
			file: options.rebase + relative.replace(/\\/g, '/'),
			deps: deps
		});

	}

	return es.through(function write(file) {
		if (file.isStream()) return this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported'));
		findModules.call(this, file);
	},
	function end() {
		var modules,
			self = this,
			defines = [],
			references = [],
			missing = [],
			finalSet;

		// search bower packages too
		if (options.bower) {
			try {
				var bowerFiles = mainBowerFiles();
			} catch (err) {
				return this.emit('error', new PluginError(PLUGIN_NAME, 'main-bower-files error: ' + err.message))
			}

			bowerFiles.forEach(function (file) {
				var relative = path.relative(options.relativeTo, file);
				var contents = fs.readFileSync(file);
				findModules.call(self, {
					relative: relative,
					contents: contents
				});
			});
		}

		// return only modules are in the dependency tree of mainModule
		modules = findRequiredModules(allModules, mainModule);

		// for each module, find the file in which it is
		// defined, and any file in which it is referenced
		modules.forEach(function (module) {
			if (module === ANGULAR_MODULE) {
				return;
			}
			var isFound = false;
			files.forEach(function (file) {
				if (file.deps.modules[module]) {
					if (!isFound) {
						pushDistinct(defines, file.file);
						isFound = true;
					}
				} else if (file.deps.dependencies.indexOf(module) > -1) {
					pushDistinct(references, file.file);
				}
			});
			// if the module definition was not found in
			// any file, push to missing array
			if (!isFound) {
				pushDistinct(missing, module);
			}
		});
{'fraction_non_alphanumeric': 0.14950980392156862, 'fraction_numerical': 0.0005767012687427913, 'mean_word_length': 3.554826001313198, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2901532', 'n_tokens_mistral': 2572, 'n_tokens_neox': 2270, 'n_words': 594}