forked from gotenxds/NpmPackageDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpmApi.js
122 lines (108 loc) · 3.62 KB
/
npmApi.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
'use strict';
let Promise = require('bluebird'),
_ = require('lodash'),
Download = require('download'),
downloadStatus = require('download-status'),
util = require('util'),
winston = require('winston'),
validUrl = require('valid-url'),
got = require('got'),
nameUtils = require('./nameUtils'),
path = require('path');
module.exports = {
getVersionsOf (name) {
return new Promise(resolve => {
getJson(buildUrl(name))
.then(data => {
resolve([name, _.keys(data.versions)]);
});
});
},
getDependenciesOf (npmPackage) {
if (!npmPackage.version) {
return this.getDependenciesOfLatest(npmPackage);
} else {
return new Promise(resolve => {
getJson(buildUrl(npmPackage.name))
.then(data => {
resolve(data.versions[npmPackage.version].dependencies);
})
});
}
},
getDevDependenciesOf (npmPackage) {
if (!npmPackage.version) {
return this.getDevDependenciesOfLatest(npmPackage);
} else {
return new Promise(resolve => {
getJson(buildUrl(npmPackage.name))
.then(data => {
resolve(data.versions[npmPackage.version].devDependencies);
})
});
}
},
getDependenciesOfLatest (name) {
return new Promise(resolve => {
this.getLatestOf(name)
.then(data => {
resolve(data.dependencies || data.peerDependencies);
})
});
},
getDevDependenciesOfLatest (name) {
return new Promise(resolve => {
this.getLatestOf(name)
.then(data => {
resolve(data.devDependencies);
})
});
},
getLatestOf (name) {
// Npm site is does not allow /latest for scoped packages (WTF)
if (nameUtils.isScoped(name)){
return new Promise(resolve => {
getJson(`http://registry.npmjs.org/${name}`)
.then((data => resolve(data.versions[data['dist-tags'].latest])));
})
}
return getJson(`${buildUrl(name)}latest`);
},
getLatestVersionOf: function (name) {
return new Promise(resolve => {
this.getLatestOf(name)
.then(data => resolve(data.version))
})
},
downloadTarBallOf (name, versions, downloadDir) {
let download = Promise.promisifyAll(new Download({retries: 10}));
versions.forEach(version => {
let url = buildUrl(name, version);
winston.info('Downloading ', url);
download.get(url);
});
return download
.use(downloadStatus())
.dest(path.join(downloadDir, name, '-'))
.runAsync();
}
};
function buildUrl(name, version) {
if (validUrl.isUri(version)) {
return version;
}
else if (version){
return util.format("https://registry.npmjs.org/%s/-/%s-%s.tgz", name.replace('%2f', '/'), nameUtils.unscopeName(name), version);
}
return util.format("https://registry.npmjs.org/%s/", name.replace('/', '%2f'));
}
function getJson(url) {
return new Promise((resolve) => {
got(url, {json: true})
.then(data => resolve(data.body))
.catch((err) => {
winston.error(`${err.toString()} : ${err.host + err.path} -> Was unable to find package: ${err.path.slice(1)}` );
resolve({});
});
})
}