Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #8 from indexzero/flatiron

Minor refactor to use flatiron and flatiron.plugins.cli
  • Loading branch information...
commit 3db63bde686dd9911557d2257a04a75f28be3515 2 parents f49754b + 9e881ee
Veselin authored
3  .gitignore
@@ -10,4 +10,5 @@ node_modules/
10 10 *.swp
11 11 *.vi
12 12 *~
13   -.DS_Store
  13 +.DS_Store
  14 +data/config.json
11 Makefile
... ... @@ -1,11 +0,0 @@
1   -TESTS = $(shell find test/ -iname \*.test.js)
2   -
3   -test:
4   - @NODE_ENV=test ./node_modules/.bin/mocha \
5   - --require should \
6   - --timeout 5000 \
7   - --slow 4000 \
8   - --reporter spec \
9   - $(TESTS)
10   -
11   -.PHONY: test
7 Readme.md
Source Rendered
@@ -48,8 +48,8 @@ $ issues nodejitsu forever
48 48
49 49 ```
50 50
51   -$ issues --set username:username
52   -$ issues --set token:token
  51 +$ issues set username username
  52 +$ issues set token token
53 53
54 54 ```
55 55
@@ -93,8 +93,7 @@ $ sudo npm install issues -g
93 93
94 94 ```
95 95
96   -$ npm install
97   -$ make test
  96 +$ npm test
98 97
99 98 ```
100 99
58 bin/git-issues
... ... @@ -1,39 +1,11 @@
1 1 #!/usr/bin/env node
2 2
3 3 /**
4   - * Module dependencies.
  4 + * Dependencies.
5 5 */
6   -var fs = require('fs');
7 6 var path = require('path');
8   -var ini = require('ini');
9   -
10   -/**
11   - * Help params.
12   - *
13   - * @type {Object}
14   - */
15   -var params = require('../lib/params');
16   -
17   -/**
18   - * Argv.
19   - *
20   - * @type {Object}
21   - */
22   -var argv = require('optimist').options(params).argv;
23   -
24   -/**
25   - * The request dispatcher.
26   - *
27   - * @type {Function}
28   - */
29   -var Dispatcher = require('../lib/dispatcher');
30   -
31   -/**
32   - * Dispatcher instance.
33   - *
34   - * @type {Object}
35   - */
36   -var dispatcher = null;
  7 +var flatiron = require('flatiron');
  8 +var app = require('../lib/cli');
37 9
38 10 /**
39 11 * Error alias.
@@ -67,14 +39,18 @@ while (!file) {
67 39 *
68 40 * @type {String}
69 41 */
70   -var config = ini.parse(fs.readFileSync(file, 'utf-8'));
  42 +app.config.use('ini', {
  43 + type: 'file',
  44 + file: file,
  45 + format: flatiron.formats.ini
  46 +});
71 47
72 48 /**
73 49 * The remote source.
74 50 *
75 51 * @type {Object}
76 52 */
77   -var source = config['remote "upstream"'] || config['remote "origin"'];
  53 +var source = app.config.get('remote "upstream"') || app.config.get('remote "origin"');
78 54
79 55 // Checks if anything was found.
80 56 if (!source) throw new Error('Orgin/upstream was not found.');
@@ -87,16 +63,12 @@ if (!source) throw new Error('Orgin/upstream was not found.');
87 63 var result = source.url.match(/(:|\/)([^\/]+)\/([^\/]+)\.git/);
88 64
89 65 // Repo.
90   -argv._.unshift(result[3]);
  66 +app.argv._.unshift(result[3]);
91 67
92 68 // Username.
93   -argv._.unshift(result[2]);
94   -
95   -// Handles exceptions.
96   -process.on('uncaughtException', function(err) {
97   - error(err.message);
98   - process.exit(1);
99   -});
  69 +app.argv._.unshift(result[2]);
100 70
101   -// Dispatches the request.
102   -dispatcher = new Dispatcher(argv).run();
  71 +/**
  72 + * Dispatches the argv to the application.
  73 + */
  74 +app.start();
21 bin/issues
... ... @@ -1,23 +1,6 @@
1 1 #!/usr/bin/env node
2 2
3 3 /**
4   - * Dependencies.
  4 + * Dispatches the argv to the application.
5 5 */
6   -var params = require('../lib/params');
7   -var argv = require('optimist').options(params).argv;
8   -var Dispatcher = require('../lib/dispatcher');
9   -var dispatcher = new Dispatcher(argv);
10   -var error = console.error;
11   -
12   -/**
13   - * Handles exceptions.
14   - */
15   -process.on('uncaughtException', function(err) {
16   - error(err.message);
17   - process.exit(1);
18   -})
19   -
20   -/**
21   - * Dispatches the request.
22   - */
23   -dispatcher.run();
  6 +require('../lib/cli').start();
37 lib/cli.js
... ... @@ -0,0 +1,37 @@
  1 +/*!
  2 + * GitHub Issues from the CLI.
  3 + *
  4 + * Veselin Todorov <hi@vesln.com>
  5 + * MIT License.
  6 + */
  7 +
  8 +/**
  9 + * Dependencies.
  10 + */
  11 +var path = require('path');
  12 +var flatiron = require('flatiron');
  13 +var app = module.exports = require('./index');
  14 +var error = console.error;
  15 +
  16 +app.use(flatiron.plugins.cli, {
  17 + usage: [
  18 + 'GitHub Issues from the CLI.',
  19 + '',
  20 + 'Usage: issues <username> [repository]'
  21 + ],
  22 + source: path.join(__dirname, 'commands'),
  23 + argv: require('./params')
  24 +});
  25 +
  26 +var commands = require('./commands');
  27 +
  28 +app.cmd(/clear ([^\s]+)/, commands.clear);
  29 +app.cmd(/([^\s]+) ([^\s]+)/, commands.issues);
  30 +
  31 +/**
  32 + * Handles exceptions.
  33 + */
  34 +process.on('uncaughtException', function(err) {
  35 + error(err.message);
  36 + process.exit(1);
  37 +});
60 lib/commands.js
@@ -8,10 +8,12 @@
8 8 /**
9 9 * Dependencies.
10 10 */
  11 +var Request = require('./request');
  12 +var reporters = require('./reporters');
  13 +var app = require('./index');
11 14 var package = require('package')(module);
12 15 var log = console.log;
13   -var optimist = require('optimist');
14   -var conf = require('./conf');
  16 +
15 17
16 18 /**
17 19 * Commands.
@@ -24,13 +26,7 @@ var commands = module.exports;
24 26 * Help command.
25 27 */
26 28 commands.help = function() {
27   - var usage = [
28   - '\nGitHub Issues from the CLI.',
29   - '',
30   - 'Usage: issues <username> [repository]'
31   - ].join('\n');
32   -
33   - optimist.usage(usage).showHelp();
  29 + log(app.help());
34 30 };
35 31
36 32 /**
@@ -57,17 +53,12 @@ commands.url = function() {
57 53 /**
58 54 * Sets an option.
59 55 *
60   - * @param {Object} argv
  56 + * @param {string} key
  57 + * @param {string} value
61 58 */
62   -commands.set = function(argv) {
63   - var options = argv.set.split(':');
64   -
65   - if (options.length < 2) {
66   - throw new Error('Invalid options.')
67   - }
68   -
69   - conf.set(options[0], options[1], function() {
70   - conf.save(function(err) {
  59 +commands.set = function(key, value) {
  60 + app.config.set(key, value, function() {
  61 + app.config.save(function(err) {
71 62 if (err) throw err;
72 63 });
73 64 });
@@ -76,13 +67,34 @@ commands.set = function(argv) {
76 67 /**
77 68 * Clears an option.
78 69 *
79   - * @param {Object} argv
  70 + * @paran {Object} key
80 71 */
81   -commands.clear = function(argv) {
82   - var key = argv.clear;
83   - conf.clear(key, function() {
84   - conf.save(function(err) {
  72 +commands.clear = function(key) {
  73 + app.config.clear(key, function() {
  74 + app.config.save(function(err) {
85 75 if (err) throw err;
86 76 });
87 77 });
  78 +};
  79 +
  80 +/**
  81 + * Makes a request to Github for the specified.
  82 + * `username` and `repo`.
  83 + *
  84 + * @param {string} username
  85 + * @param {string} repo
  86 + */
  87 +commands.issues = function (username, repo) {
  88 + var self = this;
  89 + var request = new Request(username, repo);
  90 + var name = app.config.get('reporter').toLowerCase();
  91 +
  92 + if (name === 'handle') {
  93 + name = app.config.stores.defaults.get('reporter');
  94 + }
  95 +
  96 + request.on('end', function(err, results) {
  97 + if (err) throw err;
  98 + reporters.handle(name, results);
  99 + });
88 100 };
23 lib/conf.js
... ... @@ -1,23 +0,0 @@
1   -/*!
2   - * GitHub Issues from the CLI.
3   - *
4   - * Veselin Todorov <hi@vesln.com>
5   - * MIT License.
6   - */
7   -
8   -/**
9   - * Dependencies.
10   - */
11   -var path = require('path');
12   -
13   -/**
14   - * Path to config file.
15   - *
16   - * @param {String}
17   - */
18   -var conf = path.dirname(__dirname) + '/data/config.json';
19   -
20   -/**
21   - * Exposing `conf`.
22   - */
23   -module.exports = require('nconf').file({ file: conf });
81 lib/dispatcher.js
... ... @@ -1,81 +0,0 @@
1   -/*!
2   - * GitHub Issues from the CLI.
3   - *
4   - * Veselin Todorov <hi@vesln.com>
5   - * MIT License.
6   - */
7   -
8   -/**
9   - * Dependencies.
10   - */
11   -var commands = require('./commands');
12   -var package = require('package')(module);
13   -var Request = require('./request');
14   -var reporters = require('./reporters');
15   -
16   -/**
17   - * Dispatcher constructor.
18   - *
19   - * @param {Object} argv
20   - */
21   -function Dispatcher(argv) {
22   - argv || (argv = {});
23   - this.argv = argv;
24   -};
25   -
26   -/**
27   - * Dispatches the request.
28   - */
29   -Dispatcher.prototype.run = function() {
30   - var cmds = Object.keys(commands);
31   - var username = this.argv._[0];
32   - var repository = this.argv._[1];
33   - var self = this;
34   - var request = null;
35   - var command = null;
36   -
37   - for (var i = -1, len = cmds.length; ++i < len;) {
38   - command = cmds[i];
39   - if (this.argv[command]) {
40   - return commands[command](this.argv);
41   - }
42   - }
43   -
44   - if (this.argv._.length === 0) {
45   - return commands.help();
46   - }
47   -
48   - request = new Request(username, repository);
49   -
50   - request.on('end', function(err, results) {
51   - if (err) throw err;
52   - self.handle(results);
53   - });
54   -};
55   -
56   -/**
57   - * Handles results from GitHub.
58   - *
59   - * @param {Object} results
60   - */
61   -Dispatcher.prototype.handle = function(results) {
62   - var Reporter = reporters.get[this.argv.reporter];
63   - var reporter = null;
64   -
65   - if (!Reporter) {
66   - throw new Error('Invalid reporter.');
67   - }
68   -
69   - reporter = new Reporter(results);
70   - reporter.print();
71   -};
72   -
73   -/**
74   - * Exporting the lib.
75   - */
76   -module.exports = Dispatcher;
77   -
78   -/**
79   - * Exports the version.
80   - */
81   -module.exports.version = package.version;
9 lib/github.js
@@ -9,8 +9,9 @@
9 9 * Dependencies.
10 10 */
11 11 var request = require('request');
12   -var async = require('async');
13   -var conf = require('./conf');
  12 +var flatiron = require('flatiron');
  13 +var async = flatiron.common.async;
  14 +var app = require('./index');
14 15
15 16 /**
16 17 * GitHub API.
@@ -100,8 +101,8 @@ github.request = function(url, cb) {
100 101 headers: {},
101 102 json: true
102 103 };
103   - var token = conf.get('token');
104   - var username = conf.get('username');;
  104 + var token = app.config.get('token');
  105 + var username = app.config.get('username');;
105 106 var auth = null;
106 107
107 108 if (token && username) {
13 lib/index.js
... ... @@ -0,0 +1,13 @@
  1 +/*!
  2 + * GitHub Issues from the CLI.
  3 + *
  4 + * Veselin Todorov <hi@vesln.com>
  5 + * MIT License.
  6 + */
  7 +
  8 +var path = require('path');
  9 +
  10 +
  11 +var app = module.exports = require('flatiron').app;
  12 +app.config.file({ file: path.join(__dirname, '..', 'data', 'config.json') });
  13 +app.config.defaults({ reporter: 'basic' });
3  lib/params.js
@@ -28,7 +28,6 @@ module.exports = {
28 28 description: 'Outputs this help info.',
29 29 },
30 30 'reporter': {
31   - description: 'Reporter - basic, short, eighty, number',
32   - default: 'basic'
  31 + description: 'Reporter - basic, short, eighty, number'
33 32 }
34 33 };
21 lib/reporters.js
@@ -8,9 +8,26 @@
8 8 /**
9 9 * Dependencies.
10 10 */
11   -var Redr = require('redr');
  11 +var utile = require('flatiron').common;
12 12
13 13 /**
14 14 * Exporting the reporters.
15 15 */
16   -module.exports = new Redr(__dirname + '/reporters/').load();
  16 +module.exports = utile.requireDirLazy(__dirname + '/reporters/');
  17 +
  18 +/**
  19 + * Handles results from GitHub.
  20 + *
  21 + * @param {Object} results
  22 + */
  23 +module.exports.handle = function(name, results) {
  24 + var Reporter = module.exports[name];
  25 + var reporter = null;
  26 +
  27 + if (!Reporter) {
  28 + throw new Error('Invalid reporter.');
  29 + }
  30 +
  31 + reporter = new Reporter(results);
  32 + reporter.print();
  33 +};
3  lib/request.js
@@ -9,9 +9,8 @@
9 9 * Dependencies.
10 10 */
11 11 var EventEmitter = require('events').EventEmitter;
12   -var async = require('async');
  12 +var async = require('flatiron').common.async;
13 13 var github = require('./github');
14   -var reporters = require('./reporters');
15 14
16 15 /**
17 16 * Request constructor.
12 package.json
@@ -6,15 +6,11 @@
6 6 , "keywords": ["github", "github issues"]
7 7 , "author": "Veselin Todorov <hi@vesln.com>"
8 8 , "dependencies": {
9   - "package": "1.0.0"
10   - , "async": "0.1.15"
11   - , "optimist": "0.3.1"
  9 + "flatiron": "~0.1.7"
  10 + , "package": "1.0.0"
12 11 , "request": "2.9.3"
13   - , "redr": "0.0.2"
14 12 , "cli-table": "0.0.1"
15 13 , "cli-color": "0.1.5"
16   - , "nconf": "0.5.x"
17   - , "ini": "1.0.2"
18 14 }
19 15 , "devDependencies": {
20 16 "mocha": "0.3.3"
@@ -27,13 +23,13 @@
27 23 }
28 24 , "homepage": "http://github.com/vesln/issues"
29 25 , "scripts": {
30   - "test": "make test"
  26 + "test": "NODE_ENV=test mocha --require should --timeout 5000 --slow 4000 --reporter spec test/*.test.js test/reporters/*.test.js"
31 27 }
32 28 , "bin": {
33 29 "issues": "./bin/issues"
34 30 , "git-issues": "./bin/git-issues"
35 31 }
36   - , "main": "index"
  32 + , "main": "./lib/index"
37 33 , "engines": {
38 34 "node": ">= 0.6.0 < 0.7.0"
39 35 }
65 test/dispatcher.test.js
... ... @@ -1,65 +0,0 @@
1   -/*!
2   - * GitHub Issues from the CLI.
3   - *
4   - * Veselin Todorov <hi@vesln.com>
5   - * MIT License.
6   - */
7   -
8   -/**
9   - * Dependencies.
10   - */
11   -var Dispatcher = require('../lib/dispatcher');
12   -var Basic = require('../lib/reporters/basic');
13   -var commands = require('../lib/commands');
14   -var sinon = require('sinon');
15   -
16   -describe('Dispatcher', function() {
17   - describe('run', function() {
18   - it('should call command if it is supplied as param', function() {
19   - var dispatcher = new Dispatcher({ about: true, _: [] });
20   - sinon.stub(commands, 'about');
21   - dispatcher.run();
22   - commands.about.calledOnce.should.be.ok;
23   - commands.about.restore();
24   - });
25   -
26   - it('should call help if the params are empty', function() {
27   - var dispatcher = new Dispatcher({ _: [] });
28   - sinon.stub(commands, 'help');
29   - dispatcher.run();
30   - commands.help.calledOnce.should.be.ok;
31   - commands.help.restore();
32   - });
33   -
34   - it('should call handle if everything is ok', function() {
35   - var print = Basic.prototype.print;
36   - Basic.prototype.print = function() {
37   - Basic.prototype.print = print;
38   - };
39   - var dispatcher = new Dispatcher({ _: ['vesln', 'logme'], reporter: 'basic' });
40   - sinon.stub(dispatcher, 'handle');
41   - dispatcher.run();
42   - dispatcher.handle.should.be.ok;
43   - dispatcher.handle.restore();
44   - });
45   - });
46   -
47   - describe('handle', function() {
48   - it('should throw error if invalid reporter is supplied', function(done) {
49   - var dispatcher = new Dispatcher({ _: ['vesln', 'logme'], reporter: 'foo' });
50   - try {
51   - dispatcher.handle();
52   - } catch(err) {
53   - done();
54   - }
55   - });
56   -
57   - it('should call print method of reporter if everything is ok', function() {
58   - var dispatcher = new Dispatcher({ _: ['vesln', 'logme'], reporter: 'basic' });
59   - var print = sinon.stub(Basic.prototype, 'print');
60   - dispatcher.handle({});
61   - print.calledOnce.should.be.ok;
62   - print.restore();
63   - });
64   - });
65   -});
33 test/reporters.test.js
@@ -8,17 +8,34 @@
8 8 /**
9 9 * Dependencies.
10 10 */
11   -var reporters = require('../lib/reporters');
12   -var Redr = require('redr');
  11 +var fs = require('fs');
13 12 var path = require('path');
  13 +var sinon = require('sinon');
  14 +var reporters = require('../lib/reporters');
  15 +var Basic = require('../lib/reporters/basic');
14 16
15 17 describe('reporters', function() {
16   - it('should be instanceof redr', function() {
17   - (reporters instanceof Redr).should.be.ok;
  18 + it('should have the correct modules', function() {
  19 + fs.readdirSync(path.join(__dirname, '..', 'lib', 'reporters')).forEach(function (file) {
  20 + file = file.replace('.js', '');
  21 + reporters.should.have.property(file);
  22 + });
18 23 });
19 24
20   - it('should have correct path', function() {
21   - var expected = path.dirname(__dirname) + '/lib/reporters/';
22   - reporters.paths[0].should.eql(expected);
23   - });
  25 + describe('handle', function() {
  26 + it('should throw error if invalid reporter is supplied', function(done) {
  27 + try {
  28 + reporters.handle('foo', {});
  29 + } catch(err) {
  30 + done();
  31 + }
  32 + });
  33 +
  34 + it('should call print method of reporter if everything is ok', function() {
  35 + var print = sinon.stub(Basic.prototype, 'print');
  36 + reporters.handle('basic', {});
  37 + print.calledOnce.should.be.ok;
  38 + print.restore();
  39 + });
  40 + });
24 41 });

0 comments on commit 3db63bd

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