forked from jryans/npmdep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (115 loc) · 3.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var npm = require('npm');
var Seq = require('seq');
var Hash = require('hashish');
var fs = require('fs');
var path = require('path');
var cacheFile = __dirname + '/.cache';
var packages = null;
exports.load = function (cb) {
if (packages) {
cb(null, packages)
}
else fs.exists(cacheFile, function (ex) {
if (!ex) {
cb('No packages cached. Run `npmdep update` first.')
}
else fs.readFile(cacheFile, 'utf8', function (err, file) {
if (err) cb(err)
else cb(null, JSON.parse(file));
})
})
};
exports.requires = function (target, cb) {
exports.load(function (err, pkgs) {
if (err) cb(err)
else if (!pkgs[target]) cb('No such package "' + target + '"')
else cb(null, Hash(pkgs).filter(function (pkg, name) {
return pkg.dependencies[target]
}).keys)
})
};
exports.deps = exports.dependencies = function (target, cb) {
exports.load(function (err, pkgs) {
if (err) cb(err)
else cb(null, pkgs[target].dependencies)
})
};
exports.tree = function (start, cb) {
exports.load(function (err, pkgs) {
if (err) cb(err)
else if (!pkgs[start]) cb('No such package "' + start + '"')
else cb(null, (function walk (node) {
var tree = {};
var deps = Object.keys(pkgs[node].dependencies || {});
deps.forEach(function (name) {
tree[name] = walk(name)
})
return tree;
})(start));
})
};
exports.update = function (cb) {
function after (err, pkgs, updated) {
if (err) cb(err)
else {
packages = pkgs;
cb(null, pkgs, updated);
}
}
fs.exists(cacheFile, function (ex) {
if (ex) {
exports.load(function (err, pkgs) {
if (err) cb(err)
else updateCache(pkgs, after);
});
}
else updateCache({}, after)
});
};
function updateCache (cached, cb) {
// Skip over these broken packages.
var blacklist = [ 'balancer' ];
npm.load({ outfd : null }, function () {
npm.commands.list(['latest'], function (err, pkgs) {
var names = Object.keys(pkgs)
.filter(function (name) {
return blacklist.indexOf(name.split('@')[0]) < 0
})
;
var newNames = [];
names.forEach(function (name) {
var nv = name.split('@');
var n = nv[0], v = nv[1];
if (!cached[n] || cached[n].latest !== v) {
newNames.push(name);
}
});
// This part is crazy slow and so wrong.
// If somebody wants to use the proper couch api, patches welcome.
Seq.ap(newNames)
.parMap(4, function (name) {
console.log(name);
setTimeout((function () {
npm.commands.view([ name, 'dependencies' ], this);
}).bind(this), 250);
})
.seq(function () {
var updated = {};
Hash(newNames, this.stack).forEach(function (pkg, name) {
var nv = name.split('@');
var n = nv[0], v = nv[1];
updated[n] = cached[n] = {
latest : v,
dependencies : pkg[name] && pkg[name].dependencies || {}
};
});
if (newNames.length) {
fs.writeFile(cacheFile, JSON.stringify(cached));
}
cb(null, cached, updated);
})
.catch(cb)
;
})
});
}