Skip to content

Commit 5b87ce7

Browse files
author
Aaron Chambers
committed
Merge pull request #3 from lukemelia/support-url-config
Allow configuration of redis using a url property instead of host/port/etc
2 parents b8e77c1 + d0f33b3 commit 5b87ce7

File tree

4 files changed

+117
-41
lines changed

4 files changed

+117
-41
lines changed

lib/redis.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ var Promise = require('ember-cli/lib/ext/promise');
33

44
module.exports = CoreObject.extend({
55
init: function(options) {
6-
var redisOptions = {
7-
host: options.host,
8-
port: options.port
9-
};
6+
if (options.redisClient) {
7+
this._client = options.redisClient;
8+
} else if (options.url) {
9+
this._client = require('then-redis').createClient(options.url);
10+
} else {
11+
var redisOptions = {
12+
host: options.host,
13+
port: options.port
14+
};
1015

11-
if (options.password) {
12-
redisOptions.password = options.password;
13-
}
14-
15-
this._client = options.redisClient || require('then-redis').createClient(redisOptions);
16+
if (options.password) {
17+
redisOptions.password = options.password;
18+
}
1619

20+
this._client = require('then-redis').createClient(redisOptions);
21+
}
1722
this._maxNumberOfRecentUploads = 10;
23+
this._allowOverwrite = options.allowOverwrite;
1824
},
1925

2026
upload: function(/*key, tag, value*/) {
@@ -38,13 +44,14 @@ module.exports = CoreObject.extend({
3844

3945
_uploadIfKeyDoesNotExist: function(redisKey, value) {
4046
var client = this._client;
47+
var allowOverwrite = !!this._allowOverwrite;
4148

4249
return Promise.resolve()
4350
.then(function() {
4451
return client.get(redisKey);
4552
})
4653
.then(function(value) {
47-
if (value) {
54+
if (value && !allowOverwrite) {
4855
return Promise.reject('Value already exists for key: ' + redisKey);
4956
}
5057
})

lib/utilities/validate-config.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ var chalk = require('chalk');
44
var yellow = chalk.yellow;
55
var blue = chalk.blue;
66

7+
function applyDefaultConfigIfNecessary(config, prop, defaultConfig, ui){
8+
if (!config[prop]) {
9+
var value = defaultConfig[prop];
10+
config[prop] = value;
11+
ui.write(blue('| '));
12+
ui.writeLine(yellow('- Missing config: `' + prop + '`, using default: `' + value + '`'));
13+
}
14+
}
15+
716
module.exports = function(ui, config) {
817
ui.write(blue('| '));
918
ui.writeLine(blue('- validating config'));
@@ -14,14 +23,12 @@ module.exports = function(ui, config) {
1423
filePattern: 'dist/index.html'
1524
};
1625

17-
['host', 'port', 'filePattern'].forEach(function(prop) {
18-
if (!config[prop]) {
19-
var value = defaultConfig[prop];
20-
config[prop] = value;
21-
ui.write(blue('| '));
22-
ui.writeLine(yellow('- Missing config: `' + prop + '`, using default: `' + value + '`'));
23-
}
24-
});
26+
if (!config.url) {
27+
['host', 'port'].forEach(function(prop) {
28+
applyDefaultConfigIfNecessary(config, prop, defaultConfig, ui);
29+
});
30+
}
31+
applyDefaultConfigIfNecessary(config, 'filePattern', defaultConfig, ui);
2532

2633
return Promise.resolve();
2734
}

tests/unit/lib/redis-nodetest.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ describe('redis', function() {
5050
});
5151
});
5252

53+
it('uploads the contents if the key already exists but allowOverwrite is true', function() {
54+
var fileUploaded = false;
55+
56+
var redis = new Redis({
57+
allowOverwrite: true,
58+
redisClient: {
59+
get: function(key) {
60+
return Promise.resolve('some-other-value');
61+
},
62+
set: function(key, value) {
63+
fileUploaded = true;
64+
},
65+
lpush: function() { },
66+
ltrim: function() { }
67+
}
68+
});
69+
70+
var promise = redis.upload('key', 'value');
71+
return assert.isFulfilled(promise)
72+
.then(function() {
73+
assert.ok(fileUploaded);
74+
});
75+
});
76+
5377
it('updates the list of recent uploads once upload is successful', function() {
5478
var recentUploads = [];
5579

tests/unit/lib/utilities/validate-config-nodetest.js

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ describe('validate-config', function() {
1010
});
1111

1212
beforeEach(function() {
13-
config = { };
14-
1513
mockUi = {
1614
messages: [],
1715
write: function() { },
@@ -21,30 +19,70 @@ describe('validate-config', function() {
2119
};
2220
});
2321

24-
it('warns about missing optional config', function() {
25-
return assert.isFulfilled(subject(mockUi, config))
26-
.then(function() {
27-
var messages = mockUi.messages.reduce(function(previous, current) {
28-
if (/- Missing config:\s.*, using default:\s/.test(current)) {
29-
previous.push(current);
30-
}
22+
describe('without providing config', function () {
23+
beforeEach(function() {
24+
config = { };
25+
});
26+
it('warns about missing optional config', function() {
27+
return assert.isFulfilled(subject(mockUi, config))
28+
.then(function() {
29+
var messages = mockUi.messages.reduce(function(previous, current) {
30+
if (/- Missing config:\s.*, using default:\s/.test(current)) {
31+
previous.push(current);
32+
}
3133

32-
return previous;
33-
}, []);
34+
return previous;
35+
}, []);
3436

35-
assert.equal(messages.length, 3);
36-
});
37-
});
37+
assert.equal(messages.length, 3);
38+
});
39+
});
3840

39-
it('adds default config to the config object', function() {
40-
return assert.isFulfilled(subject(mockUi, config))
41-
.then(function() {
42-
assert.isDefined(config.host);
43-
assert.isDefined(config.port);
44-
});
41+
it('adds default config to the config object', function() {
42+
return assert.isFulfilled(subject(mockUi, config))
43+
.then(function() {
44+
assert.isDefined(config.host);
45+
assert.isDefined(config.port);
46+
});
47+
});
48+
49+
it('resolves', function() {
50+
return assert.isFulfilled(subject(mockUi, config));
51+
})
4552
});
4653

47-
it('resolves if config is ok', function() {
48-
return assert.isFulfilled(subject(mockUi, config));
49-
})
54+
describe('with a url provided', function () {
55+
beforeEach(function() {
56+
config = {
57+
url: 'redis://localhost:6379'
58+
};
59+
});
60+
it('only warns about missing optional filePattern', function() {
61+
return assert.isFulfilled(subject(mockUi, config))
62+
.then(function() {
63+
var messages = mockUi.messages.reduce(function(previous, current) {
64+
if (/- Missing config:\s.*, using default:\s/.test(current)) {
65+
previous.push(current);
66+
}
67+
68+
return previous;
69+
}, []);
70+
71+
assert.equal(messages.length, 1);
72+
});
73+
});
74+
75+
it('does not add default config to the config object', function() {
76+
return assert.isFulfilled(subject(mockUi, config))
77+
.then(function() {
78+
assert.isUndefined(config.host);
79+
assert.isUndefined(config.port);
80+
assert.isDefined(config.filePattern);
81+
});
82+
});
83+
84+
it('resolves', function() {
85+
return assert.isFulfilled(subject(mockUi, config));
86+
})
87+
});
5088
});

0 commit comments

Comments
 (0)