-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
62 lines (53 loc) · 1.39 KB
/
index.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
/**
* npm-pkgs <https://github.com/tunnckoCore/npm-pkgs>
*
* Copyright (c) 2015 Charlike Mike Reagent, contributors.
* Released under the MIT license.
*/
'use strict';
var is = require('assert-kindof');
var got = require('got');
var cheerio = require('cheerio');
var url = 'https://www.npmjs.com/~';
var selector = '.bullet-free';
/**
* List packages of the given [npmjs.com](http://npm.im) user
*
* **Example**
* ```js
* var npmPkgs = require('npm-pkgs');
*
* npmPkgs('tunnckocore', function _cb(err, res) {
* if (err) {
* console.error(err);
* return;
* }
* console.log(res);
* //=> ['list', 'of', 'user', 'packages']
* });
* ```
*
* @name npmPkgs
* @param {String} `<username>` non emptry string, npm username
* @param {Function} `<callback>` node-style callback `(err, res)`
* @api public
*/
module.exports = function npmPkgs(username, callback) {
is.string(username);
if (username.length === 0) {
throw new Error('[npm-pkgs] expect `username` to be non empty string');
}
is.function(callback);
var pkgs = [];
got.get(url + username, function _cb(err, res) {
if (!is.kindof.null(err)) {
callback(err);
return;
}
var $ = cheerio.load(res);
$(selector).first().find('li a').each(function _defaultIterator() {
pkgs.push($(this).attr('href').trim().split('/package/')[1]);
});
callback(null, pkgs);
});
};