Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added webserver, restructured library to use /bin/ and /lib/ convention #5

Open
wants to merge 3 commits into from

1 participant

Commits on Aug 18, 2011
  1. @Marak
Commits on Aug 19, 2011
  1. @Marak
  2. @Marak

    [fix] Bad path in bin/npmtop

    Marak authored
This page is out of date. Refresh to see the latest.
View
7 bin/npmtop
@@ -0,0 +1,7 @@
+#!/usr/bin/env node
+
+var top = require('../lib/npmtop');
+
+top.crunch(function(results){
+ console.log(results);
+});
View
37 bin/server
@@ -0,0 +1,37 @@
+#!/usr/bin/env node
+
+var top = require('../lib/npmtop');
+var http = require('http');
+
+//
+// Default message we output while waiting for the first set of results to come back
+//
+var topResults = 'generating results...keep refreshing page...';
+
+//
+// Create a super-simple http server for serving the results
+//
+http.createServer(function (req, res) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.write(topResults);
+ res.end();
+}).listen(8000);
+
+console.log('> http server has started on port 8000');
+
+//
+// Crunch the results once to start
+//
+top.crunch(function(results){
+ topResults = results;
+});
+
+//
+// Now recrunch thr results every 60 minutes
+//
+setInterval(function(){
+ top.crunch(function(results){
+ topResults = results;
+ });
+}, 60000);
+
View
0  npmjs.js → lib/npmjs.js
File renamed without changes
View
95 lib/npmtop.js
@@ -0,0 +1,95 @@
+var npm = require('npm');
+var Hash = require('hashish');
+var sprintf = require('sprintf').sprintf;
+var argv = require('optimist').argv;
+var npmjs = require('./npmjs');
+
+
+//
+// Main method for crunching npmtop values, will take a long time to run
+//
+exports.crunch = function(callback){
+
+ npm.load({ outfd : null }, function () {
+ 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);
+ });
+ });
+
+ callback(render(authors, total));
+ }
+ else {
+ npmjs.list(function (authors) {
+ callback(render(authors, total));
+ });
+ }
+
+ });
+ });
+
+}
+
+
+function render (authors, total) {
+
+ var str = '';
+
+ str += ('Generated: ' + new Date() + ' \n\n');
+ str += ('rank percent packages author \n');
+ str += ('---- ------- -------- ------ \n');
+
+ var sorted = Object.keys(authors)
+ .sort(function (a,b) {
+ return (authors[b] - authors[a])
+ || (a.toLowerCase() < b.toLowerCase() ? -1 : 1)
+ ;
+ })
+ ;
+
+ var limit = argv._[0] || 100;
+ 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];
+ str += sprintf(
+ '%4d %.2f %% %4d.%02f %s',
+ rank + start + 1 , percent,
+ c, Math.floor((c - Math.floor(c)) * 100),
+ name
+ ) + '\n';
+
+ lastVal = authors[name];
+ fairRank = rank;
+ })
+ ;
+
+ str += ('\n Generated using: http://github.com/substack/npmtop\n\n');
+
+
+ return str;
+}
View
2  package.json
@@ -7,7 +7,7 @@
"url" : "http://github.com/substack/npmtop.git"
},
"bin" : {
- "npmtop" : "./top.js"
+ "npmtop" : "./bin/npmtop"
},
"dependencies" : {
"hashish" : ">=0.0.2",
View
80 top.js
@@ -1,80 +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.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;
- })
- ;
-}
Something went wrong with that request. Please try again.