-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
179 lines (151 loc) · 5.62 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
/* jshint node: true */
'use strict';
var Promise = require('ember-cli/lib/ext/promise');
var DeployPluginBase = require('ember-cli-deploy-plugin');
var path = require('path');
var fs = require('fs');
var denodeify = require('rsvp').denodeify;
var readFile = denodeify(fs.readFile);
module.exports = {
name: 'ember-cli-deploy-mysql',
createDeployPlugin: function (options) {
var MySQL = require('./lib/mysql');
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 3306;
}
},
maxRecentUploads: 10,
distDir: function (context) {
return context.distDir;
},
filePattern: 'index.html',
tableName: function (context) {
return context.project.name().replace(/-/g, '_') + '_bootstrap';
},
revisionKey: function (context) {
return context.commandOptions.revision || (context.revisionData && context.revisionData.revisionKey);
},
deployClient: function (context) {
var mysqlOptions = this.pluginConfig;
mysqlOptions.port = this.readConfig('port');
var mysqlLib = context._mysqlLib;
return new MySQL(mysqlOptions, mysqlLib);
},
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";
}
}
},
requiredConfig: ['database', 'user', 'password'],
configure: function (/* context */) {
this.log('validating config', { verbose: true });
var properties = [
'distDir',
'filePattern',
'tableName',
'revisionKey',
'didDeployMessage',
'deployClient',
'maxRecentUploads'
];
if (!this.pluginConfig.url) {
properties.push('host');
properties.push('port');
}
properties.forEach(this.applyDefaultConfigProperty.bind(this));
this.log('config ok', { verbose: true });
},
upload: function (/* context */) {
var deployClient = this.readConfig('deployClient');
var tableName = this.readConfig('tableName');
var revisionKey = this.readConfig('revisionKey');
var distDir = this.readConfig('distDir');
var filePattern = this.readConfig('filePattern');
var filePath = path.join(distDir, filePattern);
this.log('Uploading `' + filePath + '`', { verbose: true });
return this._readFileContents(filePath)
.then(deployClient.upload.bind(deployClient, tableName, revisionKey))
.then(this._uploadSuccessMessage.bind(this))
.then(function (args) {
return {
tableName: args[0],
revisionKey: args[1]
};
})
.catch(this._errorMessage.bind(this));
},
willActivate: function (/* context */) {
var deployClient = this.readConfig('deployClient');
var tableName = this.readConfig('tableName');
var revisionKey = deployClient.activeRevisionKey(tableName);
return {
revisionData: {
previousRevisionKey: revisionKey
}
};
},
activate: function (/* context */) {
var deployClient = this.readConfig('deployClient');
var revisionKey = this.readConfig('revisionKey');
var tableName = this.readConfig('tableName');
this.log('Activating revision `' + revisionKey + '`', { verbose: true });
return Promise.resolve(deployClient.activate(tableName, 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);
}
},
fetchRevisions: function (/* context */) {
var deployClient = this.readConfig('deployClient');
var tableName = this.readConfig('tableName');
this.log('Listing revisions in table: `' + tableName + '`');
return Promise.resolve(deployClient.fetchRevisions(tableName))
.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 (args) {
this.log('Uploaded to table `' + args[0] +
'` with key `' + args[1] + '`', { verbose: true });
return Promise.resolve(args);
},
_errorMessage: function (error) {
this.log(error, { color: 'red' });
return Promise.reject(error);
}
});
return new DeployPlugin();
}
};