Skip to content

Commit eb4dc20

Browse files
committed
Merge pull request #47 from ember-cli-deploy/add-fetch-initial-revisions
add fetchInitialRevisions
2 parents 410f616 + 2aedc0a commit eb4dc20

File tree

2 files changed

+166
-8
lines changed

2 files changed

+166
-8
lines changed

index.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ module.exports = {
2626
s3Client: function(context) {
2727
return context.s3Client; // if you want to provide your own S3 client to be used instead of one from aws-sdk
2828
},
29+
s3DeployClient: function(/* context */) {
30+
return new S3({ plugin: this });
31+
},
2932
allowOverwrite: false
3033
},
34+
3135
requiredConfig: ['bucket', 'region'],
3236

3337
upload: function(context) {
@@ -52,7 +56,7 @@ module.exports = {
5256

5357
this.log('preparing to upload revision to S3 bucket `' + bucket + '`', { verbose: true });
5458

55-
var s3 = new S3({ plugin: this });
59+
var s3 = this.readConfig('s3DeployClient');
5660
return s3.upload(options);
5761
},
5862

@@ -73,11 +77,29 @@ module.exports = {
7377

7478
this.log('preparing to activate `' + revisionKey + '`', { verbose: true });
7579

76-
var s3 = new S3({ plugin: this });
80+
var s3 = this.readConfig('s3DeployClient');
7781
return s3.activate(options);
7882
},
7983

8084
fetchRevisions: function(context) {
85+
return this._list(context)
86+
.then(function(revisions) {
87+
return {
88+
revisions: revisions
89+
};
90+
});
91+
},
92+
93+
fetchInitialRevisions: function(context) {
94+
return this._list(context)
95+
.then(function(revisions) {
96+
return {
97+
initialRevisions: revisions
98+
};
99+
});
100+
},
101+
102+
_list: function(/* context */) {
81103
var bucket = this.readConfig('bucket');
82104
var prefix = this.readConfig('prefix');
83105
var filePattern = this.readConfig('filePattern');
@@ -88,12 +110,9 @@ module.exports = {
88110
filePattern: filePattern,
89111
};
90112

91-
var s3 = new S3({ plugin: this });
92-
return s3.fetchRevisions(options)
93-
.then(function(revisions) {
94-
context.revisions = revisions;
95-
});
96-
},
113+
var s3 = this.readConfig('s3DeployClient');
114+
return s3.fetchRevisions(options);
115+
}
97116
});
98117

99118
return new DeployPlugin();

tests/unit/index-nodetest.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
'use strict';
2+
3+
var Promise = require('ember-cli/lib/ext/promise');
4+
var assert = require('ember-cli/tests/helpers/assert');
5+
6+
var stubProject = {
7+
name: function(){
8+
return 'my-project';
9+
}
10+
};
11+
12+
describe('s3-index plugin', function() {
13+
var subject, mockUi;
14+
15+
beforeEach(function() {
16+
subject = require('../../index');
17+
mockUi = {
18+
verbose: true,
19+
messages: [],
20+
write: function() { },
21+
writeLine: function(message) {
22+
this.messages.push(message);
23+
}
24+
};
25+
});
26+
27+
it('has a name', function() {
28+
var result = subject.createDeployPlugin({
29+
name: 'test-plugin'
30+
});
31+
32+
assert.equal(result.name, 'test-plugin');
33+
});
34+
35+
it('implements the correct hooks', function() {
36+
var plugin = subject.createDeployPlugin({
37+
name: 'test-plugin'
38+
});
39+
assert.ok(plugin.configure);
40+
assert.ok(plugin.upload);
41+
assert.ok(plugin.activate);
42+
assert.ok(plugin.fetchRevisions);
43+
assert.ok(plugin.fetchInitialRevisions);
44+
});
45+
46+
47+
describe('fetchInitialRevisions hook', function() {
48+
it('fills the initialRevisions variable on context', function() {
49+
var plugin;
50+
var context;
51+
52+
plugin = subject.createDeployPlugin({
53+
name: 's3-index'
54+
});
55+
56+
context = {
57+
ui: mockUi,
58+
project: stubProject,
59+
config: {
60+
's3-index': {
61+
prefix: 'test-prefix',
62+
filePattern: 'index.html',
63+
bucket: 'my-bucket',
64+
region: 'my-region',
65+
s3DeployClient: function(/* context */) {
66+
return {
67+
fetchRevisions: function(/* keyPrefix, revisionKey */) {
68+
return Promise.resolve([{
69+
revision: 'a',
70+
active: false
71+
}]);
72+
}
73+
};
74+
}
75+
}
76+
}
77+
};
78+
plugin.beforeHook(context);
79+
plugin.configure(context);
80+
81+
return assert.isFulfilled(plugin.fetchInitialRevisions(context))
82+
.then(function(result) {
83+
assert.deepEqual(result, {
84+
initialRevisions: [{
85+
"active": false,
86+
"revision": "a"
87+
}]
88+
});
89+
});
90+
});
91+
});
92+
93+
describe('fetchRevisions hook', function() {
94+
it('fills the revisions variable on context', function() {
95+
var plugin;
96+
var context;
97+
98+
plugin = subject.createDeployPlugin({
99+
name: 's3-index'
100+
});
101+
102+
context = {
103+
ui: mockUi,
104+
project: stubProject,
105+
106+
config: {
107+
's3-index': {
108+
prefix: 'test-prefix',
109+
filePattern: 'index.html',
110+
bucket: 'my-bucket',
111+
region: 'my-region',
112+
s3DeployClient: function(/* context */) {
113+
return {
114+
fetchRevisions: function(/* keyPrefix, revisionKey */) {
115+
return Promise.resolve([{
116+
revision: 'a',
117+
active: false
118+
}]);
119+
}
120+
};
121+
}
122+
}
123+
}
124+
};
125+
plugin.beforeHook(context);
126+
plugin.configure(context);
127+
128+
return assert.isFulfilled(plugin.fetchRevisions(context))
129+
.then(function(result) {
130+
assert.deepEqual(result, {
131+
revisions: [{
132+
"active": false,
133+
"revision": "a"
134+
}]
135+
});
136+
});
137+
});
138+
});
139+
});

0 commit comments

Comments
 (0)