-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathnpm.js
More file actions
56 lines (46 loc) · 1.66 KB
/
Copy pathnpm.js
File metadata and controls
56 lines (46 loc) · 1.66 KB
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
var npm = require('npm')
var Batch = require('./batch')
var batch = new Batch()
/**
* Get info about the versions for this dependency. Returns an object with
* `current` and `versions` properties. Where `current` is the version you'd
* get when you `npm install [package]` and `versions` is a sorted array of
* available versions for the dependency.
* @param {String} name Dependency name
* @param {Object} opts Options
* @param {Object} [opts.npm] npm configuration options
*/
function getVersionsInfo (name, opts, cb) {
if (batch.exists(name)) {
return batch.push(name, cb)
}
batch.push(name, cb)
npm.load(opts.npm || {}, function (err) {
if (err) return batch.call(name, function (cb) { cb(err) })
npm.commands.view([name, 'versions', 'time'], true, function (err, data) {
if (err) return batch.call(name, function (cb) { cb(err) })
var currentVersion = Object.keys(data)[0]
var versions = null
// `npm view 0 versions` returns {}
if (!currentVersion) {
return batch.call(name, function (cb) {
cb(new Error('Failed to get versions for ' + name))
})
}
// Some packages simply don't have a time object
if (data[currentVersion].time) {
versions = data[currentVersion].versions.sort(function (a, b) {
a = data[currentVersion].time[a]
b = data[currentVersion].time[b]
return (a < b ? -1 : (a > b ? 1 : 0))
})
} else {
versions = data[currentVersion].versions
}
batch.call(name, function (cb) {
cb(null, { current: currentVersion, versions })
})
})
})
}
exports.getVersionsInfo = getVersionsInfo