-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
162 lines (140 loc) · 5.27 KB
/
Copy pathindex.js
File metadata and controls
162 lines (140 loc) · 5.27 KB
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
/* eslint-env node */
'use strict';
var Promise = require('rsvp').Promise;
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-rest',
createDeployPlugin: function (options) {
var RestClient = require('./lib/rest-client');
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
// eslint-disable-next-line ember/avoid-leaking-state-in-ember-objects
defaultConfig: {
filePattern: 'index.html',
distDir: function (context) {
return context.distDir;
},
keyPrefix: function (context) {
return context.project.name();
},
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);
},
restClient: function () {
return new RestClient(this);
},
revisionData: function (context) {
return context.revisionData;
},
},
// eslint-disable-next-line ember/avoid-leaking-state-in-ember-objects
requiredConfig: ['baseUrl', 'username', 'password'],
upload: function () {
var restClient = this.readConfig('restClient');
var revisionKey = this.readConfig('revisionKey');
var distDir = this.readConfig('distDir');
var filePattern = this.readConfig('filePattern');
var keyPrefix = this.readConfig('keyPrefix');
var revisionData = this.readConfig('revisionData');
var filePath = path.join(distDir, filePattern);
this.log('Uploading `' + filePath + '`', { verbose: true });
return this._readFileContents(filePath)
.then(restClient.upload.bind(restClient, keyPrefix, revisionKey, revisionData))
.then(this._uploadSuccessMessage.bind(this))
.then(function (key) {
return { key: key };
})
.catch(this._errorMessage.bind(this));
},
willActivate: function (/* context */) {
var restClient = this.readConfig('restClient');
var keyPrefix = this.readConfig('keyPrefix');
var revisionKey = restClient.activeRevision(keyPrefix);
return {
revisionData: {
previousRevisionKey: revisionKey,
},
};
},
activate: function (/* context */) {
var restClient = this.readConfig('restClient');
var revisionKey = this.readConfig('revisionKey');
var keyPrefix = this.readConfig('keyPrefix');
this.log('Activating revision `' + revisionKey + '`', {
verbose: true,
});
return Promise.resolve(restClient.activate(keyPrefix, revisionKey))
.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 restClient = this.readConfig('restClient');
var keyPrefix = this.readConfig('keyPrefix');
this.log('Listing initial revisions for key: `' + keyPrefix + '`', {
verbose: true,
});
return Promise.resolve(restClient.fetchRevisions(keyPrefix))
.then(function (revisions) {
return {
initialRevisions: revisions,
};
})
.catch(this._errorMessage.bind(this));
},
fetchRevisions: function (/* context */) {
var restClient = this.readConfig('restClient');
var keyPrefix = this.readConfig('keyPrefix');
this.log('Listing revisions for key: `' + keyPrefix + '`');
return restClient.fetchRevisions(keyPrefix).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 Promise.resolve(key);
},
_errorMessage: function (error) {
this.log(error, { color: 'red' });
return Promise.reject(error);
},
});
return new DeployPlugin();
},
};