Skip to content

Commit ee9af93

Browse files
committed
Restructure to use ember-cli-deploy-plugin
1 parent cbb39ce commit ee9af93

File tree

5 files changed

+292
-401
lines changed

5 files changed

+292
-401
lines changed

index.js

Lines changed: 69 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,138 +4,107 @@
44
var Promise = require('ember-cli/lib/ext/promise');
55
var fs = require('fs');
66

7-
var chalk = require('chalk');
8-
var blue = chalk.blue;
9-
var red = chalk.red;
10-
117
var denodeify = require('rsvp').denodeify;
128
var readFile = denodeify(fs.readFile);
139

14-
var validateConfig = require('./lib/utilities/validate-config');
10+
var DeployPluginBase = require('ember-cli-deploy-plugin');
1511

1612
module.exports = {
1713
name: 'ember-cli-deploy-redis',
1814

1915
createDeployPlugin: function(options) {
2016
var Redis = require('./lib/redis');
2117

22-
function _readFileContents(path) {
23-
return readFile(path)
24-
.then(function(buffer) {
25-
return buffer.toString();
26-
});
27-
}
28-
29-
function _beginUploadMessage(ui, indexPath) {
30-
ui.write(blue('| '));
31-
ui.write(blue('- Uploading `' + indexPath + '`\n'));
32-
33-
return Promise.resolve();
34-
}
35-
36-
function _beginActivateMessage(ui, revisionKey) {
37-
ui.write(blue('| '));
38-
ui.write(blue('- Activating revision `' + revisionKey + '`\n'));
39-
40-
return Promise.resolve();
41-
}
42-
43-
function _successMessage(ui, key) {
44-
ui.write(blue('| '));
45-
ui.write(blue('- Uploaded with key `' + key + '`\n'));
46-
47-
return Promise.resolve(key);
48-
}
49-
50-
function _activationSuccessMessage(ui, revisionKey) {
51-
ui.write(blue('| '));
52-
ui.write(blue('- ✔ Activated revision `' + revisionKey + '`\n'));
53-
54-
return Promise.resolve();
55-
}
56-
57-
function _errorMessage(ui, error) {
58-
ui.write(blue('| '));
59-
ui.write(red('- ' + error + '`\n'));
60-
61-
return Promise.reject(error);
62-
}
63-
64-
return {
18+
var DeployPlugin = DeployPluginBase.extend({
6519
name: options.name,
20+
defaultConfig: {
21+
host: 'localhost',
22+
port: 6379,
23+
filePattern: 'dist/index.html',
24+
keyPrefix: function(context){
25+
return context.project.name() + ':index';
26+
},
27+
didDeployMessage: function(context){
28+
if (context.revisionKey && !context.activatedRevisionKey) {
29+
return "Deployed but did not activate revision " + context.revisionKey + ". "
30+
+ "To activate, run: "
31+
+ "ember activate " + context.revisionKey + " --environment=" + context.deployEnvironment + "\n";
32+
}
33+
},
34+
revisionKey: function(context) {
35+
return context.commandLineArgs.revisionKey || context.revisionKey;
36+
},
37+
redisDeployClient: function(context) {
38+
return context.redisDeployClient || new Redis(context.config.redis);
39+
}
40+
},
41+
configure: function(/* context */) {
42+
this.log('validating config');
6643

67-
configure: function(context) {
68-
var deployment = context.deployment;
69-
var ui = deployment.ui;
70-
var config = deployment.config[this.name] = deployment.config[this.name] || {};
71-
var projectName = deployment.project.name();
44+
if (!this.pluginConfig.url) {
45+
['host', 'port'].forEach(this.applyDefaultConfigProperty.bind(this));
46+
}
47+
['filePattern', 'keyPrefix', 'revisionKey', 'didDeployMessage', 'redisDeployClient'].forEach(this.applyDefaultConfigProperty.bind(this));
7248

73-
return this._resolvePipelineData(config, context)
74-
.then(validateConfig.bind(this, ui, config, projectName));
49+
this.log('config ok');
7550
},
7651

77-
upload: function(context) {
78-
var deployment = context.deployment;
79-
var ui = deployment.ui;
80-
var config = deployment.config[this.name] || {};
81-
var redis = context.redisClient || new Redis(config);
82-
var revisionKey = this._resolveConfigValue('revisionKey', config, context);
52+
upload: function(/* context */) {
53+
var redisDeployClient = this.readConfig('redisDeployClient');
54+
var revisionKey = this.readConfig('revisionKey');
55+
var filePattern = this.readConfig('filePattern');
56+
var keyPrefix = this.readConfig('keyPrefix');
8357

84-
var filePattern = config.filePattern;
85-
86-
return _beginUploadMessage(ui, filePattern)
87-
.then(_readFileContents.bind(this, filePattern))
88-
.then(redis.upload.bind(redis, config.keyPrefix, revisionKey))
89-
.then(_successMessage.bind(this, ui))
58+
this.log('Uploading `' + filePattern + '`');
59+
return this._readFileContents(filePattern)
60+
.then(redisDeployClient.upload.bind(redisDeployClient, keyPrefix, revisionKey))
61+
.then(this._uploadSuccessMessage.bind(this))
9062
.then(function(key) {
91-
return { redisKey: key }
63+
return { redisKey: key };
9264
})
93-
.catch(_errorMessage.bind(this, ui));
65+
.catch(this._errorMessage.bind(this));
9466
},
9567

96-
activate: function(context) {
97-
var deployment = context.deployment;
98-
var ui = deployment.ui;
99-
var config = deployment.config[this.name] || {};
100-
var redis = context.redisClient || new Redis(config);
101-
var revisionKey = this._resolveConfigValue('revisionKey', config, context);
68+
activate: function(/* context */) {
69+
var redisDeployClient = this.readConfig('redisDeployClient');
70+
var revisionKey = this.readConfig('revisionKey');
71+
var keyPrefix = this.readConfig('keyPrefix');
10272

103-
return _beginActivateMessage(ui, revisionKey)
104-
.then(redis.activate.bind(redis, config.keyPrefix, revisionKey))
73+
this.log('Activating revision `' + revisionKey + '`');
74+
return Promise.resolve(redisDeployClient.activate(keyPrefix, revisionKey))
75+
.then(this.log.bind(this, '✔ Activated revision `' + revisionKey + '`'))
10576
.then(function(){
106-
context.activatedRevisionKey = revisionKey;
77+
return {
78+
activatedRevisionKey: revisionKey
79+
};
10780
})
108-
.then(_activationSuccessMessage.bind(this, ui, revisionKey))
109-
.catch(_errorMessage.bind(this, ui));
81+
.catch(this._errorMessage.bind(this));
11082
},
11183

112-
didDeploy: function(context){
113-
var deployment = context.deployment;
114-
var ui = deployment.ui;
115-
var config = deployment.config[this.name] || {};
116-
var didDeployMessage = this._resolveConfigValue('didDeployMessage', config, context);
84+
didDeploy: function(/* context */){
85+
var didDeployMessage = this.readConfig('didDeployMessage');
11786
if (didDeployMessage) {
118-
ui.write(blue('| '));
119-
ui.write(blue(didDeployMessage + '`\n'));
87+
this.log(didDeployMessage);
12088
}
121-
return Promise.resolve();
12289
},
12390

124-
_resolvePipelineData: function(config, context) {
125-
config.revisionKey = config.revisionKey || function(context) {
126-
return context.deployment.commandLineArgs.revisionKey || context.revisionKey;
127-
};
128-
129-
return Promise.resolve();
91+
_readFileContents: function(path) {
92+
return readFile(path)
93+
.then(function(buffer) {
94+
return buffer.toString();
95+
});
13096
},
13197

132-
_resolveConfigValue: function(key, config, context) {
133-
if(typeof config[key] === 'function') {
134-
return config[key](context);
135-
}
98+
_uploadSuccessMessage: function(key) {
99+
this.log('Uploaded with key `' + key + '`');
100+
return Promise.resolve(key);
101+
},
136102

137-
return config[key];
103+
_errorMessage: function(error) {
104+
this.log(error, { color: 'red' });
105+
return Promise.reject(error);
138106
}
139-
};
107+
});
108+
return new DeployPlugin();
140109
}
141110
};

lib/utilities/validate-config.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"dependencies": {
4545
"chalk": "^1.0.0",
4646
"core-object": "^1.1.0",
47+
"ember-cli-deploy-plugin": "0.1.1",
4748
"ember-cli-babel": "^5.0.0",
4849
"redis": "^0.12.1",
4950
"rsvp": "^3.0.18",

0 commit comments

Comments
 (0)