-
Notifications
You must be signed in to change notification settings - Fork 41
/
index.js
195 lines (170 loc) · 6.79 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
/* jshint node: true */
'use strict';
let path = require('path');
let fs = require('fs');
let DeployPluginBase = require('ember-cli-deploy-plugin');
module.exports = {
name: 'ember-cli-deploy-redis',
createDeployPlugin(options) {
var Redis = require('./lib/redis');
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
host: 'localhost',
port(context) {
if (context.tunnel && context.tunnel.srcPort) {
return context.tunnel.srcPort;
} else {
return 6379;
}
},
filePattern: 'index.html',
maxRecentUploads: 10,
distDir(context) {
return context.distDir;
},
keyPrefix(context){
return `${context.project.name()}:index`;
},
activationSuffix: 'current',
activeContentSuffix: 'current-content',
didDeployMessage(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(context) {
return context.commandOptions.revision || (context.revisionData && context.revisionData.revisionKey);
},
redisDeployClient(context, pluginHelper) {
let redisLib = context._redisLib;
let libOptions = {
url: pluginHelper.readConfig('url'),
host: pluginHelper.readConfig('host'),
port: pluginHelper.readConfig('port'),
password: pluginHelper.readConfig('password'),
database: pluginHelper.readConfig('database'),
redisOptions: pluginHelper.readConfig('redisOptions'),
maxRecentUploads: pluginHelper.readConfig('maxRecentUploads'),
allowOverwrite: pluginHelper.readConfig('allowOverwrite'),
activationSuffix: pluginHelper.readConfig('activationSuffix')
};
return new Redis(libOptions, redisLib);
},
revisionData(context) {
return context.revisionData;
}
},
configure(/* context */) {
this.log('validating config', { verbose: true });
if (!this.pluginConfig.url) {
['host', 'port'].forEach(this.applyDefaultConfigProperty.bind(this));
} else {
var redisUrlRegexp = new RegExp('^rediss?://');
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'].forEach(this.applyDefaultConfigProperty.bind(this));
this.log('config ok', { verbose: true });
},
async upload(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let revisionKey = this.readConfig('revisionKey');
let distDir = this.readConfig('distDir');
let filePattern = this.readConfig('filePattern');
let keyPrefix = this.readConfig('keyPrefix');
let filePath = path.join(distDir, filePattern);
this.log(`Uploading \`${filePath}\``, { verbose: true });
try {
let fileContents = await this._readFileContents(filePath);
let key = await redisDeployClient.upload(keyPrefix, revisionKey, this.readConfig('revisionData'), fileContents);
this._logUploadSuccessMessage(key);
return { redisKey: key };
} catch(e) {
this._logErrorMessage(e);
throw e;
}
},
async willActivate(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let keyPrefix = this.readConfig('keyPrefix');
let previousRevisionKey = await redisDeployClient.activeRevision(keyPrefix);
return {
revisionData: {
previousRevisionKey
}
};
},
async activate(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let revisionKey = this.readConfig('revisionKey');
let keyPrefix = this.readConfig('keyPrefix');
let activationSuffix = this.readConfig('activationSuffix');
let activeContentSuffix = this.readConfig('activeContentSuffix');
this.log(`Activating revision \`${revisionKey}\``, { verbose: true });
try {
await redisDeployClient.activate(keyPrefix, revisionKey, activationSuffix, activeContentSuffix);
this.log(`✔ Activated revision \`${revisionKey}\``, {});
return {
revisionData: {
activatedRevisionKey: revisionKey
}
};
} catch(e) {
this._logErrorMessage(e);
throw e;
}
},
didDeploy(/* context */){
var didDeployMessage = this.readConfig('didDeployMessage');
if (didDeployMessage) {
this.log(didDeployMessage);
}
},
async fetchInitialRevisions(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let keyPrefix = this.readConfig('keyPrefix');
this.log(`Fetching initial revisions for key: \`${keyPrefix}\``, { verbose: true });
try {
let initialRevisions = await redisDeployClient.fetchRevisions(keyPrefix);
return {
initialRevisions
};
} catch(e) {
this._logErrorMessage(e);
throw e;
}
},
async fetchRevisions(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let keyPrefix = this.readConfig('keyPrefix');
this.log(`Fetching revisions for key: \`${keyPrefix}\``, { verbose: true });
try {
let revisions = await redisDeployClient.fetchRevisions(keyPrefix);
return {
revisions
};
} catch(e) {
this._logErrorMessage(e);
throw e;
}
},
async _readFileContents(path) {
let buffer = await fs.promises.readFile(path);
return buffer.toString();
},
_logUploadSuccessMessage(key) {
this.log(`Uploaded with key \`${key}\``, { verbose: true });
},
_logErrorMessage(error) {
this.log(error, { color: 'red' });
}
});
return new DeployPlugin();
}
};