Skip to content

Commit

Permalink
Make it possible to require slack-irc as a node module
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmartin committed May 22, 2015
1 parent 1af1145 commit 979208f
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 28 deletions.
3 changes: 0 additions & 3 deletions .jscsrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@
"disallowMultipleVarDecl": true,
"requireMultipleVarDecl": null,
"requireCamelCaseOrUpperCaseIdentifiers": "ignoreProperties",
"requirePaddingNewLinesAfterBlocks": {
"allExcept": ["inCallExpressions", "inArrayExpressions", "inProperties"]
},
"safeContextKeyword": ["that"]
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
This project adheres to [Semantic Versioning](http://semver.org/).

## [3.4.0] - 2015-05-22
### Added
- Made it possible to require slack-irc as a node module.

## [3.3.2] - 2015-05-17
### Fixed
- Upgrade dependencies.
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ $ npm install
$ node index.js --config /path/to/config.json
```

It can also be used as a node module:
```js
var slackIRC = require('slack-irc');
var config = require('./config.json');
slackIRC(config);
```

## Configuration

slack-irc uses Slack's [bot users](https://api.slack.com/bot-users).
Expand Down
18 changes: 5 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
#!/usr/bin/env node

var program = require('commander');
var checkEnv = require('check-env');
var createBots = require('./lib/helpers').createBots;
var logger = require('winston');

if (process.env.NODE_ENV === 'development') {
logger.level = 'debug';
}

program
.version('1.1.0')
.option('-c, --config <path>',
'Sets the path to the config file, otherwise read from the env variable CONFIG_FILE.'
)
.parse(process.argv);

// If no config option is given, try to use the env variable:
if (!program.config) checkEnv(['CONFIG_FILE']);
else process.env.CONFIG_FILE = program.config;
/* istanbul ignore next*/
if (!module.parent) {
require('./lib/cli')();
}

createBots();
module.exports = createBots;
1 change: 1 addition & 0 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function Bot(options) {
throw new errors.ConfigurationError('Missing configuration field ' + field);
}
});

validateChannelMapping(options.channelMapping);

this.slack = new Slack(options.token);
Expand Down
25 changes: 25 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env node

var program = require('commander');
var path = require('path');
var checkEnv = require('check-env');
var createBots = require('./helpers').createBots;

function run() {
program
.version(require('../package.json').version)
.option('-c, --config <path>',
'Sets the path to the config file, otherwise read from the env variable CONFIG_FILE.'
)
.parse(process.argv);

// If no config option is given, try to use the env variable:
if (!program.config) checkEnv(['CONFIG_FILE']);
else process.env.CONFIG_FILE = program.config;

var configFile = require(path.resolve(process.cwd(), process.env.CONFIG_FILE));

createBots(configFile);
}

module.exports = run;
3 changes: 1 addition & 2 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ var errors = require('./errors');
* Reads from the provided config file and returns an array of bots
* @return {object[]}
*/
exports.createBots = function() {
var configFile = require(path.resolve(process.cwd(), process.env.CONFIG_FILE));
exports.createBots = function(configFile) {
var bots = [];

// The config file can be both an array and an object
Expand Down
37 changes: 37 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* jshint expr: true */
var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var rewire = require('rewire');
var cli = rewire('../lib/cli');
var testConfig = require('./fixtures/test-config.json');
var singleTestConfig = require('./fixtures/single-test-config.json');

chai.should();
chai.use(sinonChai);

describe('CLI', function() {
before(function() {
this.createBotsStub = sinon.stub();
cli.__set__('createBots', this.createBotsStub);
});

afterEach(function() {
this.createBotsStub.reset();
});

it('should be possible to give the config as an env var', function() {
process.env.CONFIG_FILE = process.cwd() + '/test/fixtures/test-config.json';
process.argv = ['node', 'index.js'];
cli();
this.createBotsStub.should.have.been.calledWith(testConfig);
});

it('should be possible to give the config as an option', function() {
delete process.env.CONFIG_FILE;
process.argv = ['node', 'index.js',
'--config', process.cwd() + '/test/fixtures/single-test-config.json'];
cli();
this.createBotsStub.should.have.been.calledWith(singleTestConfig);
});
});
26 changes: 16 additions & 10 deletions test/create-bots.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/* jshint expr: true */
var chai = require('chai');
var logger = require('winston');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var createBots = require('../lib/helpers').createBots;
var Bot = require('../lib/bot');
var ConfigurationError = require('../lib/errors').ConfigurationError;
var index = require('../index');
var testConfig = require('./fixtures/test-config.json');
var singleTestConfig = require('./fixtures/single-test-config.json');
var badConfig = require('./fixtures/bad-config.json');
var stringConfig = require('./fixtures/string-config.json');

chai.should();
chai.use(sinonChai);
Expand All @@ -13,43 +19,43 @@ describe('Create Bots', function() {
before(function() {
this.connectStub = sinon.stub();
Bot.prototype.connect = this.connectStub;
process.env.CONFIG_FILE = process.cwd() + '/test/fixtures/test-config.json';
});

afterEach(function() {
this.connectStub.reset();
});

it('should work when given an array of configs', function() {
process.env.CONFIG_FILE = process.cwd() + '/test/fixtures/test-config.json';
var bots = createBots();
var bots = createBots(testConfig);
bots.length.should.equal(2);
this.connectStub.should.have.been.called;
});

it('should work when given an object as a config file', function() {
process.env.CONFIG_FILE = process.cwd() + '/test/fixtures/single-test-config.json';
var bots = createBots();
var bots = createBots(singleTestConfig);
bots.length.should.equal(1);
this.connectStub.should.have.been.called;
});

it('should throw a configuration error if any fields are missing', function() {
process.env.CONFIG_FILE = process.cwd() + '/test/fixtures/bad-config.json';
function wrap() {
createBots();
createBots(badConfig);
}

(wrap).should.throw(ConfigurationError, 'Missing configuration field nickname');
});

it('should throw if a configuration file is neither an object or an array', function() {
process.env.CONFIG_FILE = process.cwd() + '/test/fixtures/string-config.json';

function wrap() {
createBots();
createBots(stringConfig);
}

(wrap).should.throw(ConfigurationError);
});

it('should be possible to run it through require(\'slack-irc\')', function() {
var bots = index(singleTestConfig);
bots.length.should.equal(1);
this.connectStub.should.have.been.called;
});
});

0 comments on commit 979208f

Please sign in to comment.