Permalink
Browse files

added a analyzer.path method that calls the appropriate function for …

…a path

as it's mainly the code of .analyze, use it there
  • Loading branch information...
1 parent c802fea commit 5d32a739a3edc64f939b481311a9626c617cd423 @fb55 fb55 committed Sep 7, 2012
Showing with 52 additions and 41 deletions.
  1. +52 −41 lib/require-analyzer.js
View
@@ -23,8 +23,7 @@ var analyzer = exports;
// ### function analyze (options, callback)
// #### @options {Object} Options to analyze against
// #### @callback {function} Continuation to respond to when complete.
-// Calls the appropriate `require-analyzer` method based on the result
-// from `fs.stats()` on `options.target`. When dependencies are returned,
+// Calls `path`. When dependencies are returned,
// `npmAnalyze()` is called with `options` and the resulting object is
// returned to `callback`. Also returns an event emitter which outputs
// data at various stages of completion with events:
@@ -35,14 +34,57 @@ var analyzer = exports;
//
analyzer.analyze = function (options, callback) {
var emitter = new events.EventEmitter();
+
+ //
+ // let path determine what to do
+ //
+ analyzer.path(options, function (err, deps) {
+ if (err) {
+ emitter.emit('childError', err);
+ return callback(err);
+ }
+
+ // Emit the `dependencies` event for streaming results.
+ emitter.emit('dependencies', deps);
+
+ if (options.npm === false || !deps || deps.length === 0) {
+ return callback(null, deps);
+ }
+
+ var npmEmitter = analyzer.npmAnalyze(deps, options, function (nerr, reduced, suspect) {
+ return callback(err || nerr, reduced, suspect);
+ });
+
+ //
+ // Re-emit the `search` and `reduce` events from the `npmEmitter`
+ // for streaming results.
+ //
+ ['search', 'reduce'].forEach(function (ev) {
+ npmEmitter.on(ev, function () {
+ var args = Array.prototype.slice.call(arguments);
+ args.unshift(ev);
+ emitter.emit.apply(emitter, args);
+ });
+ });
+ });
+
+ return emitter;
+};
+//
+// ### function path (options, callback)
+// #### @options {Object} Options to analyze against
+// #### @callback {function} Continuation to respond to when complete.
+// Calls the appropriate `require-analyzer` method based on the result
+// from `fs.stats()` on `options.target`.
+//
+analyzer.path = function(options, callback){
if (!options || !options.target) {
//
// If there are no `options` and no `options.target` property
// respond with the appropriate error.
//
callback(new Error('options and options.target are required'));
- return emitter;
}
//
@@ -53,50 +95,19 @@ analyzer.analyze = function (options, callback) {
if (err) {
return callback(err);
}
-
- var analyzeFn, rootDir;
- if (stats.isDirectory()) {
- analyzeFn = analyzer.dir;
+ else if (stats.isDirectory()) {
+ analyzer.dir(options, callback);
}
else if (stats.isFile()) {
- analyzeFn = analyzer.file;
+ if("fileFilter" in options && !options.fileFilter(options.target)) return;
+ analyzer.file(options, callback);
}
else {
- return callback(new Error(options.target + ' is not a file or a directory.'));
+ err = new Error(options.target + ' is not a file or a directory.');
+ err.code = "UNSUPPORTED_TYPE";
+ callback(err);
}
-
- analyzeFn.call(null, options, function (err, deps) {
- if (err) {
- emitter.emit('childError', err);
- return callback(err);
- }
-
- // Emit the `dependencies` event for streaming results.
- emitter.emit('dependencies', deps);
-
- if (options.npm === false || !deps || deps.length === 0) {
- return callback(null, deps);
- }
-
- var npmEmitter = analyzer.npmAnalyze(deps, options, function (nerr, reduced, suspect) {
- return callback(err || nerr, reduced, suspect);
- });
-
- //
- // Re-emit the `search` and `reduce` events from the `npmEmitter`
- // for streaming results.
- //
- ['search', 'reduce'].forEach(function (ev) {
- npmEmitter.on(ev, function () {
- var args = Array.prototype.slice.call(arguments);
- args.unshift(ev);
- emitter.emit.apply(emitter, args);
- });
- });
- });
});
-
- return emitter;
};
//

0 comments on commit 5d32a73

Please sign in to comment.