1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | const pkg = require('get-pkg');
|
11 |
|
12 | module.exports = (names, options, cb) => {
|
13 | if (typeof options === 'function') {
|
14 | cb = options;
|
15 | options = {};
|
16 | }
|
17 |
|
18 | names = [].concat(names || []);
|
19 | let opts = options || {};
|
20 | let pending = [];
|
21 | let pkgs = [];
|
22 |
|
23 | for (let name of names) {
|
24 | let promise = pkg(name)
|
25 | .then(res => res && pkgs.push(res))
|
26 | .catch(err => {
|
27 | if (err.message === 'document not found' && opts.silent) return;
|
28 | return Promise.reject(err);
|
29 | });
|
30 |
|
31 | pending.push(promise);
|
32 | }
|
33 |
|
34 | let p = Promise.all(pending);
|
35 |
|
36 | if (typeof cb === 'function') {
|
37 | p.then(() => cb(null, pkgs)).catch(cb);
|
38 | return;
|
39 | }
|
40 |
|
41 | return p.then(() => pkgs);
|
42 | };
|