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

Deploy to Redis #1

Merged
merged 1 commit into from
Apr 23, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
machine:
node:
version: 0.12.0

dependencies:
pre:
- npm install -g bower
override:
- npm i
- bower i
74 changes: 73 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,78 @@
/* jshint node: true */
'use strict';

var fs = require('fs');
var denodeify = require('rsvp').denodeify;
var path = require('path');
var chalk = require('chalk');

var blue = chalk.blue;
var red = chalk.red;

var Promise = require('ember-cli/lib/ext/promise');

var readFile = denodeify(fs.readFile);

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

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

function _readFileContents(path) {
return readFile(path)
.then(function(buffer) {
return buffer.toString();
});
}

function _beginMessage(ui, indexPath) {
var filename = path.basename(indexPath);

ui.write(blue('| '));
ui.write(blue('- Uploading `' + filename + '`\n'));

return Promise.resolve();
}

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

return Promise.resolve(key);
}

function _errorMessage(ui, error) {
ui.write(blue('| '));
ui.write(red('- ' + error + '`\n'));

return Promise.reject(error);
}

return {
name: options.name,

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 projectName = deployment.project.name();
var tag = context.tag;
var key = projectName + ':index';

var indexPath = context.indexPath;

return _beginMessage(ui, indexPath)
.then(_readFileContents.bind(this, indexPath))
.then(redis.upload.bind(redis, key, tag))
.then(_successMessage.bind(this, ui))
.then(function(key) {
return { redisKey: key }
})
.catch(_errorMessage.bind(this, ui));
}
};
}
};
56 changes: 56 additions & 0 deletions lib/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var CoreObject = require('core-object');
var Promise = require('ember-cli/lib/ext/promise');

module.exports = CoreObject.extend({
init: function(options) {
this._client = options.redisClient || require('then-redis').createClient(options);

this._maxNumberOfRecentUploads = 10;
},

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

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

var maxEntries = this._maxNumberOfRecentUploads;

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

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

return Promise.resolve()
.then(function() {
return client.get(redisKey);
})
.then(function(value) {
if (value) {
return Promise.reject('Value already exists for key: ' + redisKey);
}
})
.then(function() {
return client.set(redisKey, value);
})
},

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

_trimRecentUploadsList: function(key, maxEntries) {
var client = this._client;
return client.ltrim(key, 0, maxEntries - 1);
}
});
22 changes: 16 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"start": "ember server",
"build": "ember build",
"test": "ember try:testall"
"test": "node tests/runner.js"
},
"repository": "",
"engines": {
Expand All @@ -19,6 +19,8 @@
"license": "MIT",
"devDependencies": {
"broccoli-asset-rev": "^2.0.2",
"chai": "^2.2.0",
"chai-as-promised": "^5.0.0",
"ember-cli": "0.2.3",
"ember-cli-app-version": "0.3.3",
"ember-cli-content-security-policy": "0.4.0",
Expand All @@ -29,17 +31,25 @@
"ember-cli-qunit": "0.3.10",
"ember-cli-uglify": "1.0.1",
"ember-data": "1.0.0-beta.16.1",
"ember-export-application-global": "^1.0.2",
"ember-disable-prototype-extensions": "^1.0.0",
"ember-try": "0.0.4"
"ember-export-application-global": "^1.0.2",
"ember-try": "0.0.4",
"glob": "^5.0.5",
"mocha": "^2.2.4"
},
"keywords": [
"ember-addon"
"ember-addon",
"ember-cli-deploy-plugin"
],
"dependencies": {
"ember-cli-babel": "^5.0.0"
"chalk": "^1.0.0",
"core-object": "^1.1.0",
"ember-cli-babel": "^5.0.0",
"redis": "^0.12.1",
"rsvp": "^3.0.18",
"then-redis": "^1.3.0"
},
"ember-addon": {
"configPath": "tests/dummy/config"
}
}
}
27 changes: 27 additions & 0 deletions tests/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var glob = require('glob');
var Mocha = require('mocha');

var mocha = new Mocha({
reporter: 'spec'
});

var arg = process.argv[2];
var root = 'tests/';

function addFiles(mocha, files) {
glob.sync(root + files).forEach(mocha.addFile.bind(mocha));
}

addFiles(mocha, '/**/*-nodetest.js');

if (arg === 'all') {
addFiles(mocha, '/**/*-nodetest-slow.js');
}

mocha.run(function(failures) {
process.on('exit', function() {
process.exit(failures);
});
});
60 changes: 60 additions & 0 deletions tests/unit/index-nodetest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

var Promise = require('ember-cli/lib/ext/promise');

var assert = require('ember-cli/tests/helpers/assert');

describe('redis plugin', function() {
var subject;

before(function() {
subject = require('../../index');
});

it('has a name', function() {
var result = subject.createDeployPlugin({
name: 'test-plugin'
});

assert.equal(result.name, 'test-plugin');
});

it('implements the correct hooks', function() {
var result = subject.createDeployPlugin({
name: 'test-plugin'
});

assert.equal(typeof result.upload, 'function');
});

describe('upload hook', function() {
it('uploads the index to redis', function() {
var plugin = subject.createDeployPlugin({
name: 'test-plugin'
});

var context = {
redisClient: {
upload: function() {
return Promise.resolve('redis-key');
}
},
tag: 'some-tag',
indexPath: 'tests/index.html',
deployment: {
ui: { write: function() {} },
project: { name: function() { return 'test-project'; } },
config: {}
}
};

return assert.isFulfilled(plugin.upload.call(plugin, context))
.then(function(result) {
assert.deepEqual(result, { redisKey: 'redis-key' });
});
});

it('returns the uploaded key', function() {
});
});
});