Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #3

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var Pkgs = require("./index");

Pkgs("ionicabizau", function (err, res) {
console.log(err || res);
});
51 changes: 29 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

'use strict';

var is = require('assert-kindof');
var got = require('got');
var cheerio = require('cheerio');
var JsonReq = require("jsonrequest");
var SameTime = require("same-time");

var url = 'https://www.npmjs.com/~';
var selector = '.bullet-free';
var url = 'https://www.npmjs.com/profile/__USER__/packages?offset=__OFFSET__';

/**
* List packages of the given [npmjs.com](http://npm.im) user
Expand All @@ -37,26 +35,35 @@ var selector = '.bullet-free';
* @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');
function doReq(offset, cb) {
var reqUrl = url.replace("__USER__", username).replace("__OFFSET__", offset);
JsonReq(reqUrl, function (err, res) {
if (err) { return cb(err); }
if (!res.items) {
return cb(null, []);
}
cb(null, res);
});
}
is.function(callback);

var pkgs = [];

got.get(url + username, function _cb(err, res) {
if (!is.kindof.null(err)) {
callback(err);
return;
doReq(0, function (err, res) {
if (err) { return callback(err); }
if (res.items.length < res.count) {
pkgs = res.items;
return SameTime(new Array(Math.ceil(res.count / res.items.length) - 1).join(".").split("").map(function (c, i) {
return function (cb) {
doReq((i + 1) * 100, function (err, res) {
if (err) { return cb(err); }
pkgs = pkgs.concat(res.items);
cb();
});
}
}), function (err) {
if (err) { return callback(err); }
callback(null, pkgs);
});
}

var $ = cheerio.load(res);

$(selector).first().find('li a').each(function _defaultIterator() {
pkgs.push($(this).attr('href').trim().split('/package/')[1]);
});

callback(null, pkgs);
callback(null, res.items);
});
};
25 changes: 11 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
"test-cov": "istanbul cover _mocha",
"test-travis": "istanbul cover _mocha --report lcovonly"
},
"author": {
"name": "Charlike Mike Reagent",
"email": "[email protected]",
"url": "https://github.com/tunnckoCore"
},
"author": "Charlike Mike Reagent <[email protected]> (https://github.com/tunnckoCore)",
"repository": {
"type": "git",
"url": "git://github.com/tunnckoCore/npm-pkgs.git"
Expand All @@ -29,17 +25,18 @@
"user",
"utils"
],
"license": {
"type": "MIT",
"url": "https://github.com/tunnckoCore/npm-pkgs/blob/master/license.md"
},
"dependencies": {
"assert-kindof": "~1.0.0",
"cheerio": "~0.19.0",
"got": "~2.7.0"
},
"license": "MIT",
"devDependencies": {
"istanbul-harmony": "~0.3.1",
"mocha": "~2.2.1"
},
"bugs": {
"url": "https://github.com/tunnckoCore/npm-pkgs/issues"
},
"homepage": "https://github.com/tunnckoCore/npm-pkgs",
"main": "index.js",
"dependencies": {
"same-time": "^2.0.0",
"jsonrequest": "^3.0.0"
}
}