Skip to content

Commit 1d87bba

Browse files
author
Aaron Chambers
committed
Merge pull request #5 from lukemelia/configurable-key-prefix
Allow keyPrefix to be configurable. Defaults to "[project name]:index"
2 parents f5e643b + ab10fc8 commit 1d87bba

File tree

5 files changed

+68
-30
lines changed

5 files changed

+68
-30
lines changed

index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ module.exports = {
5151
name: options.name,
5252

5353
willDeploy: function(context) {
54-
var deployment = context.deployment;
55-
var ui = deployment.ui;
56-
var config = deployment.config[this.name] = deployment.config[this.name] || {};
54+
var deployment = context.deployment;
55+
var ui = deployment.ui;
56+
var config = deployment.config[this.name] = deployment.config[this.name] || {};
57+
var projectName = deployment.project.name();
5758

58-
return validateConfig(ui, config)
59+
return validateConfig(ui, config, projectName)
5960
.then(function() {
6061
ui.write(blue('| '));
6162
ui.writeLine(blue('- config ok'));
@@ -67,16 +68,13 @@ module.exports = {
6768
var ui = deployment.ui;
6869
var config = deployment.config[this.name] || {};
6970
var redis = context.redisClient || new Redis(config);
70-
71-
var projectName = deployment.project.name();
7271
var tag = context.tag;
73-
var key = projectName + ':index';
7472

7573
var filePattern = config.filePattern;
7674

7775
return _beginMessage(ui, filePattern)
7876
.then(_readFileContents.bind(this, filePattern))
79-
.then(redis.upload.bind(redis, key, tag))
77+
.then(redis.upload.bind(redis, config.keyPrefix, tag))
8078
.then(_successMessage.bind(this, ui))
8179
.then(function(key) {
8280
return { redisKey: key }

lib/redis.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ module.exports = CoreObject.extend({
2323
this._allowOverwrite = options.allowOverwrite;
2424
},
2525

26-
upload: function(/*key, tag, value*/) {
26+
upload: function(/*keyPrefix, tag, value*/) {
2727
var args = Array.prototype.slice.call(arguments);
2828

29-
var key = args.shift();
29+
var keyPrefix = args.shift();
3030
var value = args.pop();
3131
var tag = args[0] || 'default';
32-
var redisKey = key + ':' + tag;
32+
var redisKey = keyPrefix + ':' + tag;
3333

3434
var maxEntries = this._maxNumberOfRecentUploads;
3535

3636
return Promise.resolve()
3737
.then(this._uploadIfKeyDoesNotExist.bind(this, redisKey, value))
38-
.then(this._updateRecentUploadsList.bind(this, key, tag))
39-
.then(this._trimRecentUploadsList.bind(this, key, maxEntries))
38+
.then(this._updateRecentUploadsList.bind(this, keyPrefix, tag))
39+
.then(this._trimRecentUploadsList.bind(this, keyPrefix, maxEntries))
4040
.then(function() {
4141
return redisKey;
4242
});
@@ -60,13 +60,13 @@ module.exports = CoreObject.extend({
6060
})
6161
},
6262

63-
_updateRecentUploadsList: function(key, tag) {
63+
_updateRecentUploadsList: function(keyPrefix, tag) {
6464
var client = this._client;
65-
return client.lpush(key, tag);
65+
return client.lpush(keyPrefix, tag);
6666
},
6767

68-
_trimRecentUploadsList: function(key, maxEntries) {
68+
_trimRecentUploadsList: function(keyPrefix, maxEntries) {
6969
var client = this._client;
70-
return client.ltrim(key, 0, maxEntries - 1);
70+
return client.ltrim(keyPrefix, 0, maxEntries - 1);
7171
}
7272
});

lib/utilities/validate-config.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,25 @@ function applyDefaultConfigIfNecessary(config, prop, defaultConfig, ui){
1313
}
1414
}
1515

16-
module.exports = function(ui, config) {
16+
module.exports = function(ui, config, projectName) {
1717
ui.write(blue('| '));
1818
ui.writeLine(blue('- validating config'));
1919

2020
var defaultConfig = {
2121
host: 'localhost',
2222
port: 6379,
23-
filePattern: 'dist/index.html'
23+
filePattern: 'dist/index.html',
24+
keyPrefix: projectName + ':index'
2425
};
2526

2627
if (!config.url) {
2728
['host', 'port'].forEach(function(prop) {
2829
applyDefaultConfigIfNecessary(config, prop, defaultConfig, ui);
2930
});
3031
}
31-
applyDefaultConfigIfNecessary(config, 'filePattern', defaultConfig, ui);
32+
['filePattern', 'keyPrefix'].forEach(function(configKey){
33+
applyDefaultConfigIfNecessary(config, configKey, defaultConfig, ui);
34+
});
3235

3336
return Promise.resolve();
3437
}

tests/unit/index-nodetest.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ describe('redis plugin', function() {
3737
var context = {
3838
deployment: {
3939
ui: { write: function() {}, writeLine: function() {} },
40+
project: {
41+
name: function(){
42+
return 'my-project';
43+
}
44+
},
4045
config: {
4146
redis: {
4247
host: 'somehost',

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

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ describe('validate-config', function() {
44
var subject;
55
var config;
66
var mockUi;
7+
var projectName = 'my-project';
78

89
before(function() {
910
subject = require('../../../../lib/utilities/validate-config');
@@ -24,7 +25,7 @@ describe('validate-config', function() {
2425
config = { };
2526
});
2627
it('warns about missing optional config', function() {
27-
return assert.isFulfilled(subject(mockUi, config))
28+
return assert.isFulfilled(subject(mockUi, config, projectName))
2829
.then(function() {
2930
var messages = mockUi.messages.reduce(function(previous, current) {
3031
if (/- Missing config:\s.*, using default:\s/.test(current)) {
@@ -33,32 +34,63 @@ describe('validate-config', function() {
3334

3435
return previous;
3536
}, []);
36-
37-
assert.equal(messages.length, 3);
37+
assert.equal(messages.length, 4);
3838
});
3939
});
4040

4141
it('adds default config to the config object', function() {
42-
return assert.isFulfilled(subject(mockUi, config))
42+
return assert.isFulfilled(subject(mockUi, config, projectName))
4343
.then(function() {
4444
assert.isDefined(config.host);
4545
assert.isDefined(config.port);
46+
assert.isDefined(config.keyPrefix);
4647
});
4748
});
4849

4950
it('resolves', function() {
50-
return assert.isFulfilled(subject(mockUi, config));
51+
return assert.isFulfilled(subject(mockUi, config, projectName));
5152
})
5253
});
5354

55+
describe('with a keyPrefix provided', function () {
56+
beforeEach(function() {
57+
config = {
58+
keyPrefix: 'proj:home'
59+
};
60+
});
61+
it('only warns about missing optional filePattern and connection info', function() {
62+
return assert.isFulfilled(subject(mockUi, config, projectName))
63+
.then(function() {
64+
var messages = mockUi.messages.reduce(function(previous, current) {
65+
if (/- Missing config:\s.*, using default:\s/.test(current)) {
66+
previous.push(current);
67+
}
68+
69+
return previous;
70+
}, []);
71+
72+
assert.equal(messages.length, 3);
73+
});
74+
});
75+
it('does not add default config to the config object', function() {
76+
return assert.isFulfilled(subject(mockUi, config, projectName))
77+
.then(function() {
78+
assert.isDefined(config.host);
79+
assert.isDefined(config.port);
80+
assert.isDefined(config.filePattern);
81+
assert.equal(config.keyPrefix, 'proj:home');
82+
});
83+
});
84+
});
85+
5486
describe('with a url provided', function () {
5587
beforeEach(function() {
5688
config = {
5789
url: 'redis://localhost:6379'
5890
};
5991
});
60-
it('only warns about missing optional filePattern', function() {
61-
return assert.isFulfilled(subject(mockUi, config))
92+
it('warns about missing optional filePattern and keyPrefix only', function() {
93+
return assert.isFulfilled(subject(mockUi, config, projectName))
6294
.then(function() {
6395
var messages = mockUi.messages.reduce(function(previous, current) {
6496
if (/- Missing config:\s.*, using default:\s/.test(current)) {
@@ -68,12 +100,12 @@ describe('validate-config', function() {
68100
return previous;
69101
}, []);
70102

71-
assert.equal(messages.length, 1);
103+
assert.equal(messages.length, 2);
72104
});
73105
});
74106

75107
it('does not add default config to the config object', function() {
76-
return assert.isFulfilled(subject(mockUi, config))
108+
return assert.isFulfilled(subject(mockUi, config, projectName))
77109
.then(function() {
78110
assert.isUndefined(config.host);
79111
assert.isUndefined(config.port);
@@ -82,7 +114,7 @@ describe('validate-config', function() {
82114
});
83115

84116
it('resolves', function() {
85-
return assert.isFulfilled(subject(mockUi, config));
117+
return assert.isFulfilled(subject(mockUi, config, projectName));
86118
})
87119
});
88120
});

0 commit comments

Comments
 (0)