Permalink
Browse files

[refactor] First pass at refactoring to use flatiron

  • Loading branch information...
1 parent f49754b commit 6eb2abf8728b37cf99ad650b2f9fa681c7d210bc @indexzero indexzero committed Jan 18, 2012
Showing with 87 additions and 52 deletions.
  1. +2 −1 .gitignore
  2. +17 −6 bin/issues
  3. +59 −33 lib/commands.js
  4. +5 −4 lib/github.js
  5. +2 −2 lib/reporters.js
  6. +2 −6 package.json
View
@@ -10,4 +10,5 @@ node_modules/
*.swp
*.vi
*~
-.DS_Store
+.DS_Store
+data/config.json
View
@@ -3,12 +3,23 @@
/**
* Dependencies.
*/
-var params = require('../lib/params');
-var argv = require('optimist').options(params).argv;
-var Dispatcher = require('../lib/dispatcher');
-var dispatcher = new Dispatcher(argv);
+var path = require('path');
+var flatiron = require('flatiron');
+var app = flatiron.app;
var error = console.error;
+app.config.file({ file: path.join(__dirname, '..', 'data', 'config.json') });
+app.use(flatiron.plugins.cli, {
+ usage: [
+ 'GitHub Issues from the CLI.',
+ '',
+ 'Usage: issues <username> [repository]'
+ ],
+ argv: require('../lib/params')
+});
+
+require('../lib/commands');
+
/**
* Handles exceptions.
*/
@@ -18,6 +29,6 @@ process.on('uncaughtException', function(err) {
})
/**
- * Dispatches the request.
+ * Dispatches the argv to the application.
*/
-dispatcher.run();
+app.start();
View
@@ -8,10 +8,12 @@
/**
* Dependencies.
*/
+var Request = require('./request');
+var reporters = require('./reporters');
+var app = require('flatiron').app;
var package = require('package')(module);
var log = console.log;
-var optimist = require('optimist');
-var conf = require('./conf');
+
/**
* Commands.
@@ -21,68 +23,92 @@ var conf = require('./conf');
var commands = module.exports;
/**
- * Help command.
+ * Handles results from GitHub.
+ *
+ * @param {Object} results
*/
-commands.help = function() {
- var usage = [
- '\nGitHub Issues from the CLI.',
- '',
- 'Usage: issues <username> [repository]'
- ].join('\n');
+commands.handle = function(results) {
+ var name = app.config.get('reporter').toLowerCase();
+ var Reporter = reporters[name];
+ var reporter = null;
- optimist.usage(usage).showHelp();
+ if (!Reporter) {
+ throw new Error('Invalid reporter.');
+ }
+
+ reporter = new Reporter(results);
+ reporter.print();
};
+
+/**
+ * Help command.
+ */
+app.cmd('help', commands.help = function() {
+ log(app.help());
+});
+
/**
* Outputs lib author.
*/
-commands.about = function() {
+app.cmd('about', commands.about = function() {
log('Veselin Todorov <hi@vesln.com>');
-};
+});
/**
* Outputs lib version.
*/
-commands.version = function() {
+app.cmd('version', commands.version = function() {
log(package.version);
-};
+});
/**
* Outputs url address of the project.
*/
-commands.url = function() {
+app.cmd('url', commands.url = function() {
log('http://github.com/vesln/issues');
-};
+});
/**
* Sets an option.
*
- * @param {Object} argv
+ * @param {string} key
+ * @param {string} value
*/
-commands.set = function(argv) {
- var options = argv.set.split(':');
-
- if (options.length < 2) {
- throw new Error('Invalid options.')
- }
-
- conf.set(options[0], options[1], function() {
- conf.save(function(err) {
+app.cmd(/set ([^\s]+) ([^\s]+)/, commands.set = function(key, value) {
+ app.config.set(key, value, function() {
+ app.config.save(function(err) {
if (err) throw err;
});
});
-};
+});
/**
* Clears an option.
*
- * @param {Object} argv
+ * @paran {Object} key
*/
-commands.clear = function(argv) {
- var key = argv.clear;
- conf.clear(key, function() {
- conf.save(function(err) {
+app.cmd(/clear ([^\s]+)/, commands.clear = function(key) {
+ app.config.clear(key, function() {
+ app.config.save(function(err) {
if (err) throw err;
});
});
-};
+});
+
+/**
+ * Makes a request to Github for the specified.
+ * `username` and `repo`.
+ *
+ * @param {string} username
+ * @param {string} repo
+ */
+app.cmd(/([^\s]+) ([^\s]+)/, commands.request = function (username, repo) {
+ var self = this;
+ var request = new Request(username, repo);
+
+ request.on('end', function(err, results) {
+ if (err) throw err;
+ commands.handle(results);
+ });
+});
View
@@ -9,8 +9,9 @@
* Dependencies.
*/
var request = require('request');
-var async = require('async');
-var conf = require('./conf');
+var flatiron = require('flatiron');
+var async = flatiron.common.async;
+var app = flatiron.app;
/**
* GitHub API.
@@ -100,8 +101,8 @@ github.request = function(url, cb) {
headers: {},
json: true
};
- var token = conf.get('token');
- var username = conf.get('username');;
+ var token = app.config.get('token');
+ var username = app.config.get('username');;
var auth = null;
if (token && username) {
View
@@ -8,9 +8,9 @@
/**
* Dependencies.
*/
-var Redr = require('redr');
+var utile = require('flatiron').common;
/**
* Exporting the reporters.
*/
-module.exports = new Redr(__dirname + '/reporters/').load();
+module.exports = utile.requireDirLazy(__dirname + '/reporters/');
View
@@ -6,15 +6,11 @@
, "keywords": ["github", "github issues"]
, "author": "Veselin Todorov <hi@vesln.com>"
, "dependencies": {
- "package": "1.0.0"
- , "async": "0.1.15"
- , "optimist": "0.3.1"
+ "flatiron": "~0.1.7"
+ , "package": "1.0.0"
, "request": "2.9.3"
- , "redr": "0.0.2"
, "cli-table": "0.0.1"
, "cli-color": "0.1.5"
- , "nconf": "0.5.x"
- , "ini": "1.0.2"
}
, "devDependencies": {
"mocha": "0.3.3"

0 comments on commit 6eb2abf

Please sign in to comment.