Skip to content
Browse files

get debug output via --debug

  • Loading branch information...
1 parent b842743 commit 68031cf31ec85b672e4745672201bb971f130df5 @dylang committed Aug 6, 2011
Showing with 97 additions and 51 deletions.
  1. +35 −17 lib/cli.js
  2. +9 −6 lib/github.js
  3. +16 −0 lib/log.js
  4. +5 −0 lib/npm.js
  5. +32 −28 lib/output.js
View
52 lib/cli.js
@@ -1,38 +1,56 @@
var CLI = require('cli');
var Output = require('./output');
+var Step = require('step');
+var Github = require('./github');
+var Npm = require('./npm');
+var log = require('./log');
+
CLI.parse();
/*
Usage:
changelog request
changelog http://github.com/mikeal/request
-
- With plugins?
- changelog gem httparty
- changelog brew redis
*/
CLI.main(function (args, options) {
var project = args[0];
var output = options.json ? Output.json : Output.markdown;
- if (!project) {
- console.log('Usage: changelog { npm package name or github.com repo url }');
- } else {
- if (project.match(/github.com/)) {
- var repo = project.match(/github\.com\/([^\/]*\/[^\/]*)/);
- if (repo && repo[1]) {
- repo = repo[1].replace(/\.git$/, '');
- require('./github').changelog(repo, output);
+ log.debugEnabled(options.debug);
+
+ Step(
+ function() {
+ if (!project) {
+ throw new Error('Usage: changelog { npm package name or github.com repo url }');
} else {
- console.error('bad repo descriptor');
+ if (project.match(/github.com/)) {
+ var repo = project.match(/github\.com\/([^\/]*\/[^\/]*)/);
+ if (repo && repo[1]) {
+ repo = repo[1].replace(/\.git$/, '');
+ Github.changelog(repo, this);
+ } else {
+ throw new Error('Bad repo url: ' + project);
+ }
+ } else {
+ Npm.changelog(project, this);
+ }
+ }
+ },
+ function(err, data) {
+ if (err) { throw err; }
+ output(data, this);
+ },
+ function(err, data) {
+ if (err) {
+ console.error(err.message);
+ }
+ else {
+ console.info(data);
}
- } else {
- require('./npm').changelog(project, output);
}
- }
-
+ );
});
View
15 lib/github.js
@@ -1,7 +1,8 @@
-var URL = require('url');
+var URL = require('url');
var Request = require('request');
-var Step = require('step');
-var Util = require('./util');
+var Step = require('step');
+var Util = require('./util');
+var log = require('./log');
function changes(options, cb) {
// TODO: better argument check and url fix, or just use npm project name
@@ -16,9 +17,11 @@ function changes(options, cb) {
// remove leading /'s
project = project.replace(/^\//g, '');
- var api = 'https://api.github.com/repos/' + project + '/commits?per_page=100';
- //log('api:', api);
- Request({uri: api}, function (error, response, body) {
+ var url = 'https://api.github.com/repos/' + project + '/commits?per_page=100';
+ log.debug('requesting: ' + url);
+ Request({uri: url}, function (error, response, body) {
+ log.debug('complete: ' + url);
+
// TODO: better check for return data
var changes = [];
View
16 lib/log.js
@@ -0,0 +1,16 @@
+var showDebugMessages = false;
+
+function debug(message) {
+ if (showDebugMessages) {
+ console.info(message);
+ }
+}
+
+function debugEnabled(bool) {
+ showDebugMessages = !!bool;
+}
+
+module.exports = {
+ debug: debug,
+ debugEnabled: debugEnabled
+};
View
5 lib/npm.js
@@ -2,6 +2,8 @@ var Request = require('request');
var Step = require('step');
var Github = require('./github');
var Semver = require('semver');
+var log = require('./log');
+
function sortVersions (a, b) {
if (Semver.gt(a, b)) { return -1; }
@@ -12,7 +14,10 @@ function sortVersions (a, b) {
function versions(packageName, cb) {
var v = [];
var url = 'http://search.npmjs.org/api/' + packageName;
+ log.debug('requesting: ' + url);
Request({ uri: url }, function(err, response, body) {
+ log.debug('complete: ' + url);
+
var data, repo;
if (!err && body) {
try {
View
60 lib/output.js
@@ -1,37 +1,41 @@
var Util = require('./util');
-function markdown(err, changelog) {
- if (err || !changelog) {
- console.error(err && err.message || 'No Changelog');
- } else {
- changelog.forEach(function(version, i){
- if (version.changes) {
- var date = version.date;
- var versionString = (version.version ? version.version + ' / ' : '')
- + date.getFullYear() + '-' + Util.padZero(date.getMonth() + 1) + '-' + Util.padZero(date.getDate());
- if (i > 0) { console.log(''); }
- console.log(versionString);
- console.log(new Array(versionString.length + 1).join('='));
- console.log('');
- var uniqueChanges = {};
- version.changes.forEach(function(change){
- if (!uniqueChanges[change.message]) {
- console.log(Util.bullet(change.message));
- }
- uniqueChanges[change.message] = true;
- });
+function markdown(changelog, cb) {
+ var output = [];
+ var err = !changelog || !changelog.length ? new Error ('No Changes') : null;
+
+ changelog.map(function(version, i){
+ if (version.changes) {
+ var date = version.date;
+ var versionString = (version.version ? version.version + ' / ' : '')
+ + date.getFullYear() + '-' + Util.padZero(date.getMonth() + 1) + '-' + Util.padZero(date.getDate());
+
+ // add a blank line between sections
+ if (i > 0) {
+ output.push('');
}
- });
- }
+
+ output.push(versionString);
+ output.push(new Array(versionString.length + 1).join('='));
+ output.push('');
+
+ var uniqueChanges = {};
+ version.changes.forEach(function(change){
+ if (!uniqueChanges[change.message]) {
+ output.push(Util.bullet(change.message));
+ }
+ uniqueChanges[change.message] = true;
+ });
+ }
+ });
+
+ cb(err, output.join('\n'));
}
-function json(err, changelog) {
- if (err || !changelog) {
- console.error(err && err.message || 'No Changelog');
- } else {
- console.log(JSON.stringify(changelog));
- }
+function json(changelog, cb) {
+ var err = !changelog || !changelog.length ? new Error ('No Changes') : null;
+ cb(err, JSON.stringify(changelog));
}

0 comments on commit 68031cf

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