Exemplo n.º 1
0
	factory = function () {
		var watcher, emitter, pipe, args, def;
		args = arguments;
		watcher = memoized.apply(this, arguments);
		if (isPromise(watcher)) {
			def = deferred();
			emitter = def.promise;
			def.resolve(watcher);
		} else {
			emitter = ee();
		}
		pipe = eePipe(watcher, emitter);
		emitter.close = function () {
			emitter.close = noop;
			pipe.close();
			if (memoized.deleteRef.apply(this, args)) watcher.close();
		};
		return emitter;
	};
Exemplo n.º 2
0
	})(function (stats) {
		var root, result, watch, stream;

		watch = options.watch;
		stream = options.stream;

		if (watch && !linters) return {};

		// Ignore subdirectories of directories (if any)
		paths.filter(function (path, index) {
			return stats[index] === 'directory';
		}).sort(function (a, b) {
			return a.length - b.length;
		}).forEach(function (path, index, self) {
			var i, sub;
			for (i = index + 1; (sub = self[i]); ++i) {
				if (startsWith.call(sub, path + sep)) {
					index = paths.indexOf(sub);
					stats.splice(index, 1);
					paths.splice(index, 1);
					self.splice(i--, 1);
				}
			}
		});
		if (paths.length === 1) {
			result = lintDirectory(linter, paths[0], options);
			if (watch || stream) {
				eePipe(result, promise);
				promise.close = result.close;
			}
			return result;
		}

		root = commonPath.apply(null, paths);
		if (root) promise.root = root;
		else promise.root = '';

		result = {};
		return deferred.map(paths, function (path, index) {
			var lint, name, names, ignores;
			name = root ? path.slice(root.length + 1) : path;
			if (stats[index] === 'file') {
				lint = lintFile(linter, path, options);
				if (watch) {
					lint.on('change', function (report) {
						result[name] = report;
						promise.emit('change',
							{ type: 'update', name: name, report: report });
					});
					lint.on('end', function () {
						delete result[name];
						if (watch) remove.call(linters, lint);
						promise.emit('change', { type: 'remove', name: name });
					});
				}
				lint(function (report) {
					if (result[name]) {
						// Duplicate or closed
						lint.close();
						return;
					}
					result[name] = report;
					if (stream) {
						promise.emit('change', { type: 'add', name: name, report: report });
					}
				}, watch ? function () { remove.call(linters, lint); } : getNull);
			} else {
				names = [];
				ignores = {};
				lint = lintDirectory(linter, path, options);
				if (watch || stream) {
					lint.on('change', function (event) {
						var subname = name + sep + event.name, data;
						if (ignores[subname]) return;
						if (stream && !promise.resolved) {
							if (result[subname]) {
								// Duplicate
								ignores[subname] = true;
								return;
							}
							names.push(subname);
						}
						result[subname] = event.report;
						data = { type: event.type, name: subname };
						if (event.report) data.report = event.report;
						promise.emit('change', data);
					});
					if (watch) {
						lint.on('end', function () {
							if (watch) remove.call(linters, lint);
							names.forEach(function (name) {
								delete result[name];
								promise.emit('change', { type: 'remove', name: name });
							});
						});
					}
				}
				lint(function (data) {
					if (!stream) {
						forEach(data, function (report, subname) {
							subname = name + sep + subname;
							if (result[subname]) {
								// Duplicate
								ignores[subname] = true;
								return;
							}
							result[subname] = report;
							names.push(subname);
						});
					}
				}, function () {
					if (watch) remove.call(linters, lint);
					names.forEach(function (name) {
						delete result[name];
						promise.emit('change', { type: 'remove', name: name });
					});
				}, getNull);
			}
			if (watch) linters.push(lint);
			return lint;
		})(result);
	});