Skip to content
Browse files

use new npmTop view

  • Loading branch information...
1 parent 7103788 commit e576951e1d80ada9fe3c18f9ff3c5f3e42b505a3 @substack committed Jun 15, 2012
Showing with 88 additions and 112 deletions.
  1. +37 −0 bin/cmd.js
  2. +43 −0 index.js
  3. +0 −19 npmjs.js
  4. +8 −12 package.json
  5. +0 −81 top.js
View
37 bin/cmd.js
@@ -0,0 +1,37 @@
+#!/usr/bin/env node
+var sprintf = require('sprintf').sprintf;
+var argv = require('optimist').argv;
+var npmtop = require('../');
+
+var arg = argv._[0];
+var who = argv.who;
+var limit = argv.limit;
+var start = argv.start || 0;
+
+if (/^\d+-\d+/.test(arg)) {
+ var xs = arg.split('-');
+ start = parseInt(xs[0], 10);
+ limit = parseInt(xs[0], 10) - start;
+}
+else if (typeof arg === 'string') {
+ who = arg;
+}
+else if (typeof arg === 'number') {
+ limit = arg;
+}
+if (limit === undefined) limit = 15;
+
+npmtop(function (err, all) {
+ if (err) return console.error(err);
+ console.log('rank percent packages author');
+ console.log('---- ------- -------- ------');
+
+ var rows = all.slice(start, start + limit);
+ rows.forEach(function (row, ix) {
+ var rank = start + ix + 1;
+ console.log(sprintf(
+ '%4d %.2f %% %4d %s',
+ row.rank, row.percent, row.packages, row.author
+ ));
+ });
+});
View
43 index.js
@@ -0,0 +1,43 @@
+var request = require('request');
+var uri = 'https://isaacs.iriscouch.com/registry/'
+ + '_design/app/_view/npmTop?group_level=1';
+
+module.exports = function (cb) {
+ getScores(function (err, scores) {
+ if (err) return cb(err);
+
+ var total = Object.keys(scores).reduce(function (sum, name) {
+ return sum + scores[name];
+ }, 0);
+
+ var sorted = Object.keys(scores)
+ .sort(function (a,b) {
+ return (scores[b] - scores[a])
+ || (a.toLowerCase() < b.toLowerCase() ? -1 : 1)
+ ;
+ })
+ .map(function (name, ix) {
+ return {
+ rank : ix + 1,
+ percent : 100 * scores[name] / total,
+ packages : scores[name],
+ author : name,
+ };
+ })
+ ;
+ cb(null, sorted);
+ });
+};
+
+function getScores (cb) {
+ request({ uri : uri, json : true }, function (err, res, body) {
+ if (err) return cb(err)
+ if (body.error) return cb(body.error)
+
+ var scores = body.rows.reduce(function (acc, row) {
+ acc[row.key] = row.value;
+ return acc;
+ }, {});
+ cb(null, scores);
+ });
+}
View
19 npmjs.js
@@ -1,19 +0,0 @@
-var http = require('http')
-
-var req = {
- host : 'search.npmjs.org',
- 'path' : '/_view/author?group=true'
-};
-
-exports.list = function (cb) {
- http.get(req, function (res) {
- var s = '';
- res.on('data', function (buf) { s += buf.toString() });
- res.on('end', function () {
- cb(JSON.parse(s).rows.reduce(function (acc, row) {
- acc[row.key] = row.value;
- return acc;
- }, {}));
- });
- });
-};
View
20 package.json
@@ -1,32 +1,28 @@
{
"name" : "npmtop",
- "version" : "0.1.2",
- "description" : "Silly program that ranks npm contributors by number of packages",
+ "version" : "0.2.0",
+ "description" : "silly program that ranks npm contributors by number of packages",
"repository" : {
"type" : "git",
"url" : "http://github.com/substack/npmtop.git"
},
+ "main" : "index.js",
"bin" : {
- "npmtop" : "./top.js"
+ "npmtop" : "bin/cmd.js"
},
"dependencies" : {
- "hashish" : ">=0.0.2",
- "sprintf" : ">=0.1.1",
- "npm" : "1.0.106",
- "optimist": ">=0.1.0"
+ "request" : "~2.9.202"
},
"keywords" : [
- "hall of fame",
- "awesome people",
- "pissing contest",
+ "npm",
"contributors",
- "npm"
+ "modules"
],
"author" : {
"name" : "James Halliday",
"email" : "mail@substack.net",
"url" : "http://substack.net"
},
"license" : "MIT/X11",
- "engine" : ["node >=0.2.0"]
+ "engine" : { "node" : ">=0.6.0" }
}
View
81 top.js
@@ -1,81 +0,0 @@
-#!/usr/bin/env node
-
-var npm = require('npm');
-var Hash = require('hashish');
-var sprintf = require('sprintf').sprintf;
-var argv = require('optimist').argv;
-
-var npmjs = require('./npmjs');
-
-npm.load({ outfd : null }, function () {
- npm.config.set('outfd', null);
- npm.commands.search([], function (err, pkgs) {
- var authors = {};
- var total = Hash(pkgs).length;
-
- if (argv.maintainers) {
- Hash(pkgs).forEach(function (pkg) {
- var users = pkg.maintainers
- .map(function (w) { return w.slice(1) })
- ;
-
- users.forEach(function (u) {
- authors[u] = (authors[u] || 0) + (1 / users.length);
- });
- });
-
- render(authors, total);
- }
- else {
- npmjs.list(function (authors) {
- render(authors, total);
- });
- }
-
- });
-});
-
-function render (authors, total) {
- console.log('rank percent packages author');
- console.log('---- ------- -------- ------');
-
- var sorted = Object.keys(authors)
- .sort(function (a,b) {
- return (authors[b] - authors[a])
- || (a.toLowerCase() < b.toLowerCase() ? -1 : 1)
- ;
- })
- ;
-
- var limit = argv._[0] || 15;
- var start = 0;
-
- if (!limit.toString().match(/^\d+$/)) {
- var who = argv._[0];
- start = sorted.indexOf(who);
- limit = 1;
- }
-
- var fairRank = 1;
- var lastVal = -1;
- sorted
- .slice(start, start + limit)
- .forEach(function (name, rank) {
- var percent = (authors[name] / total) * 100;
-
- if(authors[name] == lastVal)
- rank = fairRank;
-
- var c = authors[name];
- console.log(sprintf(
- '%4d %.2f %% %4d.%02f %s',
- rank + start + 1 , percent,
- c, Math.floor((c - Math.floor(c)) * 100),
- name
- ));
-
- lastVal = authors[name];
- fairRank = rank;
- })
- ;
-}

0 comments on commit e576951

Please sign in to comment.
Something went wrong with that request. Please try again.