Skip to content

Commit f288476

Browse files
author
Aaron Chambers
committed
Merge pull request #11 from LevelbossMike/master
Add `allowOverwrite` option
2 parents 8025b46 + aeeb4c5 commit f288476

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ The unique revision number for the version of the file being uploaded to S3. The
8888

8989
*Default:* `context.commandLineArgs.revisionKey || context.revisionKey`
9090

91+
### allowOverwrite
92+
93+
A flag to specify whether the revision should be overwritten if it already exists in S3.
94+
95+
*Default:* `false`
96+
9197
### s3Client
9298

9399
The underlying S3 library used to upload the files to S3. This allows the user to use the default upload client provided by this plugin but switch out the underlying library that is used to actually send the files.

index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ module.exports = {
2525
},
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
28-
}
28+
},
29+
allowOverwrite: false
2930
},
3031
requiredConfig: ['accessKeyId', 'secretAccessKey', 'bucket'],
3132

3233
upload: function(context) {
33-
var bucket = this.readConfig('bucket');
34-
var prefix = this.readConfig('prefix');
35-
var revisionKey = this.readConfig('revisionKey');
36-
var distDir = this.readConfig('distDir');
37-
var filePattern = this.readConfig('filePattern');
34+
var bucket = this.readConfig('bucket');
35+
var prefix = this.readConfig('prefix');
36+
var revisionKey = this.readConfig('revisionKey');
37+
var distDir = this.readConfig('distDir');
38+
var filePattern = this.readConfig('filePattern');
39+
var allowOverwrite = this.readConfig('allowOverwrite');
3840
var filePath = path.join(distDir, filePattern);
3941

4042
var options = {
@@ -43,6 +45,7 @@ module.exports = {
4345
filePattern: filePattern,
4446
filePath: filePath,
4547
revisionKey: revisionKey,
48+
allowOverwrite: allowOverwrite
4649
};
4750

4851
this.log('preparing to upload revision to S3 bucket `' + bucket + '`');

lib/s3.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ module.exports = CoreObject.extend({
3131
},
3232

3333
upload: function(options) {
34-
var client = this._client;
35-
var plugin = this._plugin;
36-
var bucket = options.bucket;
37-
var acl = options.acl || 'public-read';
38-
var key = path.join(options.prefix, options.filePattern + ":" + options.revisionKey);
39-
var putObject = Promise.denodeify(client.putObject.bind(client));
34+
var client = this._client;
35+
var plugin = this._plugin;
36+
var bucket = options.bucket;
37+
var acl = options.acl || 'public-read';
38+
var allowOverwrite = options.allowOverwrite;
39+
var key = path.join(options.prefix, options.filePattern + ":" + options.revisionKey);
40+
var putObject = Promise.denodeify(client.putObject.bind(client));
4041

4142
var params = {
4243
Bucket: bucket,
@@ -46,11 +47,20 @@ module.exports = CoreObject.extend({
4647
CacheControl: 'max-age=0, no-cache'
4748
}
4849

49-
return readFile(options.filePath).then(function(fileContents) {
50-
params.Body = fileContents;
51-
return putObject(params).then(function() {
52-
plugin.log('✔ ' + key);
50+
return this.fetchRevisions(options)
51+
.then(function(revisions) {
52+
var found = revisions.map(function(element) { return element.revision; }).indexOf(options.revisionKey);
53+
if (found >= 0 && !allowOverwrite) {
54+
return Promise.reject("REVISION ALREADY UPLOADED! (set `allowOverwrite: true` if you want to support overwriting revisions)")
55+
}
56+
return Promise.resolve();
5357
})
58+
.then(readFile.bind(this, options.filePath))
59+
.then(function(fileContents) {
60+
params.Body = fileContents;
61+
return putObject(params).then(function() {
62+
plugin.log('✔ ' + key);
63+
});
5464
});
5565
},
5666

tests/unit/lib/s3-nodetest.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ describe('s3', function() {
7474
prefix: '',
7575
filePattern: filePattern,
7676
revisionKey: revisionKey,
77-
filePath: 'tests/unit/fixtures/test.html'
77+
filePath: 'tests/unit/fixtures/test.html',
78+
allowOverwrite: false
7879
};
7980

8081
s3Client.putObject = function(params, cb) {
@@ -152,6 +153,26 @@ describe('s3', function() {
152153
assert.equal(s3Params.ACL, acl, 'acl passed correctly');
153154
});
154155
});
156+
157+
describe("when revisionKey was already uploaded", function() {
158+
beforeEach(function() {
159+
options.revisionKey = "123";
160+
});
161+
162+
it('rejects when trying to upload an already uploaded revision', function() {
163+
var promise = subject.upload(options);
164+
165+
return assert.isRejected(promise);
166+
});
167+
168+
it('does not reject when allowOverwrite option is set to true', function() {
169+
options.allowOverwrite = true;
170+
171+
var promise = subject.upload(options);
172+
173+
return assert.isFulfilled(promise);
174+
});
175+
});
155176
});
156177

157178
describe('#fetchRevisions', function() {

0 commit comments

Comments
 (0)