Skip to content
Browse files

[refactor] Finish refactor of issues to use flatiron

  • Loading branch information...
1 parent 61a1460 commit 9e881ee59048ea1a3884c07b83e63aa390d6fa5e @indexzero indexzero committed
Showing with 138 additions and 203 deletions.
  1. +0 −11 Makefile
  2. +3 −4 Readme.md
  3. +15 −43 bin/git-issues
  4. +1 −29 bin/issues
  5. +37 −0 lib/cli.js
  6. +21 −35 lib/commands.js
  7. +1 −1 lib/github.js
  8. +13 −0 lib/index.js
  9. +1 −2 lib/params.js
  10. +18 −1 lib/reporters.js
  11. +1 −2 lib/request.js
  12. +2 −2 package.json
  13. +0 −65 test/dispatcher.test.js
  14. +25 −8 test/reporters.test.js
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
30 bin/issues
@@ -1,34 +1,6 @@
#!/usr/bin/env node
/**
- * Dependencies.
- */
-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.
- */
-process.on('uncaughtException', function(err) {
- error(err.message);
- process.exit(1);
-})
-
-/**
* Dispatches the argv to the application.
*/
-app.start();
+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
56 lib/commands.js
@@ -10,7 +10,7 @@
*/
var Request = require('./request');
var reporters = require('./reporters');
-var app = require('flatiron').app;
+var app = require('./index');
var package = require('package')(module);
var log = console.log;
@@ -23,51 +23,32 @@ var log = console.log;
var commands = module.exports;
/**
- * Handles results from GitHub.
- *
- * @param {Object} results
- */
-commands.handle = function(results) {
- var name = app.config.get('reporter').toLowerCase();
- var Reporter = reporters[name];
- var reporter = null;
-
- if (!Reporter) {
- throw new Error('Invalid reporter.');
- }
-
- reporter = new Reporter(results);
- reporter.print();
-};
-
-
-/**
* Help command.
*/
-app.cmd('help', commands.help = function() {
+commands.help = function() {
log(app.help());
-});
+};
/**
* Outputs lib author.
*/
-app.cmd('about', commands.about = function() {
+commands.about = function() {
log('Veselin Todorov <hi@vesln.com>');
-});
+};
/**
* Outputs lib version.
*/
-app.cmd('version', commands.version = function() {
+commands.version = function() {
log(package.version);
-});
+};
/**
* Outputs url address of the project.
*/
-app.cmd('url', commands.url = function() {
+commands.url = function() {
log('http://github.com/vesln/issues');
-});
+};
/**
* Sets an option.
@@ -75,26 +56,26 @@ app.cmd('url', commands.url = function() {
* @param {string} key
* @param {string} value
*/
-app.cmd(/set ([^\s]+) ([^\s]+)/, commands.set = function(key, value) {
+commands.set = function(key, value) {
app.config.set(key, value, function() {
app.config.save(function(err) {
if (err) throw err;
});
});
-});
+};
/**
* Clears an option.
*
* @paran {Object} key
*/
-app.cmd(/clear ([^\s]+)/, commands.clear = function(key) {
+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.
@@ -103,12 +84,17 @@ app.cmd(/clear ([^\s]+)/, commands.clear = function(key) {
* @param {string} username
* @param {string} repo
*/
-app.cmd(/([^\s]+) ([^\s]+)/, commands.request = function (username, 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;
- commands.handle(results);
+ reporters.handle(name, results);
});
-});
+};
View
2 lib/github.js
@@ -11,7 +11,7 @@
var request = require('request');
var flatiron = require('flatiron');
var async = flatiron.common.async;
-var app = flatiron.app;
+var app = require('./index');
/**
* GitHub API.
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
19 lib/reporters.js
@@ -13,4 +13,21 @@ var utile = require('flatiron').common;
/**
* Exporting the reporters.
*/
-module.exports = utile.requireDirLazy(__dirname + '/reporters/');
+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
4 package.json
@@ -23,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 9e881ee

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