UNPKG

1.01 kBJavaScriptView Raw
1/*!
2 * get-pkgs <https://github.com/jonschlinkert/get-pkgs>
3 *
4 * Copyright (c) 2014-2015, Jon Schlinkert.
5 * Licensed under the MIT License.
6 */
7
8'use strict';
9
10var utils = require('./utils');
11
12module.exports = function get(repos, pattern, cb) {
13 if (typeof pattern === 'function') {
14 cb = pattern; pattern = '*';
15 }
16
17 utils.async.reduce(arrayify(repos), [], function(acc, repo, next) {
18 pkg(repo, 'latest', function (err, json) {
19 if (err) {
20 console.log(utils.red(err + ': "') + utils.bold(repo) + '"');
21 return next(err);
22 }
23 next(null, acc.concat(utils.filter(json, pattern)));
24 });
25 }, cb);
26};
27
28function arrayify(val) {
29 return !Array.isArray(val) ? [val] : val;
30}
31
32function pkg(name, version, cb) {
33 var url = 'https://registry.npmjs.org/' + name + '/';
34
35 if (typeof version !== 'string') {
36 cb = version;
37 version = '';
38 }
39 utils.request(url + version, {}, function (err, res) {
40 if (err) return cb(err);
41 cb(null, JSON.parse(res.body));
42 });
43}