-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (41 loc) · 1.27 KB
/
index.js
File metadata and controls
52 lines (41 loc) · 1.27 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
'use strict'
const packument = require('packument')
const semver = require('semver')
module.exports = factory(packument)
module.exports.factory = factory
function factory (packument, defaults) {
return function (name, opts, callback) {
if (typeof opts === 'string') {
opts = { version: opts }
} else if (typeof opts === 'function') {
callback = opts
opts = null
}
opts = Object.assign({}, defaults, opts)
getPackage(packument, name, opts, callback)
}
}
function getPackage (packument, name, opts, callback) {
const version = opts.version || 'latest'
packument(name, opts, function (err, packument, headers) {
if (err) return callback(err)
let pkg
try {
if (packument['dist-tags'][version]) {
pkg = packument.versions[packument['dist-tags'][version]]
} else if (packument.versions[version]) {
pkg = packument.versions[version]
} else {
const versions = Object.keys(packument.versions)
const match = semver.maxSatisfying(versions, version)
pkg = match && packument.versions[match]
}
} catch (err) {
return callback(err)
}
if (pkg == null) {
return callback(new Error('package version does not exist'))
}
callback(null, pkg, headers)
})
}