1 | 'use strict';
|
2 |
|
3 | var debug = require('debug')('npmjs::packages')
|
4 | , normalize = require('../normalize')
|
5 | , licenses = require('licenses')
|
6 | , semver = require('../semver');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function Packages(api) {
|
16 | this.api = api;
|
17 |
|
18 | this.send = api.send.bind(api);
|
19 | this.view = api.view.bind(api);
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | Packages.prototype.get = function get(name, fn) {
|
35 | return this.send(name.replace('@', '/'), fn).map(normalize.packages);
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | Packages.prototype.depended = function depended(name, fn) {
|
47 | return this.view('dependedUpon', {
|
48 | key: name
|
49 | }, fn)
|
50 | .map(this.api.map.simple)
|
51 | .filter(Boolean);
|
52 | };
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | Packages.prototype.starred = function starred(name, fn) {
|
63 | return this.view('browseStarPackage', {
|
64 | key: name
|
65 | }, fn).map(function map(data) {
|
66 | return data[2];
|
67 | });
|
68 | };
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | Packages.prototype.keyword = function keyword(name, fn) {
|
79 | return this.view('byKeyword', {
|
80 | key: name
|
81 | }, fn)
|
82 | .map(this.api.map.simple)
|
83 | .filter(Boolean);
|
84 | };
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | Packages.prototype.releases = function releases(name, fn) {
|
94 | var api = this.api;
|
95 |
|
96 | return this.details(name, fn).emits(function emit(data, add) {
|
97 | if (!data.versions) return;
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | Object.keys(data.versions).forEach(function addmore(version) {
|
103 | var release = data.versions[version];
|
104 | release.date = data.time[version];
|
105 |
|
106 | add(normalize.packages(release, data));
|
107 | });
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | if ('dist-tags' in data) Object.keys(data['dist-tags']).forEach(function (key) {
|
113 | if (key in data.versions) return;
|
114 |
|
115 | var version = data['dist-tags'][key]
|
116 | , release;
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | if (!version || !(version in data.versions)) return;
|
125 |
|
126 | release = api.merge({}, data.versions[version]);
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | release.date = data.time[version];
|
134 | release.tag = key;
|
135 |
|
136 | add(normalize.packages(release, data));
|
137 | });
|
138 |
|
139 | return false;
|
140 | }).reduce(function reduce(memo, release) {
|
141 | memo[release.tag || release.version] = release;
|
142 | return memo;
|
143 | }, {});
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | Packages.prototype.release = function release(name, version, fn) {
|
156 | return this.details(name +'/'+ version, fn).map(normalize.packages);
|
157 | };
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | Packages.prototype.range = function ranged(name, range, fn) {
|
169 | if (!semver.validRange(range)) return fn(new Error('Invalid semver range'));
|
170 |
|
171 | return this.releases(name, function releases(err, versions) {
|
172 | if (err) return fn(err);
|
173 |
|
174 | if (range in versions) {
|
175 | debug('found and direct range (%s) match for %s', range, name);
|
176 | return fn(undefined, versions[range]);
|
177 | }
|
178 |
|
179 | var version = semver.maxSatisfying(Object.keys(versions), range);
|
180 |
|
181 | debug('max satisfying version for %s is %s', name, version);
|
182 | fn(undefined, versions[version]);
|
183 | });
|
184 | };
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | Packages.prototype.details = function details(name, fn) {
|
200 | var packages = this;
|
201 |
|
202 | return this.get(name, fn).async.map(function map(data, next) {
|
203 | licenses(data, {
|
204 | githulk: packages.api.githulk,
|
205 | npmjs: packages
|
206 | }, function parsed(err, licenses) {
|
207 | data.licenses = licenses;
|
208 |
|
209 | if (err) debug('failed to detect license: %s', err.message);
|
210 | return next(err, data);
|
211 | });
|
212 | });
|
213 | };
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | module.exports = Packages;
|