Skip to content

Commit 979208f

Browse files
author
ekmartin
committed
Make it possible to require slack-irc as a node module
1 parent 1af1145 commit 979208f

File tree

9 files changed

+96
-28
lines changed

9 files changed

+96
-28
lines changed

.jscsrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@
44
"disallowMultipleVarDecl": true,
55
"requireMultipleVarDecl": null,
66
"requireCamelCaseOrUpperCaseIdentifiers": "ignoreProperties",
7-
"requirePaddingNewLinesAfterBlocks": {
8-
"allExcept": ["inCallExpressions", "inArrayExpressions", "inProperties"]
9-
},
107
"safeContextKeyword": ["that"]
118
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
This project adheres to [Semantic Versioning](http://semver.org/).
33

4+
## [3.4.0] - 2015-05-22
5+
### Added
6+
- Made it possible to require slack-irc as a node module.
7+
48
## [3.3.2] - 2015-05-17
59
### Fixed
610
- Upgrade dependencies.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ $ npm install
2020
$ node index.js --config /path/to/config.json
2121
```
2222

23+
It can also be used as a node module:
24+
```js
25+
var slackIRC = require('slack-irc');
26+
var config = require('./config.json');
27+
slackIRC(config);
28+
```
29+
2330
## Configuration
2431

2532
slack-irc uses Slack's [bot users](https://api.slack.com/bot-users).

index.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
#!/usr/bin/env node
22

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

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

12-
program
13-
.version('1.1.0')
14-
.option('-c, --config <path>',
15-
'Sets the path to the config file, otherwise read from the env variable CONFIG_FILE.'
16-
)
17-
.parse(process.argv);
18-
19-
// If no config option is given, try to use the env variable:
20-
if (!program.config) checkEnv(['CONFIG_FILE']);
21-
else process.env.CONFIG_FILE = program.config;
10+
/* istanbul ignore next*/
11+
if (!module.parent) {
12+
require('./lib/cli')();
13+
}
2214

23-
createBots();
15+
module.exports = createBots;

lib/bot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function Bot(options) {
1818
throw new errors.ConfigurationError('Missing configuration field ' + field);
1919
}
2020
});
21+
2122
validateChannelMapping(options.channelMapping);
2223

2324
this.slack = new Slack(options.token);

lib/cli.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env node
2+
3+
var program = require('commander');
4+
var path = require('path');
5+
var checkEnv = require('check-env');
6+
var createBots = require('./helpers').createBots;
7+
8+
function run() {
9+
program
10+
.version(require('../package.json').version)
11+
.option('-c, --config <path>',
12+
'Sets the path to the config file, otherwise read from the env variable CONFIG_FILE.'
13+
)
14+
.parse(process.argv);
15+
16+
// If no config option is given, try to use the env variable:
17+
if (!program.config) checkEnv(['CONFIG_FILE']);
18+
else process.env.CONFIG_FILE = program.config;
19+
20+
var configFile = require(path.resolve(process.cwd(), process.env.CONFIG_FILE));
21+
22+
createBots(configFile);
23+
}
24+
25+
module.exports = run;

lib/helpers.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ var errors = require('./errors');
77
* Reads from the provided config file and returns an array of bots
88
* @return {object[]}
99
*/
10-
exports.createBots = function() {
11-
var configFile = require(path.resolve(process.cwd(), process.env.CONFIG_FILE));
10+
exports.createBots = function(configFile) {
1211
var bots = [];
1312

1413
// The config file can be both an array and an object

test/cli.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* jshint expr: true */
2+
var chai = require('chai');
3+
var sinon = require('sinon');
4+
var sinonChai = require('sinon-chai');
5+
var rewire = require('rewire');
6+
var cli = rewire('../lib/cli');
7+
var testConfig = require('./fixtures/test-config.json');
8+
var singleTestConfig = require('./fixtures/single-test-config.json');
9+
10+
chai.should();
11+
chai.use(sinonChai);
12+
13+
describe('CLI', function() {
14+
before(function() {
15+
this.createBotsStub = sinon.stub();
16+
cli.__set__('createBots', this.createBotsStub);
17+
});
18+
19+
afterEach(function() {
20+
this.createBotsStub.reset();
21+
});
22+
23+
it('should be possible to give the config as an env var', function() {
24+
process.env.CONFIG_FILE = process.cwd() + '/test/fixtures/test-config.json';
25+
process.argv = ['node', 'index.js'];
26+
cli();
27+
this.createBotsStub.should.have.been.calledWith(testConfig);
28+
});
29+
30+
it('should be possible to give the config as an option', function() {
31+
delete process.env.CONFIG_FILE;
32+
process.argv = ['node', 'index.js',
33+
'--config', process.cwd() + '/test/fixtures/single-test-config.json'];
34+
cli();
35+
this.createBotsStub.should.have.been.calledWith(singleTestConfig);
36+
});
37+
});

test/create-bots.test.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
/* jshint expr: true */
22
var chai = require('chai');
3+
var logger = require('winston');
34
var sinon = require('sinon');
45
var sinonChai = require('sinon-chai');
56
var createBots = require('../lib/helpers').createBots;
67
var Bot = require('../lib/bot');
78
var ConfigurationError = require('../lib/errors').ConfigurationError;
9+
var index = require('../index');
10+
var testConfig = require('./fixtures/test-config.json');
11+
var singleTestConfig = require('./fixtures/single-test-config.json');
12+
var badConfig = require('./fixtures/bad-config.json');
13+
var stringConfig = require('./fixtures/string-config.json');
814

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

1924
afterEach(function() {
2025
this.connectStub.reset();
2126
});
2227

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

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

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

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

4648
it('should throw if a configuration file is neither an object or an array', function() {
47-
process.env.CONFIG_FILE = process.cwd() + '/test/fixtures/string-config.json';
48-
4949
function wrap() {
50-
createBots();
50+
createBots(stringConfig);
5151
}
5252

5353
(wrap).should.throw(ConfigurationError);
5454
});
55+
56+
it('should be possible to run it through require(\'slack-irc\')', function() {
57+
var bots = index(singleTestConfig);
58+
bots.length.should.equal(1);
59+
this.connectStub.should.have.been.called;
60+
});
5561
});

0 commit comments

Comments
 (0)