npf
Version:
Node package finder
70 lines (63 loc) • 1.92 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var ENDPOINT, argv, colors, readline;
argv = require('argv');
colors = require('colors');
readline = require('readline');
ENDPOINT = 'https://www.npmjs.com/search';
this.run = function() {
var fetch, http, options, pkg, q, ref, rl, targets;
ref = argv.option([
{
name: 'version',
short: 'v',
type: 'bool'
}
]).run(), targets = ref.targets, options = ref.options;
if (options.version) {
pkg = require(__dirname + "/../package.json");
console.log(pkg.name + "-" + pkg.version);
process.exit();
}
if (targets.length === 0) {
console.error("search words are empty");
process.exit(1);
}
q = targets.map(encodeURIComponent).join('+');
http = require('cheerio-httpcli');
rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
process.on('SIGINT', function() {
console.log('');
return process.exit();
});
fetch = function(page) {
return http.fetch(ENDPOINT, {
q: q,
page: page
}, function(err, $, res) {
$('.package-details').each(function(i, e) {
var desc, name, star, ver;
e = $(e);
name = e.find('.name').text();
desc = e.find('.description').text().replace(/[\s\n]+/g, ' ').blue;
star = "*".repeat(Number(e.find('.stars').text())).yellow;
ver = e.find('.version').text();
return console.log(name + " " + ver + star + " " + desc);
});
if ($('.pagination .next').length > 0) {
process.stdout.write('[ENTER] to more...');
return rl.on('line', function() {
return fetch(page + 1);
});
} else {
return process.exit();
}
});
};
return fetch(1);
};
}).call(this);