Skip to content

Commit cee04f1

Browse files
committed
Merge pull request #2 from achambers/set-current-version
Now we can set a specific release version to be the current one
2 parents fa4cc61 + 89d7760 commit cee04f1

File tree

5 files changed

+101
-141
lines changed

5 files changed

+101
-141
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ Secondly, the function will keep track of the versions of `data` that have been
5656

5757
Finally, this function will trim the list of uploaded versions to a limit defined by `versionCount`.
5858

59+
### \#setCurrent(key)
60+
61+
This function will set a key called `appId:current` to be the version key specified. This will only happen if the version key specified is a valid version that has previously been uploaded.
62+
63+
The server that serves the index.html from Redis should look for the index.html file stored under `appId:current` if not query parameter has been passed in, specifying another version.
64+
5965
## Installation
6066

6167
From within your [Ember CLI][2] application, run:
@@ -74,10 +80,12 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.
7480

7581
## Release History
7682

83+
- [v0.0.2][5]
7784
- [v0.0.1][4]
7885

7986

8087
[1]: https://github.com/achambers/ember-cli-deploy "ember-cli-deploy"
8188
[2]: http://ember-cli.com "Ember CLI"
8289
[3]: https://github.com/achambers/ember-cli-deploy#adapters "ember-cli-deploy adapters"
8390
[4]: https://github.com/achambers/ember-cli-deploy-redis-index-adapter/releases/tag/v0.0.1 "Release v0.0.1"
91+
[5]: https://github.com/achambers/ember-cli-deploy-redis-index-adapter/releases/tag/v0.0.2 "Release v0.0.2"

lib/redis-index-adapter.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ var Adapter = CoreObject.extend({
1616

1717
upload: function(data) {
1818
var key = this._key();
19-
var promises = [
20-
this._uploadIfNotInVersionList(key, data),
21-
this._updateVersionList(key)
22-
];
2319

24-
return CLIPromise.all(promises)
20+
return this._uploadIfNotInVersionList(key, data)
21+
.then(this._updateVersionList.bind(this, key))
2522
.then(this._trimVersionList.bind(this))
2623
.then(function() {
2724
return key;
2825
});
2926
},
3027

28+
setCurrent: function(key) {
29+
return this._setCurrentIfInVersionList(key);
30+
},
31+
3132
_key: function() {
3233
var cmd = new GitCommand('./', 'rev-parse', ['--short=10'], 'HEAD');
3334
return cmd.execSync().trim();
@@ -40,14 +41,29 @@ var Adapter = CoreObject.extend({
4041
return this._listVersions()
4142
.then(function(keys) {
4243
if (keys.indexOf(key) === -1) {
43-
return self.client.set(redisKey, value);
44+
return self._set(redisKey, value);
4445
} else {
4546
var message = 'Version for key [' + key + ']' + ' has already been uploaded\n';
4647
return CLIPromise.reject(new SilentError(message));
4748
}
4849
});
4950
},
5051

52+
_setCurrentIfInVersionList: function(key) {
53+
var self = this;
54+
var appId = this.appId;
55+
56+
return this._listVersions()
57+
.then(function(versions) {
58+
if (versions.indexOf(key) === -1) {
59+
var message = 'Version for key [' + key + ']' + ' does not exist\n';
60+
return CLIPromise.reject(new SilentError(message));
61+
} else {
62+
return self._set(appId + ':current', key);
63+
}
64+
});
65+
},
66+
5167
_updateVersionList: function(key) {
5268
var self = this;
5369

@@ -71,6 +87,10 @@ var Adapter = CoreObject.extend({
7187
return this.client.lrange(this.appId, 0, count - 1);
7288
},
7389

90+
_set: function(key, value) {
91+
return this.client.set(key, value);
92+
},
93+
7494
_client: function() {
7595
return require('romis').createClient(this.connection.port, this.connection.host, {
7696
auth_pass: this.connection.password

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ember-cli-deploy-redis-index-adapter",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"directories": {
55
"doc": "doc",
66
"test": "tests"

tests/helpers/mock-redis-client.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,34 @@ var CoreObject = require('core-object');
22
var CLIPromise = require('ember-cli/lib/ext/promise');
33

44
module.exports = CoreObject.extend({
5+
init: function() {
6+
this.store = {};
7+
},
8+
59
lpush: function(appId, key) {
6-
this.appId = appId;
7-
this.key = key;
8-
return CLIPromise.resolve();
10+
this.store[appId] = this.store[appId] || [];
11+
var length = this.store[appId].unshift(key);
12+
13+
return CLIPromise.resolve(length);
914
},
1015
lrange: function(appId, start, end) {
11-
this.appId = appId;
12-
this.start = start;
13-
this.end = end;
16+
var values = this.store[appId] || [];
1417

15-
var result = ['key', 'a', 'b', 'c', 'd'].slice(start, end + 1);
18+
var result = values.slice(start, end + 1);
1619

1720
return CLIPromise.resolve(result);
1821
},
1922
ltrim: function(appId, min, max) {
20-
this.appId = appId;
21-
this.min = min;
22-
this.max = max;
23-
return CLIPromise.resolve();
23+
var values = this.store[appId] || [];
24+
25+
this.store[appId] = values.slice(min, max + 1);
26+
return CLIPromise.resolve('OK');
2427
},
2528
set: function(key, value) {
26-
this.key = key;
27-
this.value = value;
28-
return CLIPromise.resolve();
29+
this.store[key] = value;
30+
return CLIPromise.resolve('OK');
31+
},
32+
get: function(key) {
33+
return this.store[key];
2934
}
3035
});

tests/unit/redis-index-adapter-nodetest.js

Lines changed: 47 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -78,164 +78,91 @@ describe('redis-index-adpater', function(){
7878
});
7979

8080
describe('#upload', function() {
81-
it('proceeds if index is uploaded and returns the key', function() {
82-
adapterOptions._uploadIfNotInVersionList = succeeded;
83-
adapterOptions._updateVersionList = succeeded;
84-
adapterOptions._trimVersionList = succeeded;
81+
it('rejects if the version is already uploaded', function() {
82+
mockClient.set('aaa', 'data');
83+
mockClient.lpush(adapterOptions.appId, 'aaa');
8584

8685
var subject = new Adapter(adapterOptions);
8786

88-
return subject.upload('data')
89-
.then(function(key) {
90-
assert.ok(/[0-9a-f]{10}/.test(key));
91-
}, function(error) {
92-
assert.ok(false, 'Should have resolved upload');
93-
});
94-
});
95-
96-
it('rejects if index is not uploaded', function() {
97-
adapterOptions._uploadIfNotInVersionList = failed;
98-
adapterOptions._updateVersionList = succeeded;
99-
adapterOptions._trimVersionList = succeeded;
100-
101-
var subject = new Adapter(adapterOptions);
87+
subject._key = function() {
88+
return 'aaa';
89+
};
10290

10391
return subject.upload('data')
10492
.then(function() {
105-
assert.ok(false, 'Should have rejected upload');
93+
assert.ok(false, 'Should have rejected due to version already being uploaded');
10694
}, function(error) {
107-
assert.equal(error, 'failed');
95+
assert.equal(error.message, 'Version for key [aaa] has already been uploaded\n');
10896
});
10997
});
11098

111-
it('rejects if version list is not updated', function() {
112-
adapterOptions._uploadIfNotInVersionList = succeeded;
113-
adapterOptions._updateVersionList = failed;
114-
adapterOptions._trimVersionList = succeeded;
115-
99+
it('maintains the version list to be at the specified number of versions', function() {
100+
for(var i = 0; i < 4; i++) {
101+
mockClient.lpush(adapterOptions.appId, 'version-' + i);
102+
}
116103
var subject = new Adapter(adapterOptions);
117104

118-
return subject.upload('data')
119-
.then(function() {
120-
assert.ok(false, 'Should have rejected upload');
121-
}, function(error) {
122-
assert.equal(error, 'failed');
123-
});
124-
});
125-
126-
it('rejects if version list is not trimmed', function() {
127-
adapterOptions._uploadIfNotInVersionList = succeeded;
128-
adapterOptions._updateVersionList = succeeded;
129-
adapterOptions._trimVersionList = failed;
105+
subject._key = function() {
106+
return 'aaa';
107+
};
130108

131-
var subject = new Adapter(adapterOptions);
109+
assert.equal(mockClient.get(adapterOptions.appId).indexOf('aaa'), -1);
132110

133111
return subject.upload('data')
134112
.then(function() {
135-
assert.ok(false, 'Should have rejected upload');
113+
assert.equal(mockClient.get(adapterOptions.appId).length, 4);
114+
assert.equal(mockClient.get(adapterOptions.appId)[0], 'aaa');
136115
}, function(error) {
137-
assert.equal(error, 'failed');
116+
assert.ok(false, 'Should have resolved');
138117
});
139118
});
140-
});
141119

142-
describe('#_key', function() {
143-
it('returns the current git hash', function() {
120+
it('returns the key for which the data was uploaded', function() {
144121
var subject = new Adapter(adapterOptions);
145122

146-
var sha = subject._key();
123+
subject._key = function() {
124+
return 'aaa';
125+
};
147126

148-
assert.ok(/[0-9a-f]{10}/.test(sha), 'Should return hash');
149-
});
150-
});
151-
152-
describe('#_uploadIfNotInVersionList', function() {
153-
it('resolves on a successful upload', function() {
154-
var subject = new Adapter(adapterOptions);
155-
156-
return subject._uploadIfNotInVersionList('new-key', 'value')
157-
.then(function() {
158-
assert.equal(mockClient.key, 'my-app:new-key');
159-
assert.equal(mockClient.value, 'value');
127+
return subject.upload('data')
128+
.then(function(key) {
129+
assert.equal(key, 'aaa');
160130
}, function() {
161-
assert.ok(false, 'Should have uploaded successfully');
162-
});
163-
});
164-
165-
it('rejects if a version already exists for the current git sha', function() {
166-
var subject = new Adapter(adapterOptions);
167-
168-
return subject._uploadIfNotInVersionList('key', 'value')
169-
.then(function() {
170-
assert.ok(false, 'Should have rejected due to version already being uploaded');
171-
}, function(error) {
172-
assert.equal(error.message, 'Version for key [key] has already been uploaded\n');
131+
assert.ok(false, 'Should have resolved with upload key');
173132
});
174133
});
175134
});
176135

177-
describe('#_updateVersionList', function() {
178-
it('resolves on a successful update', function() {
136+
describe('#setCurrent', function() {
137+
it('rejects if the version has not been previously uploaded', function() {
179138
var subject = new Adapter(adapterOptions);
180139

181-
return subject._updateVersionList('new-key')
140+
return subject.setCurrent('aaa')
182141
.then(function() {
183-
assert.equal(mockClient.appId, 'my-app');
184-
assert.equal(mockClient.key, 'new-key');
185-
}, function() {
186-
assert.ok(false, 'Should have updated versions successfully');
187-
});
188-
});
189-
190-
it('rejects if a version already exists for the current git sha', function() {
191-
var subject = new Adapter(adapterOptions);
192-
193-
return subject._updateVersionList('key')
194-
.then(function() {
195-
assert.ok(false, 'Should have rejected due to version already being in version list');
142+
assert.ok(false, 'Should have rejected');
196143
}, function(error) {
197-
assert.equal(error.message, 'Version for key [key] has already been uploaded\n');
198-
});
199-
});
200-
});
201-
202-
describe('#_trimVersions', function() {
203-
it('resolves on a successful call', function() {
204-
adapterOptions.versionCount = 5;
205-
206-
var subject = new Adapter(adapterOptions);
207-
208-
return subject._trimVersionList()
209-
.then(function() {
210-
assert.equal(mockClient.appId, 'my-app');
211-
assert.equal(mockClient.min, 0);
212-
assert.equal(mockClient.max, 4);
213-
}, function() {
214-
assert.ok(false, 'Should have trimmed the version list successfully');
144+
assert.equal(error.message, 'Version for key [aaa] does not exist\n');
145+
var currentKey = mockClient.get(adapterOptions.appId + ':current');
146+
assert.equal(currentKey, null);
215147
});
216148
});
217-
});
218149

219-
describe('#_listVersions', function() {
220-
it('returns the number of versions specified', function() {
150+
it('sets the specified version as the current version', function() {
221151
var subject = new Adapter(adapterOptions);
222152

223-
return subject._listVersions(3)
224-
.then(function(result) {
225-
assert.equal(result.length, 3);
226-
}, function() {
227-
assert.ok(false, 'Should have returned specified number of versions');
228-
});
229-
});
230-
231-
it('returns the default number of versions when count not specified', function() {
232-
var subject = new Adapter(adapterOptions);
153+
subject._key = function() {
154+
return 'aaa';
155+
};
233156

234-
return subject._listVersions()
235-
.then(function(result) {
236-
assert.equal(result.length, 4);
237-
}, function() {
238-
assert.ok(false, 'Should have returned specified number of versions');
157+
return subject.upload('data')
158+
.then(function(key) {
159+
return subject.setCurrent(key)
160+
.then(function() {
161+
var currentKey = mockClient.get(adapterOptions.appId + ':current');
162+
assert.equal(currentKey, key);
163+
}, function() {
164+
assert.ok(false, 'Should have resolved');
165+
});
239166
});
240167
});
241168
});

0 commit comments

Comments
 (0)