Browse files

Merge pull request #8 from indexzero/flatiron

Minor refactor to use flatiron and flatiron.plugins.cli
  • Loading branch information...
2 parents f49754b + 9e881ee commit 3db63bde686dd9911557d2257a04a75f28be3515 @vesln committed Jan 18, 2012
Showing with 163 additions and 297 deletions.
  1. +2 −1 .gitignore
  2. +0 −11 Makefile
  3. +3 −4 Readme.md
  4. +15 −43 bin/git-issues
  5. +2 −19 bin/issues
  6. +37 −0 lib/cli.js
  7. +36 −24 lib/commands.js
  8. +0 −23 lib/conf.js
  9. +0 −81 lib/dispatcher.js
  10. +5 −4 lib/github.js
  11. +13 −0 lib/index.js
  12. +1 −2 lib/params.js
  13. +19 −2 lib/reporters.js
  14. +1 −2 lib/request.js
  15. +4 −8 package.json
  16. +0 −65 test/dispatcher.test.js
  17. +25 −8 test/reporters.test.js
View
3 .gitignore
@@ -10,4 +10,5 @@ node_modules/
*.swp
*.vi
*~
-.DS_Store
+.DS_Store
+data/config.json
View
11 Makefile
@@ -1,11 +0,0 @@
-TESTS = $(shell find test/ -iname \*.test.js)
-
-test:
- @NODE_ENV=test ./node_modules/.bin/mocha \
- --require should \
- --timeout 5000 \
- --slow 4000 \
- --reporter spec \
- $(TESTS)
-
-.PHONY: test
View
7 Readme.md
@@ -48,8 +48,8 @@ $ issues nodejitsu forever
```
-$ issues --set username:username
-$ issues --set token:token
+$ issues set username username
+$ issues set token token
```
@@ -93,8 +93,7 @@ $ sudo npm install issues -g
```
-$ npm install
-$ make test
+$ npm test
```
View
58 bin/git-issues
@@ -1,39 +1,11 @@
#!/usr/bin/env node
/**
- * Module dependencies.
+ * Dependencies.
*/
-var fs = require('fs');
var path = require('path');
-var ini = require('ini');
-
-/**
- * Help params.
- *
- * @type {Object}
- */
-var params = require('../lib/params');
-
-/**
- * Argv.
- *
- * @type {Object}
- */
-var argv = require('optimist').options(params).argv;
-
-/**
- * The request dispatcher.
- *
- * @type {Function}
- */
-var Dispatcher = require('../lib/dispatcher');
-
-/**
- * Dispatcher instance.
- *
- * @type {Object}
- */
-var dispatcher = null;
+var flatiron = require('flatiron');
+var app = require('../lib/cli');
/**
* Error alias.
@@ -67,14 +39,18 @@ while (!file) {
*
* @type {String}
*/
-var config = ini.parse(fs.readFileSync(file, 'utf-8'));
+app.config.use('ini', {
+ type: 'file',
+ file: file,
+ format: flatiron.formats.ini
+});
/**
* The remote source.
*
* @type {Object}
*/
-var source = config['remote "upstream"'] || config['remote "origin"'];
+var source = app.config.get('remote "upstream"') || app.config.get('remote "origin"');
// Checks if anything was found.
if (!source) throw new Error('Orgin/upstream was not found.');
@@ -87,16 +63,12 @@ if (!source) throw new Error('Orgin/upstream was not found.');
var result = source.url.match(/(:|\/)([^\/]+)\/([^\/]+)\.git/);
// Repo.
-argv._.unshift(result[3]);
+app.argv._.unshift(result[3]);
// Username.
-argv._.unshift(result[2]);
-
-// Handles exceptions.
-process.on('uncaughtException', function(err) {
- error(err.message);
- process.exit(1);
-});
+app.argv._.unshift(result[2]);
-// Dispatches the request.
-dispatcher = new Dispatcher(argv).run();
+/**
+ * Dispatches the argv to the application.
+ */
+app.start();
View
21 bin/issues
@@ -1,23 +1,6 @@
#!/usr/bin/env node
/**
- * Dependencies.
+ * Dispatches the argv to the application.
*/
-var params = require('../lib/params');
-var argv = require('optimist').options(params).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();
+require('../lib/cli').start();
View
37 lib/cli.js
@@ -0,0 +1,37 @@
+/*!
+ * GitHub Issues from the CLI.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var path = require('path');
+var flatiron = require('flatiron');
+var app = module.exports = require('./index');
+var error = console.error;
+
+app.use(flatiron.plugins.cli, {
+ usage: [
+ 'GitHub Issues from the CLI.',
+ '',
+ 'Usage: issues <username> [repository]'
+ ],
+ source: path.join(__dirname, 'commands'),
+ argv: require('./params')
+});
+
+var commands = require('./commands');
+
+app.cmd(/clear ([^\s]+)/, commands.clear);
+app.cmd(/([^\s]+) ([^\s]+)/, commands.issues);
+
+/**
+ * Handles exceptions.
+ */
+process.on('uncaughtException', function(err) {
+ error(err.message);
+ process.exit(1);
+});
View
60 lib/commands.js
@@ -8,10 +8,12 @@
/**
* Dependencies.
*/
+var Request = require('./request');
+var reporters = require('./reporters');
+var app = require('./index');
var package = require('package')(module);
var log = console.log;
-var optimist = require('optimist');
-var conf = require('./conf');
+
/**
* Commands.
@@ -24,13 +26,7 @@ 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();
+ log(app.help());
};
/**
@@ -57,17 +53,12 @@ commands.url = function() {
/**
* 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) {
+commands.set = function(key, value) {
+ app.config.set(key, value, function() {
+ app.config.save(function(err) {
if (err) throw err;
});
});
@@ -76,13 +67,34 @@ commands.set = function(argv) {
/**
* 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) {
+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
+ */
+commands.issues = function (username, repo) {
+ var self = this;
+ var request = new Request(username, repo);
+ var name = app.config.get('reporter').toLowerCase();
+
+ if (name === 'handle') {
+ name = app.config.stores.defaults.get('reporter');
+ }
+
+ request.on('end', function(err, results) {
+ if (err) throw err;
+ reporters.handle(name, results);
+ });
};
View
23 lib/conf.js
@@ -1,23 +0,0 @@
-/*!
- * GitHub Issues from the CLI.
- *
- * Veselin Todorov <hi@vesln.com>
- * MIT License.
- */
-
-/**
- * Dependencies.
- */
-var path = require('path');
-
-/**
- * Path to config file.
- *
- * @param {String}
- */
-var conf = path.dirname(__dirname) + '/data/config.json';
-
-/**
- * Exposing `conf`.
- */
-module.exports = require('nconf').file({ file: conf });
View
81 lib/dispatcher.js
@@ -1,81 +0,0 @@
-/*!
- * 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 self = this;
- var request = null;
- var command = null;
-
- for (var i = -1, len = cmds.length; ++i < len;) {
- command = cmds[i];
- if (this.argv[command]) {
- return commands[command](this.argv);
- }
- }
-
- 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;
View
9 lib/github.js
@@ -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 = require('./index');
/**
* 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
13 lib/index.js
@@ -0,0 +1,13 @@
+/*!
+ * GitHub Issues from the CLI.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+var path = require('path');
+
+
+var app = module.exports = require('flatiron').app;
+app.config.file({ file: path.join(__dirname, '..', 'data', 'config.json') });
+app.config.defaults({ reporter: 'basic' });
View
3 lib/params.js
@@ -28,7 +28,6 @@ module.exports = {
description: 'Outputs this help info.',
},
'reporter': {
- description: 'Reporter - basic, short, eighty, number',
- default: 'basic'
+ description: 'Reporter - basic, short, eighty, number'
}
};
View
21 lib/reporters.js
@@ -8,9 +8,26 @@
/**
* 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/');
+
+/**
+ * Handles results from GitHub.
+ *
+ * @param {Object} results
+ */
+module.exports.handle = function(name, results) {
+ var Reporter = module.exports[name];
+ var reporter = null;
+
+ if (!Reporter) {
+ throw new Error('Invalid reporter.');
+ }
+
+ reporter = new Reporter(results);
+ reporter.print();
+};
View
3 lib/request.js
@@ -9,9 +9,8 @@
* Dependencies.
*/
var EventEmitter = require('events').EventEmitter;
-var async = require('async');
+var async = require('flatiron').common.async;
var github = require('./github');
-var reporters = require('./reporters');
/**
* Request constructor.
View
12 package.json
@@ -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"
@@ -27,13 +23,13 @@
}
, "homepage": "http://github.com/vesln/issues"
, "scripts": {
- "test": "make test"
+ "test": "NODE_ENV=test mocha --require should --timeout 5000 --slow 4000 --reporter spec test/*.test.js test/reporters/*.test.js"
}
, "bin": {
"issues": "./bin/issues"
, "git-issues": "./bin/git-issues"
}
- , "main": "index"
+ , "main": "./lib/index"
, "engines": {
"node": ">= 0.6.0 < 0.7.0"
}
View
65 test/dispatcher.test.js
@@ -1,65 +0,0 @@
-/*!
- * GitHub Issues from the CLI.
- *
- * Veselin Todorov <hi@vesln.com>
- * MIT License.
- */
-
-/**
- * Dependencies.
- */
-var Dispatcher = require('../lib/dispatcher');
-var Basic = require('../lib/reporters/basic');
-var commands = require('../lib/commands');
-var sinon = require('sinon');
-
-describe('Dispatcher', function() {
- describe('run', function() {
- it('should call command if it is supplied as param', function() {
- var dispatcher = new Dispatcher({ about: true, _: [] });
- sinon.stub(commands, 'about');
- dispatcher.run();
- commands.about.calledOnce.should.be.ok;
- commands.about.restore();
- });
-
- it('should call help if the params are empty', function() {
- var dispatcher = new Dispatcher({ _: [] });
- sinon.stub(commands, 'help');
- dispatcher.run();
- commands.help.calledOnce.should.be.ok;
- commands.help.restore();
- });
-
- it('should call handle if everything is ok', function() {
- var print = Basic.prototype.print;
- Basic.prototype.print = function() {
- Basic.prototype.print = print;
- };
- var dispatcher = new Dispatcher({ _: ['vesln', 'logme'], reporter: 'basic' });
- sinon.stub(dispatcher, 'handle');
- dispatcher.run();
- dispatcher.handle.should.be.ok;
- dispatcher.handle.restore();
- });
- });
-
- describe('handle', function() {
- it('should throw error if invalid reporter is supplied', function(done) {
- var dispatcher = new Dispatcher({ _: ['vesln', 'logme'], reporter: 'foo' });
- try {
- dispatcher.handle();
- } catch(err) {
- done();
- }
- });
-
- it('should call print method of reporter if everything is ok', function() {
- var dispatcher = new Dispatcher({ _: ['vesln', 'logme'], reporter: 'basic' });
- var print = sinon.stub(Basic.prototype, 'print');
- dispatcher.handle({});
- print.calledOnce.should.be.ok;
- print.restore();
- });
- });
-});
View
33 test/reporters.test.js
@@ -8,17 +8,34 @@
/**
* Dependencies.
*/
-var reporters = require('../lib/reporters');
-var Redr = require('redr');
+var fs = require('fs');
var path = require('path');
+var sinon = require('sinon');
+var reporters = require('../lib/reporters');
+var Basic = require('../lib/reporters/basic');
describe('reporters', function() {
- it('should be instanceof redr', function() {
- (reporters instanceof Redr).should.be.ok;
+ it('should have the correct modules', function() {
+ fs.readdirSync(path.join(__dirname, '..', 'lib', 'reporters')).forEach(function (file) {
+ file = file.replace('.js', '');
+ reporters.should.have.property(file);
+ });
});
- it('should have correct path', function() {
- var expected = path.dirname(__dirname) + '/lib/reporters/';
- reporters.paths[0].should.eql(expected);
- });
+ describe('handle', function() {
+ it('should throw error if invalid reporter is supplied', function(done) {
+ try {
+ reporters.handle('foo', {});
+ } catch(err) {
+ done();
+ }
+ });
+
+ it('should call print method of reporter if everything is ok', function() {
+ var print = sinon.stub(Basic.prototype, 'print');
+ reporters.handle('basic', {});
+ print.calledOnce.should.be.ok;
+ print.restore();
+ });
+ });
});

0 comments on commit 3db63bd

Please sign in to comment.