Skip to content
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

Implement activate hook #8

Merged
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
66 changes: 53 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,34 @@ module.exports = {
});
}

function _beginMessage(ui, indexPath) {
function _beginUploadMessage(ui, indexPath) {
ui.write(blue('| '));
ui.write(blue('- Uploading `' + indexPath + '`\n'));

return Promise.resolve();
}

function _beginActivateMessage(ui, revisionKey) {
ui.write(blue('| '));
ui.write(blue('- Activating revision `' + revisionKey + '`\n'));

return Promise.resolve();
}

function _successMessage(ui, key) {
ui.write(blue('| '));
ui.write(blue('- Uploaded with key `' + key + '`\n'));

return Promise.resolve(key);
}

function _activationSuccessMessage(ui, revisionKey) {
ui.write(blue('| '));
ui.write(blue('- ✔ Activated revision `' + revisionKey + '`\n'));

return Promise.resolve();
}

function _errorMessage(ui, error) {
ui.write(blue('| '));
ui.write(red('- ' + error + '`\n'));
Expand All @@ -56,30 +70,56 @@ module.exports = {
var config = deployment.config[this.name] = deployment.config[this.name] || {};
var projectName = deployment.project.name();

return validateConfig(ui, config, projectName)
.then(function() {
ui.write(blue('| '));
ui.writeLine(blue('- config ok'));
});
return this._resolvePipelineData(config, context)
.then(validateConfig.bind(this, ui, config, projectName));
},

upload: function(context) {
var deployment = context.deployment;
var ui = deployment.ui;
var config = deployment.config[this.name] || {};
var redis = context.redisClient || new Redis(config);
var tag = context.tag;
var deployment = context.deployment;
var ui = deployment.ui;
var config = deployment.config[this.name] || {};
var redis = context.redisClient || new Redis(config);
var revisionKey = this._resolveConfigValue('revisionKey', config, context);

var filePattern = config.filePattern;

return _beginMessage(ui, filePattern)
return _beginUploadMessage(ui, filePattern)
.then(_readFileContents.bind(this, filePattern))
.then(redis.upload.bind(redis, config.keyPrefix, tag))
.then(redis.upload.bind(redis, config.keyPrefix, revisionKey))
.then(_successMessage.bind(this, ui))
.then(function(key) {
return { redisKey: key }
})
.catch(_errorMessage.bind(this, ui));
},

activate: function(context) {
var deployment = context.deployment;
var ui = deployment.ui;
var config = deployment.config[this.name] || {};
var redis = context.redisClient || new Redis(config);
var revisionKey = this._resolveConfigValue('revisionKey', config, context);

return _beginActivateMessage(ui, revisionKey)
.then(redis.activate.bind(redis, config.keyPrefix, revisionKey))
.then(_activationSuccessMessage.bind(this, ui, revisionKey))
.catch(_errorMessage.bind(this, ui));
},

_resolvePipelineData: function(config, context) {
config.revisionKey = config.revisionKey || function(context) {
return context.deployment.commandLineArgs.revisionKey || context.revisionKey;
};

return Promise.resolve();
},

_resolveConfigValue: function(key, config, context) {
if(typeof config[key] === 'function') {
return config[key](context);
}

return config[key];
}
};
}
Expand Down
41 changes: 32 additions & 9 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,50 @@ module.exports = CoreObject.extend({
this._allowOverwrite = options.allowOverwrite;
},

upload: function(/*keyPrefix, tag, value*/) {
upload: function(/*keyPrefix, revisionKey, value*/) {
var args = Array.prototype.slice.call(arguments);

var keyPrefix = args.shift();
var value = args.pop();
var tag = args[0] || 'default';
var redisKey = keyPrefix + ':' + tag;
var keyPrefix = args.shift();
var value = args.pop();
var revisionKey = args[0] || 'default';
var redisKey = keyPrefix + ':' + revisionKey;

var maxEntries = this._maxNumberOfRecentUploads;

return Promise.resolve()
.then(this._uploadIfKeyDoesNotExist.bind(this, redisKey, value))
.then(this._updateRecentUploadsList.bind(this, keyPrefix, tag))
.then(this._updateRecentUploadsList.bind(this, keyPrefix, revisionKey))
.then(this._trimRecentUploadsList.bind(this, keyPrefix, maxEntries))
.then(function() {
return redisKey;
});
},

_uploadIfKeyDoesNotExist: function(redisKey, value) {
activate: function(keyPrefix, revisionKey) {
var currentKey = keyPrefix + ':current';

return Promise.resolve()
.then(this._listRevisions.bind(this, keyPrefix))
.then(this._validateRevisionKey.bind(this, revisionKey))
.then(this._activateRevisionKey.bind(this, currentKey, revisionKey));
},

_listRevisions: function(keyPrefix) {
var client = this._client;
return client.lrange(keyPrefix, 0, this._maxNumberOfRecentUploads - 1);
},

_validateRevisionKey: function(revisionKey, revisions) {
return revisions.indexOf(revisionKey) > -1 ? Promise.resolve() : Promise.reject('`' + revisionKey + '` is not a valid revision key');
},

_activateRevisionKey: function(currentKey, revisionKey) {
var client = this._client;
return client.set(currentKey, revisionKey);
},

_uploadIfKeyDoesNotExist: function(redisKey, value) {
var client = this._client;
var allowOverwrite = !!this._allowOverwrite;

return Promise.resolve()
Expand All @@ -60,9 +83,9 @@ module.exports = CoreObject.extend({
})
},

_updateRecentUploadsList: function(keyPrefix, tag) {
_updateRecentUploadsList: function(keyPrefix, revisionKey) {
var client = this._client;
return client.lpush(keyPrefix, tag);
return client.lpush(keyPrefix, revisionKey);
},

_trimRecentUploadsList: function(keyPrefix, maxEntries) {
Expand Down
3 changes: 3 additions & 0 deletions lib/utilities/validate-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ module.exports = function(ui, config, projectName) {
applyDefaultConfigIfNecessary(config, configKey, defaultConfig, ui);
});

ui.write(blue('| '));
ui.writeLine(blue('- config ok'));

return Promise.resolve();
}
Loading