Skip to content

Commit b8e77c1

Browse files
author
Aaron Chambers
committed
Merge pull request #2 from strange-studios/use-config-before-context
Now we're using user defined config over context
2 parents 4214740 + 0f2da59 commit b8e77c1

File tree

5 files changed

+148
-17
lines changed

5 files changed

+148
-17
lines changed

index.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/* jshint node: true */
22
'use strict';
33

4+
var Promise = require('ember-cli/lib/ext/promise');
45
var fs = require('fs');
5-
var denodeify = require('rsvp').denodeify;
6-
var path = require('path');
7-
var chalk = require('chalk');
86

7+
var chalk = require('chalk');
98
var blue = chalk.blue;
109
var red = chalk.red;
1110

12-
var Promise = require('ember-cli/lib/ext/promise');
13-
11+
var denodeify = require('rsvp').denodeify;
1412
var readFile = denodeify(fs.readFile);
1513

14+
var validateConfig = require('./lib/utilities/validate-config');
15+
1616
module.exports = {
1717
name: 'ember-cli-deploy-redis',
1818

@@ -27,10 +27,8 @@ module.exports = {
2727
}
2828

2929
function _beginMessage(ui, indexPath) {
30-
var filename = path.basename(indexPath);
31-
3230
ui.write(blue('| '));
33-
ui.write(blue('- Uploading `' + filename + '`\n'));
31+
ui.write(blue('- Uploading `' + indexPath + '`\n'));
3432

3533
return Promise.resolve();
3634
}
@@ -52,6 +50,18 @@ module.exports = {
5250
return {
5351
name: options.name,
5452

53+
willDeploy: function(context) {
54+
var deployment = context.deployment;
55+
var ui = deployment.ui;
56+
var config = deployment.config[this.name] = deployment.config[this.name] || {};
57+
58+
return validateConfig(ui, config)
59+
.then(function() {
60+
ui.write(blue('| '));
61+
ui.writeLine(blue('- config ok'));
62+
});
63+
},
64+
5565
upload: function(context) {
5666
var deployment = context.deployment;
5767
var ui = deployment.ui;
@@ -62,10 +72,10 @@ module.exports = {
6272
var tag = context.tag;
6373
var key = projectName + ':index';
6474

65-
var indexPath = context.indexPath;
75+
var filePattern = config.filePattern;
6676

67-
return _beginMessage(ui, indexPath)
68-
.then(_readFileContents.bind(this, indexPath))
77+
return _beginMessage(ui, filePattern)
78+
.then(_readFileContents.bind(this, filePattern))
6979
.then(redis.upload.bind(redis, key, tag))
7080
.then(_successMessage.bind(this, ui))
7181
.then(function(key) {

lib/redis.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ var Promise = require('ember-cli/lib/ext/promise');
33

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

817
this._maxNumberOfRecentUploads = 10;
918
},

lib/utilities/validate-config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var Promise = require('ember-cli/lib/ext/promise');
2+
3+
var chalk = require('chalk');
4+
var yellow = chalk.yellow;
5+
var blue = chalk.blue;
6+
7+
module.exports = function(ui, config) {
8+
ui.write(blue('| '));
9+
ui.writeLine(blue('- validating config'));
10+
11+
var defaultConfig = {
12+
host: 'localhost',
13+
port: 6379,
14+
filePattern: 'dist/index.html'
15+
};
16+
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+
});
25+
26+
return Promise.resolve();
27+
}

tests/unit/index-nodetest.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,72 @@ describe('redis plugin', function() {
2424
name: 'test-plugin'
2525
});
2626

27+
assert.equal(typeof result.willDeploy, 'function');
2728
assert.equal(typeof result.upload, 'function');
2829
});
2930

30-
describe('upload hook', function() {
31-
it('uploads the index to redis', function() {
31+
describe('willDeploy hook', function() {
32+
it('resolves if config is ok', function() {
3233
var plugin = subject.createDeployPlugin({
33-
name: 'test-plugin'
34+
name: 'redis'
3435
});
3536

3637
var context = {
38+
deployment: {
39+
ui: { write: function() {}, writeLine: function() {} },
40+
config: {
41+
redis: {
42+
host: 'somehost',
43+
port: 1234
44+
}
45+
}
46+
}
47+
};
48+
49+
return assert.isFulfilled(plugin.willDeploy.call(plugin, context))
50+
});
51+
});
52+
53+
describe('upload hook', function() {
54+
var plugin;
55+
var context;
56+
57+
beforeEach(function() {
58+
plugin = subject.createDeployPlugin({
59+
name: 'redis'
60+
});
61+
62+
context = {
3763
redisClient: {
3864
upload: function() {
3965
return Promise.resolve('redis-key');
4066
}
4167
},
4268
tag: 'some-tag',
43-
indexPath: 'tests/index.html',
4469
deployment: {
4570
ui: { write: function() {} },
4671
project: { name: function() { return 'test-project'; } },
47-
config: {}
72+
config: {
73+
redis: {
74+
filePattern: 'tests/index.html',
75+
}
76+
}
4877
}
4978
};
79+
});
5080

81+
it('uploads the index to redis', function() {
5182
return assert.isFulfilled(plugin.upload.call(plugin, context))
5283
.then(function(result) {
5384
assert.deepEqual(result, { redisKey: 'redis-key' });
5485
});
5586
});
5687

5788
it('returns the uploaded key', function() {
89+
return assert.isFulfilled(plugin.upload.call(plugin, context))
90+
.then(function(result) {
91+
assert.deepEqual(result.redisKey, 'redis-key');
92+
});
5893
});
5994
});
6095
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var assert = require('ember-cli/tests/helpers/assert');
2+
3+
describe('validate-config', function() {
4+
var subject;
5+
var config;
6+
var mockUi;
7+
8+
before(function() {
9+
subject = require('../../../../lib/utilities/validate-config');
10+
});
11+
12+
beforeEach(function() {
13+
config = { };
14+
15+
mockUi = {
16+
messages: [],
17+
write: function() { },
18+
writeLine: function(message) {
19+
this.messages.push(message);
20+
}
21+
};
22+
});
23+
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+
}
31+
32+
return previous;
33+
}, []);
34+
35+
assert.equal(messages.length, 3);
36+
});
37+
});
38+
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+
});
45+
});
46+
47+
it('resolves if config is ok', function() {
48+
return assert.isFulfilled(subject(mockUi, config));
49+
})
50+
});

0 commit comments

Comments
 (0)