Comparing version 0.1.2 to 0.2.0
@@ -14,2 +14,16 @@ # Change Log | ||
## 0.2.0 - 2016-10-28 | ||
### Added | ||
- Implemented batch request for npm monthly download counts | ||
- Minor code comments | ||
### Changed | ||
- Deprecated Github star count in favor of npm download counts | ||
- Documented change in README | ||
## 0.1.2 - 2016-10-27 | ||
### Changed | ||
- Cleaned metadocs | ||
- Removed debugging logs | ||
## 0.1.1 - 2016-10-27 | ||
@@ -24,2 +38,2 @@ ### Added | ||
- Mocha dependencies are in place, however no tests have been written | ||
– Module discovers and reads root package.json in limited dev environments | ||
- Module discovers and reads root package.json in limited dev environments |
88
index.js
@@ -9,9 +9,5 @@ /* @flow */ | ||
var rootDir = process.cwd() + '/'; | ||
var pkgjsn; | ||
var inCount = 0; | ||
var outCount = 0; | ||
var rootDir = process.cwd() + '/'; | ||
var dependencies = {}; | ||
var event = new EventEmitter(); | ||
var event = new EventEmitter(); | ||
@@ -21,3 +17,3 @@ module.exports = function(callback) { | ||
if (err) throw err; | ||
pkgjsn = JSON.parse(data.toString('utf8')); | ||
var pkgjsn = JSON.parse(data.toString('utf8')); | ||
@@ -36,11 +32,11 @@ if (pkgjsn.dependencies) { | ||
// Manually inject Node because it's certainly part of your stack | ||
dependencies.node = { | ||
name: 'Node.js', | ||
version: process.versions.node, | ||
description: 'A JavaScript runtime ✨🐢🚀✨', | ||
stars: '' | ||
name : 'Node.js', | ||
version : process.versions.node, | ||
description : 'A JavaScript runtime ✨🐢🚀✨', | ||
stars : 'Deprecated. Use ["downloads"] instead.', | ||
downloads : 1000000 // A fake number since Node isn't downloaded on npm | ||
} | ||
gitStarCount('nodejs/node', dependencies.node); | ||
event.on('complete', function() { | ||
@@ -53,2 +49,3 @@ callback(dependencies); | ||
// Add modules and their versions to the dependencies object | ||
function seedDependencies(obj) { | ||
@@ -63,4 +60,6 @@ Object.keys(obj) | ||
}); | ||
fetchModuleDownloads(); | ||
} | ||
// Fetch each module's package.json and add details from it | ||
function gatherDetails(module) { | ||
@@ -75,16 +74,12 @@ var modulePath = rootDir + 'node_modules/' + module + '/package.json'; | ||
dependency.description = details.description; | ||
if (details.repository) { | ||
gitStarCount(details.repository.url, dependency); | ||
} | ||
// Retaining the deprecated stars key for this version | ||
dependency.stars = 'Deprecated. Use ["downloads"] instead.' | ||
}); | ||
} | ||
function gitStarCount(url, dependency) { | ||
var parse = /git.*\:\/\/git@\w*\.*com\/|\w*.*\:\/\/\w*\.*com\/|\.git/g | ||
inCount++; | ||
// Batch retrieve the npm download counts for all modules for the past month | ||
function fetchModuleDownloads() { | ||
var httpOptions = { | ||
hostname: 'api.github.com', | ||
path: '/repos/' + url.replace(parse, ''), | ||
hostname: 'api.npmjs.org', | ||
path: '/downloads/point/last-month/' + Object.keys(dependencies).join(','), | ||
method: 'GET', | ||
@@ -101,8 +96,16 @@ headers: {} | ||
}).on('error', function(err) { | ||
outCount++; | ||
console.error(err); | ||
}).on('close', function() { | ||
outCount++; | ||
dependency.stars = JSON.parse(result.toString('utf8')).stargazers_count; | ||
var resultObj = JSON.parse(result.toString('utf8')); | ||
var inCount = Object.keys(resultObj).length; | ||
var outCount = 0; | ||
Object.keys(resultObj).forEach(function(key) { | ||
if (dependencies[key]) { | ||
dependencies[key].downloads = resultObj[key] ? | ||
resultObj[key].downloads : null; | ||
outCount++; | ||
} | ||
}); | ||
if (inCount === outCount) { | ||
@@ -114,1 +117,34 @@ event.emit('complete'); | ||
} | ||
// Leaving this here for posterity. Feel free to fork a version that uses | ||
// Github stars | ||
// function gitStarCount(url, dependency) { | ||
// var parse = /git.*\:\/\/git@\w*\.*com\/|\w*.*\:\/\/\w*\.*com\/|\.git/g | ||
// inCount++; | ||
// var httpOptions = { | ||
// hostname: 'api.github.com', | ||
// path: '/repos/' + url.replace(parse, ''), | ||
// method: 'GET', | ||
// headers: {} | ||
// }; | ||
// httpOptions.headers['User-Agent'] = 'cachilders/backpat'; | ||
// var result = ''; | ||
// https.get(httpOptions, function(res) { | ||
// res.on('data', function(data) { | ||
// result += data; | ||
// }); | ||
// }).on('error', function(err) { | ||
// outCount++; | ||
// console.error(err); | ||
// }).on('close', function() { | ||
// outCount++; | ||
// dependency.stars = JSON.parse(result.toString('utf8')).stargazers_count; | ||
// if (inCount === outCount) { | ||
// event.emit('complete'); | ||
// } | ||
// }); | ||
// } |
{ | ||
"name": "backpat", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "A simple tool for high-fiving your tech stack", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# Backpat | ||
Backpat is a simple tool for use in the automating the production of tech | ||
stack notes in projects. (Looking at you, student coders.) | ||
Backpat is a simple tool for use in automating the production of tech stack | ||
notes in projects. (Looking at you, student coders.) | ||
@@ -26,11 +26,13 @@ Require it like so: | ||
name: 'eslint', | ||
url: 'http://eslint.org', | ||
url: 'https://github.com/eslint/eslint.git', | ||
description: 'An AST-based pattern checker for JavaScript.', | ||
stars: 6170 }, | ||
stars: 'Deprecated. Use ["downloads"] instead.', | ||
downloads: 4038383 }, | ||
mocha: | ||
{ version: '3.1.2', | ||
name: 'mocha', | ||
url: 'https://mochajs.org', | ||
url: 'https://github.com/mochajs/mocha.git', | ||
description: 'simple, flexible, fun test framework', | ||
stars: 10661 } | ||
stars: 'Deprecated. Use ["downloads"] instead.', | ||
downloads: 4001598 }} | ||
``` | ||
@@ -41,6 +43,8 @@ | ||
**Please note:** Github strongly limits unauthenticated API calls. If you get | ||
undefined for many or all of your dependencies' stars, the limit for the app | ||
has reached for the given hour. This is less than ideal and a solution is in | ||
the works. | ||
**Please note:** Due to Github's _strong_ restriction of unauthenticated calls | ||
I've transitioned from stargazer_count to npm's download count for the prior | ||
month. I'll remove the stars key entirely in a future update. For now it | ||
and the code responsible for the request remain in the project. Fork at | ||
your leisure to build that alternate timeline. Bonus: it's significantly | ||
faster since the npm API allows for batch requests. | ||
@@ -51,3 +55,3 @@ Also worth noting: it's all async – so don't worry if you've got kitchen- | ||
This is a nascent module that is bound to require some TLC. If you encounter | ||
any rough edges, please don't hesitate to drop me a line. Oh, and feel free to | ||
submit at PR. | ||
any rough edges, please don't hesitate to drop me a line. Oh, and _feel free to | ||
submit at PR_. There's still much to be done. |
8172
122
54