Skip to content

Commit

Permalink
[refactor] Complete refactor work for upgrade to npm 1.0. Fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed May 21, 2011
1 parent b350f91 commit d758b48
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions lib/require-analyzer.js
Expand Up @@ -13,6 +13,7 @@ var util = require('util'),
spawn = require('child_process').spawn,
npm = require('npm'),
npmout = require('npm/lib/utils/output'),
npmls = require('npm/lib/utils/read-installed'),
semver = require('semver'),
findit = require('findit');

Expand Down Expand Up @@ -154,30 +155,29 @@ analyzer.npmAnalyze = function (deps, options, callback) {
// Then see if it depends on any other dependencies that are in the
// list so those dependencies may be removed (only if `options.reduce` is set).
//
npm.commands.ls([], function (err, results) {
npmls(options.root, function (err, result) {
if (err) {
return callback(err);
}
else if (!result || !result.dependencies) {
return callback(null);
}

Object.keys(results).forEach(function (result) {
var parts = result.split('@'),
pkg = parts[0],
version = parts[1];

Object.keys(result.dependencies).forEach(function (pkg) {
if (deps.indexOf(pkg) !== -1) {
pkgs[pkg] = results[result].data.versions[version];
pkgs[pkg] = result.dependencies[pkg];
}
});

emitter.emit('search', pkgs);
if (!options.reduce) {
npmout.write = _write;
return callback(null, pkgs);
}

var reduced = analyzer.merge({}, pkgs),
suspect = {};

deps.forEach(function (dep) {
if (pkgs[dep] && pkgs[dep].dependencies) {
Object.keys(pkgs[dep].dependencies).forEach(function (cdep) {
Expand All @@ -188,10 +188,10 @@ analyzer.npmAnalyze = function (deps, options, callback) {
});
}
});

emitter.emit('reduce', reduced, suspect);
npmout.write = _write;
callback(null, reduced, suspect);
callback(null, reduced, suspect);
});
});

Expand Down Expand Up @@ -361,11 +361,16 @@ analyzer.file = function (options, callback) {
// If a timeout has been set then exit the
// process after the specified timespan
//
setTimeout(function () {
var timeoutId = setTimeout(function () {
deps.kill();
}, options.timeout);

deps.on('exit', function () {
//
// Remove the timeout now that we have exited.
//
clearTimeout(timeoutId);

//
// When the process is complete remove any `core` node.js packages
// (i.e. packages in `process.bindings('natives')`) and any packages
Expand Down

0 comments on commit d758b48

Please sign in to comment.