-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
207 lines (157 loc) · 7.24 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
200
201
202
203
204
205
206
207
'use strict';
const fs = require('fs');
const path = require('path');
const RSVP = require('rsvp');
const minimatch = require('minimatch');
const denodeify = require('rsvp').denodeify;
const readFile = denodeify(fs.readFile);
const DeployPluginBase = require('ember-cli-deploy-plugin');
const CloudflareClient = require('./lib/cloudflare');
module.exports = {
name: require('./package').name,
createDeployPlugin: function (options) {
const DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
filePattern: 'index.html',
activationSuffix: 'current',
activationContentSuffix: 'current-content',
distDir: function(context) {
return context.distDir;
},
distFiles: function(context) {
return context.distFiles || [];
},
didDeployMessage: function(context){
const revisionKey = context.revisionData && context.revisionData.revisionKey;
const 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}`;
}
},
revisionKey: function(context) {
return context.commandOptions.revision || (context.revisionData && context.revisionData.revisionKey);
},
revisionData: function(context) {
return context.revisionData;
},
keyPrefix: function(context) {
return context.project.name();
},
cfClient: function(_context, pluginHelper) {
const email = pluginHelper.readConfig('email');
const apiKey = pluginHelper.readConfig('apiKey');
const accountId = pluginHelper.readConfig('accountId');
const namespace = pluginHelper.readConfig('namespace');
return new CloudflareClient(email, apiKey, accountId, namespace);
}
},
requiredConfig: ['accountId', 'email', 'apiKey', 'namespace'],
upload: async function(/* context */) {
const cfClient = this.readConfig('cfClient');
const distDir = this.readConfig('distDir');
const distFiles = this.readConfig('distFiles');
const filePattern = this.readConfig('filePattern');
const keyPrefix = this.readConfig('keyPrefix');
const revisionKey = this.readConfig('revisionKey');
const filesToUpload = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
if (!filesToUpload.includes('index.html')) {
return this._errorMessage(new Error("`filePattern` must include index.html as your app's entry point"));
}
const uploadPromises = filesToUpload.map(async fp => {
this.log(`Uploading \`${fp}\``, { verbose: true });
try {
const fileContents = await this._readFileContents(path.join(distDir, fp));
return await cfClient.fetch(`${keyPrefix}-${path.parse(fp).name}-${revisionKey}`, 'PUT', fileContents);
} catch(e) {
return this._errorMessage(e);
}
});
try {
await RSVP.all(uploadPromises);
filesToUpload.forEach(fp => this.log(`Uploaded with key \`${keyPrefix}-${path.parse(fp).name}-${revisionKey}\``, { verbose: true }));
await this._updateRevisionList(`${keyPrefix}-index-${revisionKey}`);
} catch (e) {
return this._errorMessage(e);
}
return RSVP.resolve();
},
willActivate: function(/* context */) {
return RSVP.resolve();
},
activate: async function(/* context */) {
const cfClient = this.readConfig('cfClient');
const keyPrefix = this.readConfig('keyPrefix');
const revisionKey = this.readConfig('revisionKey');
const activationSuffix = this.readConfig('activationSuffix');
const activationContentSuffix = this.readConfig('activationContentSuffix');
const keyName = `${keyPrefix}-index-${revisionKey}`;
const activationKey = `${keyPrefix}-index-${activationSuffix}`;
const activationContentKey = `${keyPrefix}-index-${activationContentSuffix}`;
this.log(`Activating revision \`${revisionKey}\``, { verbose: true });
const contentResponse = await cfClient.fetch(keyName, 'GET');
const body = await contentResponse.text();
await cfClient.fetch(activationContentKey, 'PUT', body);
await cfClient.fetch(activationKey, 'PUT', revisionKey);
this.log(`✔ Activated revision \`${revisionKey}\``);
return RSVP.resolve({
revisionData: {
activatedRevisionKey: revisionKey
}
});
},
didDeploy: function(/* context */){
const didDeployMessage = this.readConfig('didDeployMessage');
if (didDeployMessage) {
this.log(didDeployMessage);
}
},
fetchInitialRevisions: async function() {
const cfClient = this.readConfig('cfClient');
const keyPrefix = this.readConfig('keyPrefix');
this.log(`Listing initial revisions for key: \`${keyPrefix}-index\``);
const resp = await cfClient.getRevisions(`${keyPrefix}-index`);
const revisionJson = await resp.json();
const revisions = revisionJson ? revisionJson.revisions : [];
this.log(revisions.toString(), { verbose: true });
return revisions;
},
fetchRevisions: async function(/* context */) {
const cfClient = this.readConfig('cfClient');
const keyPrefix = this.readConfig('keyPrefix');
this.log(`Listing revisions for key: \`${keyPrefix}-index\``);
const resp = await cfClient.getRevisions(`${keyPrefix}-index`);
const revisionJson = await resp.json();
const revisions = revisionJson ? revisionJson.revisions : [];
this.log(revisions.toString(), { verbose: true });
return revisions;
},
_updateRevisionList: async function(revisionKey) {
this.log(`Updating revision list with \`${revisionKey}\``, { verbose: true });
const revisionBody = {};
const cfClient = this.readConfig('cfClient');
const keyPrefix = this.readConfig('keyPrefix');
const getRevisions = await cfClient.getRevisions(`${keyPrefix}-index`)
if (getRevisions.ok) {
const currentRevisions = await getRevisions.json();
revisionBody.revisions = [...currentRevisions.revisions, revisionKey];
} else {
revisionBody.revisions = [revisionKey];
}
await cfClient.fetch(`${keyPrefix}-index-revisions`, 'PUT', JSON.stringify(revisionBody));
},
_readFileContents: function(path) {
return readFile(path)
.then(function(buffer) {
return buffer.toString();
});
},
_errorMessage: function(error) {
this.log(error, { color: 'red' });
return RSVP.reject(error);
},
});
return new DeployPlugin();
}
};