This repository has been archived by the owner on Jun 20, 2022. It is now read-only.
forked from ember-cli-deploy/ember-cli-deploy-redis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
136 lines (116 loc) · 4.69 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
/* jshint node: true */
'use strict';
var Promise = require('ember-cli/lib/ext/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-redis',
createDeployPlugin: function(options) {
var Redis = require('./lib/redis');
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
host: 'localhost',
port: 6379,
filePattern: 'index.html',
distDir: function(context) {
return context.distDir;
},
keyPrefix: function(context){
return context.project.name() + ':index';
},
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) {
var redisOptions = this.pluginConfig;
var redisLib = context._redisLib;
return new Redis(redisOptions, redisLib);
}
},
configure: function(/* context */) {
this.log('validating config');
if (!this.pluginConfig.url) {
['host', 'port'].forEach(this.applyDefaultConfigProperty.bind(this));
}
['filePattern', 'distDir', 'keyPrefix', 'revisionKey', 'didDeployMessage', 'redisDeployClient'].forEach(this.applyDefaultConfigProperty.bind(this));
this.log('config ok');
},
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 + '`');
return this._readFileContents(filePath)
.then(redisDeployClient.upload.bind(redisDeployClient, keyPrefix, revisionKey))
.then(this._uploadSuccessMessage.bind(this))
.then(function(key) {
return { redisKey: key };
})
.catch(this._errorMessage.bind(this));
},
activate: function(/* context */) {
var redisDeployClient = this.readConfig('redisDeployClient');
var revisionKey = this.readConfig('revisionKey');
var keyPrefix = this.readConfig('keyPrefix');
this.log('Activating revision `' + revisionKey + '`');
return Promise.resolve(redisDeployClient.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);
}
},
fetchRevisions: function(context) {
var redisDeployClient = this.readConfig('redisDeployClient');
var keyPrefix = this.readConfig('keyPrefix');
this.log('Listing revisions for key: `' + keyPrefix + '`');
return Promise.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 + '`');
return Promise.resolve(key);
},
_errorMessage: function(error) {
this.log(error, { color: 'red' });
return Promise.reject(error);
}
});
return new DeployPlugin();
}
};