Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit fce7928b14302c4f9ce22afa2dfdc1d2a9784d82 @vesln committed Jan 6, 2012
Showing with 509 additions and 0 deletions.
  1. +13 −0 .gitignore
  2. +3 −0 .travis.yml
  3. +9 −0 Makefile
  4. 0 Readme.md
  5. +40 −0 bin/issues
  6. +54 −0 lib/commands.js
  7. +81 −0 lib/dispatcher.js
  8. +95 −0 lib/github.js
  9. +16 −0 lib/reporters.js
  10. +60 −0 lib/reporters/basic.js
  11. +42 −0 lib/reporters/short.js
  12. +63 −0 lib/request.js
  13. +33 −0 package.json
@@ -0,0 +1,13 @@
+node_modules/
+*._
+*.tmp
+.monitor
+*.diff
+*.err
+*.orig
+*.rej
+*.swo
+*.swp
+*.vi
+*~
+.DS_Store
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 0.6
@@ -0,0 +1,9 @@
+TESTS = test/*.test.js
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha \
+ --require should \
+ --reporter spec \
+ $(TESTS)
+
+.PHONY: test
No changes.
@@ -0,0 +1,40 @@
+#!/usr/bin/env node
+
+/**
+ * Dependencies.
+ */
+var argv = require('optimist').options({
+ 'about': {
+ description: 'Library author.',
+ },
+ 'version': {
+ description: 'Outputs version number.',
+ },
+ 'url': {
+ description: 'Outputs the url of the library.',
+ },
+ 'help': {
+ description: 'Outputs this help info.',
+ },
+ 'reporter': {
+ description: 'Reporter - basic, short',
+ default: 'basic'
+ }
+}).argv;
+
+var Dispatcher = require('../lib/dispatcher');
+var dispatcher = new Dispatcher(argv);
+var error = console.error;
+
+/**
+ * Handles exceptions.
+ */
+process.on('uncaughtException', function(err) {
+ error(err.message);
+ process.exit(1);
+})
+
+/**
+ * Dispatches the request.
+ */
+dispatcher.run();
@@ -0,0 +1,54 @@
+/*!
+ * GitHub Issues from the CLI.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var package = require('package')(module);
+var optimist = require('optimist');
+var log = console.log;
+
+/**
+ * Commands.
+ *
+ * @type {Object}
+ */
+var commands = module.exports;
+
+/**
+ * Help command.
+ */
+commands.help = function() {
+ var usage = [
+ '\nGitHub Issues from the CLI.',
+ '',
+ 'Usage: issues <username> [repository]'
+ ].join('\n');
+
+ optimist.usage(usage).showHelp();
+};
+
+/**
+ * Outputs lib author.
+ */
+commands.about = function() {
+ log('Veselin Todorov <hi@vesln.com>');
+};
+
+/**
+ * Outputs lib version.
+ */
+commands.version = function() {
+ log(package.version);
+};
+
+/**
+ * Outputs url address of the project.
+ */
+commands.url = function() {
+ log('http://github.com/vesln/issues');
+};
@@ -0,0 +1,81 @@
+/*!
+ * GitHub Issues from the CLI.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var commands = require('./commands');
+var package = require('package')(module);
+var Request = require('./request');
+var reporters = require('./reporters');
+
+/**
+ * Dispatcher constructor.
+ *
+ * @param {Object} argv
+ */
+function Dispatcher(argv) {
+ argv || (argv = {});
+ this.argv = argv;
+};
+
+/**
+ * Dispatches the request.
+ */
+Dispatcher.prototype.run = function() {
+ var cmds = Object.keys(commands);
+ var username = this.argv._[0];
+ var repository = this.argv._[1];
+ var cmd = null;
+ var self = this;
+ var request = null;
+
+ for (var i = -1, len = cmds.length; ++i < len;) {
+ command = cmds[i];
+ if (this.argv[command]) {
+ return commands[command]();
+ }
+ }
+
+ if (this.argv._.length === 0) {
+ return commands.help();
+ }
+
+ request = new Request(username, repository);
+
+ request.on('end', function(err, results) {
+ if (err) throw err;
+ self.handle(results);
+ });
+};
+
+/**
+ * Handles results from GitHub.
+ *
+ * @param {Object} results
+ */
+Dispatcher.prototype.handle = function(results) {
+ var Reporter = reporters.get[this.argv.reporter];
+ var reporter = null;
+
+ if (!Reporter) {
+ throw new Error('Invalid reporter.');
+ }
+
+ reporter = new Reporter(results);
+ reporter.print();
+};
+
+/**
+ * Exporting the lib.
+ */
+module.exports = Dispatcher;
+
+/**
+ * Exports the version.
+ */
+module.exports.version = package.version;
@@ -0,0 +1,95 @@
+/*!
+ * GitHub Issues from the CLI.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var request = require('request');
+var async = require('async');
+
+/**
+ * GitHub API.
+ *
+ * @type {Object}
+ */
+var github = module.exports;
+
+/**
+ * URLs.
+ *
+ * @type {Object}
+ */
+github.urls = {
+ main: 'https://api.github.com',
+ repos: '/users/:user/repos',
+ issues: '/repos/:user/:repo/issues'
+};
+
+/**
+ * Returns all repos of a user.
+ *
+ * @param {String} username
+ * @param {Function} callback
+ */
+github.repos = function(username, cb) {
+ var url = github.url('repos', { user: username });
+ var repos = [];
+
+ request({ url: url, json: true }, function(err, response, body) {
+ body || (body = []);
+ body.forEach(function(repo) {
+ repos.push(repo.name);
+ });
+
+ cb(err, repos);
+ });
+};
+
+/**
+ * Fetches all issues for supplied repos.
+ *
+ * @param {String} username
+ * @param {Array} repos
+ * @param {Function} callback
+ */
+github.issues = function(username, repos, cb) {
+ var results = {};
+ var issue = function(repo, done) {
+ var url = github.url('issues', { user: username, repo: repo });
+
+ request({ url: url, json: true }, function(err, response, body) {
+ switch (response.statusCode) {
+ case 404: return done(new Error('Invalid username or repository name.'));
+ case 200: break;
+ default: body = []
+ }
+ results[repo] = body;
+ done(err);
+ });
+ };
+
+ async.forEach(repos, issue, function(err) {
+ cb(err, results);
+ });
+};
+
+/**
+ * Returns a url address.
+ *
+ * @type {String} name
+ * @type {Object} param
+ * @returns {String} url
+ */
+github.url = function(name, params) {
+ var url = github.urls.main + github.urls[name];
+
+ Object.keys(params).forEach(function(param) {
+ url = url.replace(':' + param, params[param]);
+ });
+
+ return url;
+};
@@ -0,0 +1,16 @@
+/*!
+ * GitHub Issues from the CLI.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var Redr = require('redr');
+
+/**
+ * Exporting the reporters.
+ */
+module.exports = new Redr(__dirname + '/reporters/').load();
@@ -0,0 +1,60 @@
+/*!
+ * GitHub Issues from the CLI.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var print = console.log;
+var Table = require('cli-table');
+var colors = require('cli-color');
+
+/**
+ * Basic reporter constructor.
+ *
+ * @param {Object} data
+ */
+function Basic(data) {
+ this.data = data;
+};
+
+/**
+ * Table options.
+ *
+ * @type {Object}
+ */
+Basic.prototype.options = {
+ head: ['Title', 'Author', 'Date'],
+ colWidths: [40, 20, 25]
+};
+
+/**
+ * Prints the results.
+ */
+Basic.prototype.print = function() {
+ var table = null;
+ var self = this;
+ var issues = null;
+
+ Object.keys(this.data).forEach(function(repo) {
+ issues = self.data[repo];
+ table = new Table(self.options);
+
+ issues.forEach(function(issue) {
+ table.push([issue.title, issue.user.login, issue.created_at]);
+ });
+
+ print('\n ' + repo.bold.underline + ' (%d open issues)\n', issues.length);
+ print(table.toString());
+ if (!table.length) print(' ' + 'No issues to show.')
+ print('\n');
+ });
+};
+
+/**
+ * Exporting the lib.
+ */
+module.exports = Basic;
Oops, something went wrong.

0 comments on commit fce7928

Please sign in to comment.