Skip to content

Update to more modern Javascript syntax #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 6,
ecmaVersion: 10,
sourceType: 'module'
},
extends: 'eslint:recommended',
Expand Down
195 changes: 96 additions & 99 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
/* jshint node: true */
'use strict';

var RSVP = require('rsvp');
var path = require('path');
var fs = require('fs');
let path = require('path');
let fs = require('fs');

var denodeify = require('rsvp').denodeify;
var readFile = denodeify(fs.readFile);

var DeployPluginBase = require('ember-cli-deploy-plugin');
let DeployPluginBase = require('ember-cli-deploy-plugin');

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

createDeployPlugin: function(options) {
createDeployPlugin(options) {
var Redis = require('./lib/redis');

var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
host: 'localhost',
port: function(context) {
port(context) {
if (context.tunnel && context.tunnel.srcPort) {
return context.tunnel.srcPort;
} else {
Expand All @@ -29,27 +25,27 @@ module.exports = {
},
filePattern: 'index.html',
maxRecentUploads: 10,
distDir: function(context) {
distDir(context) {
return context.distDir;
},
keyPrefix: function(context){
return context.project.name() + ':index';
keyPrefix(context){
return `${context.project.name()}:index`;
},
activationSuffix: 'current',
activeContentSuffix: 'current-content',
didDeployMessage: function(context){
didDeployMessage(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";
return `Deployed but did not activate revision ${revisionKey}. `
+ `To activate, run: `
+ `ember deploy:activate ${context.deployTarget} --revision=${revisionKey}` + "\n";
}
},
revisionKey: function(context) {
revisionKey(context) {
return context.commandOptions.revision || (context.revisionData && context.revisionData.revisionKey);
},
redisDeployClient: function(context, pluginHelper) {
redisDeployClient(context, pluginHelper) {
var redisLib = context._redisLib;
var options = {
url: pluginHelper.readConfig('url'),
Expand All @@ -65,11 +61,11 @@ module.exports = {
return new Redis(options, redisLib);
},

revisionData: function(context) {
revisionData(context) {
return context.revisionData;
}
},
configure: function(/* context */) {
configure(/* context */) {
this.log('validating config', { verbose: true });

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

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

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

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 + '`', { verbose: true });
return this._readFileContents(filePath)
.then(redisDeployClient.upload.bind(redisDeployClient, keyPrefix, revisionKey, this.readConfig('revisionData')))
.then(this._uploadSuccessMessage.bind(this))
.then(function(key) {
return { redisKey: key };
})
.catch(this._errorMessage.bind(this));
async upload(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let revisionKey = this.readConfig('revisionKey');
let distDir = this.readConfig('distDir');
let filePattern = this.readConfig('filePattern');
let keyPrefix = this.readConfig('keyPrefix');
let filePath = path.join(distDir, filePattern);

this.log(`Uploading \`${filePath}\``, { verbose: true });
try {
let fileContents = await this._readFileContents(filePath);
let key = await redisDeployClient.upload(keyPrefix, revisionKey, this.readConfig('revisionData'), fileContents);
this._logUploadSuccessMessage(key);
return { redisKey: key };
} catch(e) {
this._logErrorMessage(e);
throw e;
}
},

willActivate: function(/* context */) {
var redisDeployClient = this.readConfig('redisDeployClient');
var keyPrefix = this.readConfig('keyPrefix');
async willActivate(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let keyPrefix = this.readConfig('keyPrefix');

var revisionKey = redisDeployClient.activeRevision(keyPrefix);
let previousRevisionKey = await redisDeployClient.activeRevision(keyPrefix);
return {
revisionData: {
previousRevisionKey
}
};
},

return RSVP.resolve(revisionKey).then(function(previousRevisionKey) {
async activate(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let revisionKey = this.readConfig('revisionKey');
let keyPrefix = this.readConfig('keyPrefix');
let activationSuffix = this.readConfig('activationSuffix');
let activeContentSuffix = this.readConfig('activeContentSuffix');

this.log(`Activating revision \`${revisionKey}\``, { verbose: true });
try {
await redisDeployClient.activate(keyPrefix, revisionKey, activationSuffix, activeContentSuffix);
this.log(`✔ Activated revision \`${revisionKey}\``, {});
return {
revisionData: {
previousRevisionKey: previousRevisionKey
activatedRevisionKey: revisionKey
}
};
});
},

activate: function(/* context */) {
var redisDeployClient = this.readConfig('redisDeployClient');
var revisionKey = this.readConfig('revisionKey');
var keyPrefix = this.readConfig('keyPrefix');
var activationSuffix = this.readConfig('activationSuffix');
var activeContentSuffix = this.readConfig('activeContentSuffix');

this.log('Activating revision `' + revisionKey + '`', { verbose: true });
return RSVP.resolve(redisDeployClient.activate(keyPrefix, revisionKey, activationSuffix, activeContentSuffix))
.then(this.log.bind(this, '✔ Activated revision `' + revisionKey + '`', {}))
.then(function(){
return {
revisionData: {
activatedRevisionKey: revisionKey
}
};
})
.catch(this._errorMessage.bind(this));
} catch(e) {
this._logErrorMessage(e);
throw e;
}
},

didDeploy: function(/* context */){
didDeploy(/* context */){
var didDeployMessage = this.readConfig('didDeployMessage');
if (didDeployMessage) {
this.log(didDeployMessage);
}
},

fetchInitialRevisions: function(/* context */) {
var redisDeployClient = this.readConfig('redisDeployClient');
var keyPrefix = this.readConfig('keyPrefix');

this.log('Listing initial revisions for key: `' + keyPrefix + '`', { verbose: true });
return RSVP.resolve(redisDeployClient.fetchRevisions(keyPrefix))
.then(function(revisions) {
return {
initialRevisions: revisions
};
})
.catch(this._errorMessage.bind(this));
async fetchInitialRevisions(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let keyPrefix = this.readConfig('keyPrefix');

this.log(`Listing initial revisions for key: \`${keyPrefix}\``, { verbose: true });
try {
let initialRevisions = await redisDeployClient.fetchRevisions(keyPrefix);
return {
initialRevisions
};
} catch(e) {
this._logErrorMessage(e);
throw e;
}
},

fetchRevisions: function(/* context */) {
var redisDeployClient = this.readConfig('redisDeployClient');
var keyPrefix = this.readConfig('keyPrefix');

this.log('Listing revisions for key: `' + keyPrefix + '`');
return RSVP.resolve(redisDeployClient.fetchRevisions(keyPrefix))
.then(function(revisions) {
return {
revisions: revisions
};
})
.catch(this._errorMessage.bind(this));
async fetchRevisions(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let keyPrefix = this.readConfig('keyPrefix');

this.log(`Listing revisions for key: \`${keyPrefix}\``);
try {
let revisions = await redisDeployClient.fetchRevisions(keyPrefix);
return {
revisions
};
} catch(e) {
this._logErrorMessage(e);
throw e;
}
},

_readFileContents: function(path) {
return readFile(path)
.then(function(buffer) {
return buffer.toString();
});
async _readFileContents(path) {
let buffer = await fs.promises.readFile(path);
return buffer.toString();
},

_uploadSuccessMessage: function(key) {
this.log('Uploaded with key `' + key + '`', { verbose: true });
return RSVP.resolve(key);
_logUploadSuccessMessage(key) {
this.log(`Uploaded with key \`${key}\``, { verbose: true });
},

_errorMessage: function(error) {
_logErrorMessage(error) {
this.log(error, { color: 'red' });
return RSVP.reject(error);
}
});

Expand Down
Loading