Skip to content

Commit d9966f9

Browse files
authored
Merge pull request #95 from ember-cli-deploy/chore/async-await
Update to more modern Javascript syntax
2 parents 0cdb9aa + 758382f commit d9966f9

File tree

5 files changed

+398
-576
lines changed

5 files changed

+398
-576
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
root: true,
33
parserOptions: {
4-
ecmaVersion: 6,
4+
ecmaVersion: 10,
55
sourceType: 'module'
66
},
77
extends: 'eslint:recommended',

index.js

Lines changed: 96 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
/* jshint node: true */
22
'use strict';
33

4-
var RSVP = require('rsvp');
5-
var path = require('path');
6-
var fs = require('fs');
4+
let path = require('path');
5+
let fs = require('fs');
76

8-
var denodeify = require('rsvp').denodeify;
9-
var readFile = denodeify(fs.readFile);
10-
11-
var DeployPluginBase = require('ember-cli-deploy-plugin');
7+
let DeployPluginBase = require('ember-cli-deploy-plugin');
128

139
module.exports = {
1410
name: 'ember-cli-deploy-redis',
1511

16-
createDeployPlugin: function(options) {
12+
createDeployPlugin(options) {
1713
var Redis = require('./lib/redis');
1814

1915
var DeployPlugin = DeployPluginBase.extend({
2016
name: options.name,
2117
defaultConfig: {
2218
host: 'localhost',
23-
port: function(context) {
19+
port(context) {
2420
if (context.tunnel && context.tunnel.srcPort) {
2521
return context.tunnel.srcPort;
2622
} else {
@@ -29,27 +25,27 @@ module.exports = {
2925
},
3026
filePattern: 'index.html',
3127
maxRecentUploads: 10,
32-
distDir: function(context) {
28+
distDir(context) {
3329
return context.distDir;
3430
},
35-
keyPrefix: function(context){
36-
return context.project.name() + ':index';
31+
keyPrefix(context){
32+
return `${context.project.name()}:index`;
3733
},
3834
activationSuffix: 'current',
3935
activeContentSuffix: 'current-content',
40-
didDeployMessage: function(context){
36+
didDeployMessage(context){
4137
var revisionKey = context.revisionData && context.revisionData.revisionKey;
4238
var activatedRevisionKey = context.revisionData && context.revisionData.activatedRevisionKey;
4339
if (revisionKey && !activatedRevisionKey) {
44-
return "Deployed but did not activate revision " + revisionKey + ". "
45-
+ "To activate, run: "
46-
+ "ember deploy:activate " + context.deployTarget + " --revision=" + revisionKey + "\n";
40+
return `Deployed but did not activate revision ${revisionKey}. `
41+
+ `To activate, run: `
42+
+ `ember deploy:activate ${context.deployTarget} --revision=${revisionKey}` + "\n";
4743
}
4844
},
49-
revisionKey: function(context) {
45+
revisionKey(context) {
5046
return context.commandOptions.revision || (context.revisionData && context.revisionData.revisionKey);
5147
},
52-
redisDeployClient: function(context, pluginHelper) {
48+
redisDeployClient(context, pluginHelper) {
5349
var redisLib = context._redisLib;
5450
var options = {
5551
url: pluginHelper.readConfig('url'),
@@ -65,11 +61,11 @@ module.exports = {
6561
return new Redis(options, redisLib);
6662
},
6763

68-
revisionData: function(context) {
64+
revisionData(context) {
6965
return context.revisionData;
7066
}
7167
},
72-
configure: function(/* context */) {
68+
configure(/* context */) {
7369
this.log('validating config', { verbose: true });
7470

7571
if (!this.pluginConfig.url) {
@@ -78,7 +74,7 @@ module.exports = {
7874
var redisUrlRegexp = new RegExp('^redis://');
7975

8076
if (!this.pluginConfig.url.match(redisUrlRegexp)) {
81-
throw new Error('Your Redis URL appears to be missing the "redis://" protocol. Update your URL to: redis://' + this.pluginConfig.url);
77+
throw new Error(`Your Redis URL appears to be missing the "redis://" protocol. Update your URL to: redis://${this.pluginConfig.url}`);
8278
}
8379
}
8480

@@ -87,109 +83,110 @@ module.exports = {
8783
this.log('config ok', { verbose: true });
8884
},
8985

90-
upload: function(/* context */) {
91-
var redisDeployClient = this.readConfig('redisDeployClient');
92-
var revisionKey = this.readConfig('revisionKey');
93-
var distDir = this.readConfig('distDir');
94-
var filePattern = this.readConfig('filePattern');
95-
var keyPrefix = this.readConfig('keyPrefix');
96-
var filePath = path.join(distDir, filePattern);
97-
98-
this.log('Uploading `' + filePath + '`', { verbose: true });
99-
return this._readFileContents(filePath)
100-
.then(redisDeployClient.upload.bind(redisDeployClient, keyPrefix, revisionKey, this.readConfig('revisionData')))
101-
.then(this._uploadSuccessMessage.bind(this))
102-
.then(function(key) {
103-
return { redisKey: key };
104-
})
105-
.catch(this._errorMessage.bind(this));
86+
async upload(/* context */) {
87+
let redisDeployClient = this.readConfig('redisDeployClient');
88+
let revisionKey = this.readConfig('revisionKey');
89+
let distDir = this.readConfig('distDir');
90+
let filePattern = this.readConfig('filePattern');
91+
let keyPrefix = this.readConfig('keyPrefix');
92+
let filePath = path.join(distDir, filePattern);
93+
94+
this.log(`Uploading \`${filePath}\``, { verbose: true });
95+
try {
96+
let fileContents = await this._readFileContents(filePath);
97+
let key = await redisDeployClient.upload(keyPrefix, revisionKey, this.readConfig('revisionData'), fileContents);
98+
this._logUploadSuccessMessage(key);
99+
return { redisKey: key };
100+
} catch(e) {
101+
this._logErrorMessage(e);
102+
throw e;
103+
}
106104
},
107105

108-
willActivate: function(/* context */) {
109-
var redisDeployClient = this.readConfig('redisDeployClient');
110-
var keyPrefix = this.readConfig('keyPrefix');
106+
async willActivate(/* context */) {
107+
let redisDeployClient = this.readConfig('redisDeployClient');
108+
let keyPrefix = this.readConfig('keyPrefix');
111109

112-
var revisionKey = redisDeployClient.activeRevision(keyPrefix);
110+
let previousRevisionKey = await redisDeployClient.activeRevision(keyPrefix);
111+
return {
112+
revisionData: {
113+
previousRevisionKey
114+
}
115+
};
116+
},
113117

114-
return RSVP.resolve(revisionKey).then(function(previousRevisionKey) {
118+
async activate(/* context */) {
119+
let redisDeployClient = this.readConfig('redisDeployClient');
120+
let revisionKey = this.readConfig('revisionKey');
121+
let keyPrefix = this.readConfig('keyPrefix');
122+
let activationSuffix = this.readConfig('activationSuffix');
123+
let activeContentSuffix = this.readConfig('activeContentSuffix');
124+
125+
this.log(`Activating revision \`${revisionKey}\``, { verbose: true });
126+
try {
127+
await redisDeployClient.activate(keyPrefix, revisionKey, activationSuffix, activeContentSuffix);
128+
this.log(`✔ Activated revision \`${revisionKey}\``, {});
115129
return {
116130
revisionData: {
117-
previousRevisionKey: previousRevisionKey
131+
activatedRevisionKey: revisionKey
118132
}
119133
};
120-
});
121-
},
122-
123-
activate: function(/* context */) {
124-
var redisDeployClient = this.readConfig('redisDeployClient');
125-
var revisionKey = this.readConfig('revisionKey');
126-
var keyPrefix = this.readConfig('keyPrefix');
127-
var activationSuffix = this.readConfig('activationSuffix');
128-
var activeContentSuffix = this.readConfig('activeContentSuffix');
129-
130-
this.log('Activating revision `' + revisionKey + '`', { verbose: true });
131-
return RSVP.resolve(redisDeployClient.activate(keyPrefix, revisionKey, activationSuffix, activeContentSuffix))
132-
.then(this.log.bind(this, '✔ Activated revision `' + revisionKey + '`', {}))
133-
.then(function(){
134-
return {
135-
revisionData: {
136-
activatedRevisionKey: revisionKey
137-
}
138-
};
139-
})
140-
.catch(this._errorMessage.bind(this));
134+
} catch(e) {
135+
this._logErrorMessage(e);
136+
throw e;
137+
}
141138
},
142139

143-
didDeploy: function(/* context */){
140+
didDeploy(/* context */){
144141
var didDeployMessage = this.readConfig('didDeployMessage');
145142
if (didDeployMessage) {
146143
this.log(didDeployMessage);
147144
}
148145
},
149146

150-
fetchInitialRevisions: function(/* context */) {
151-
var redisDeployClient = this.readConfig('redisDeployClient');
152-
var keyPrefix = this.readConfig('keyPrefix');
153-
154-
this.log('Listing initial revisions for key: `' + keyPrefix + '`', { verbose: true });
155-
return RSVP.resolve(redisDeployClient.fetchRevisions(keyPrefix))
156-
.then(function(revisions) {
157-
return {
158-
initialRevisions: revisions
159-
};
160-
})
161-
.catch(this._errorMessage.bind(this));
147+
async fetchInitialRevisions(/* context */) {
148+
let redisDeployClient = this.readConfig('redisDeployClient');
149+
let keyPrefix = this.readConfig('keyPrefix');
150+
151+
this.log(`Listing initial revisions for key: \`${keyPrefix}\``, { verbose: true });
152+
try {
153+
let initialRevisions = await redisDeployClient.fetchRevisions(keyPrefix);
154+
return {
155+
initialRevisions
156+
};
157+
} catch(e) {
158+
this._logErrorMessage(e);
159+
throw e;
160+
}
162161
},
163162

164-
fetchRevisions: function(/* context */) {
165-
var redisDeployClient = this.readConfig('redisDeployClient');
166-
var keyPrefix = this.readConfig('keyPrefix');
167-
168-
this.log('Listing revisions for key: `' + keyPrefix + '`');
169-
return RSVP.resolve(redisDeployClient.fetchRevisions(keyPrefix))
170-
.then(function(revisions) {
171-
return {
172-
revisions: revisions
173-
};
174-
})
175-
.catch(this._errorMessage.bind(this));
163+
async fetchRevisions(/* context */) {
164+
let redisDeployClient = this.readConfig('redisDeployClient');
165+
let keyPrefix = this.readConfig('keyPrefix');
166+
167+
this.log(`Listing revisions for key: \`${keyPrefix}\``);
168+
try {
169+
let revisions = await redisDeployClient.fetchRevisions(keyPrefix);
170+
return {
171+
revisions
172+
};
173+
} catch(e) {
174+
this._logErrorMessage(e);
175+
throw e;
176+
}
176177
},
177178

178-
_readFileContents: function(path) {
179-
return readFile(path)
180-
.then(function(buffer) {
181-
return buffer.toString();
182-
});
179+
async _readFileContents(path) {
180+
let buffer = await fs.promises.readFile(path);
181+
return buffer.toString();
183182
},
184183

185-
_uploadSuccessMessage: function(key) {
186-
this.log('Uploaded with key `' + key + '`', { verbose: true });
187-
return RSVP.resolve(key);
184+
_logUploadSuccessMessage(key) {
185+
this.log(`Uploaded with key \`${key}\``, { verbose: true });
188186
},
189187

190-
_errorMessage: function(error) {
188+
_logErrorMessage(error) {
191189
this.log(error, { color: 'red' });
192-
return RSVP.reject(error);
193190
}
194191
});
195192

0 commit comments

Comments
 (0)