forked from validkeys/ember-cli-deploy-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
199 lines (171 loc) · 7.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* jshint node: true */
'use strict';
var RSVP = require('rsvp');
var path = require('path');
var fs = require('fs');
var denodeify = require('rsvp').denodeify;
var readFile = denodeify(fs.readFile);
var DeployPluginBase = require('ember-cli-deploy-plugin');
module.exports = {
name: 'ember-cli-deploy-redis',
createDeployPlugin: function(options) {
var Redis = require('./lib/redis');
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
host: 'localhost',
port: function(context) {
if (context.tunnel && context.tunnel.srcPort) {
return context.tunnel.srcPort;
} else {
return 6379;
}
},
filePattern: 'index.html',
maxRecentUploads: 10,
distDir: function(context) {
return context.distDir;
},
keyPrefix: function(context){
return context.project.name() + ':index';
},
activationSuffix: 'current',
activeContentSuffix: 'current-content',
didDeployMessage: function(context){
var revisionKey = context.revisionData && context.revisionData.revisionKey;
var activatedRevisionKey = context.revisionData && context.revisionData.activatedRevisionKey;
if (revisionKey && !activatedRevisionKey) {
return "Deployed but did not activate revision " + revisionKey + ". "
+ "To activate, run: "
+ "ember deploy:activate " + context.deployTarget + " --revision=" + revisionKey + "\n";
}
},
revisionKey: function(context) {
return context.commandOptions.revision || (context.revisionData && context.revisionData.revisionKey);
},
redisDeployClient: function(context, pluginHelper) {
var redisLib = context._redisLib;
var options = {
url: pluginHelper.readConfig('url'),
host: pluginHelper.readConfig('host'),
port: pluginHelper.readConfig('port'),
tls: pluginHelper.readConfig('tls'),
password: pluginHelper.readConfig('password'),
database: pluginHelper.readConfig('database'),
maxRecentUploads: pluginHelper.readConfig('maxRecentUploads'),
allowOverwrite: pluginHelper.readConfig('allowOverwrite'),
activationSuffix: pluginHelper.readConfig('activationSuffix')
};
return new Redis(options, redisLib);
},
revisionData: function(context) {
return context.revisionData;
}
},
configure: function(/* context */) {
this.log('validating config', { verbose: true });
if (!this.pluginConfig.url) {
['host', 'port'].forEach(this.applyDefaultConfigProperty.bind(this));
} else {
var redisUrlRegexp = new RegExp('^redis://');
if (!this.pluginConfig.url.match(redisUrlRegexp)) {
throw new Error('Your Redis URL appears to be missing the "redis://" protocol. Update your URL to: redis://' + this.pluginConfig.url);
}
}
['filePattern', 'distDir', 'keyPrefix', 'activationSuffix', 'activeContentSuffix', 'revisionKey', 'didDeployMessage', 'redisDeployClient', 'maxRecentUploads', 'revisionData','tls'].forEach(this.applyDefaultConfigProperty.bind(this));
this.log('config ok', { verbose: true });
},
upload: function(/* context */) {
var redisDeployClient = this.readConfig('redisDeployClient');
var revisionKey = this.readConfig('revisionKey');
var distDir = this.readConfig('distDir');
var filePattern = this.readConfig('filePattern');
var keyPrefix = this.readConfig('keyPrefix');
var filePath = path.join(distDir, filePattern);
this.log('Uploading `' + filePath + '`', { verbose: true });
return this._readFileContents(filePath)
.then(redisDeployClient.upload.bind(redisDeployClient, keyPrefix, revisionKey, this.readConfig('revisionData')))
.then(this._uploadSuccessMessage.bind(this))
.then(function(key) {
return { redisKey: key };
})
.catch(this._errorMessage.bind(this));
},
willActivate: function(/* context */) {
var redisDeployClient = this.readConfig('redisDeployClient');
var keyPrefix = this.readConfig('keyPrefix');
var revisionKey = redisDeployClient.activeRevision(keyPrefix);
return RSVP.resolve(revisionKey).then(function(previousRevisionKey) {
return {
revisionData: {
previousRevisionKey: previousRevisionKey
}
};
});
},
activate: function(/* context */) {
var redisDeployClient = this.readConfig('redisDeployClient');
var revisionKey = this.readConfig('revisionKey');
var keyPrefix = this.readConfig('keyPrefix');
var activationSuffix = this.readConfig('activationSuffix');
var activeContentSuffix = this.readConfig('activeContentSuffix');
this.log('Activating revision `' + revisionKey + '`', { verbose: true });
return RSVP.resolve(redisDeployClient.activate(keyPrefix, revisionKey, activationSuffix, activeContentSuffix))
.then(this.log.bind(this, '✔ Activated revision `' + revisionKey + '`', {}))
.then(function(){
return {
revisionData: {
activatedRevisionKey: revisionKey
}
};
})
.catch(this._errorMessage.bind(this));
},
didDeploy: function(/* context */){
var didDeployMessage = this.readConfig('didDeployMessage');
if (didDeployMessage) {
this.log(didDeployMessage);
}
},
fetchInitialRevisions: function(/* context */) {
var redisDeployClient = this.readConfig('redisDeployClient');
var keyPrefix = this.readConfig('keyPrefix');
this.log('Listing initial revisions for key: `' + keyPrefix + '`', { verbose: true });
return RSVP.resolve(redisDeployClient.fetchRevisions(keyPrefix))
.then(function(revisions) {
return {
initialRevisions: revisions
};
})
.catch(this._errorMessage.bind(this));
},
fetchRevisions: function(/* context */) {
var redisDeployClient = this.readConfig('redisDeployClient');
var keyPrefix = this.readConfig('keyPrefix');
this.log('Listing revisions for key: `' + keyPrefix + '`');
return RSVP.resolve(redisDeployClient.fetchRevisions(keyPrefix))
.then(function(revisions) {
return {
revisions: revisions
};
})
.catch(this._errorMessage.bind(this));
},
_readFileContents: function(path) {
return readFile(path)
.then(function(buffer) {
return buffer.toString();
});
},
_uploadSuccessMessage: function(key) {
this.log('Uploaded with key `' + key + '`', { verbose: true });
return RSVP.resolve(key);
},
_errorMessage: function(error) {
this.log(error, { color: 'red' });
return RSVP.reject(error);
}
});
return new DeployPlugin();
}
};