-
Notifications
You must be signed in to change notification settings - Fork 25
/
tool.js
73 lines (55 loc) · 2.56 KB
/
tool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var gutil = require('gulp-util');
var AWS = require('aws-sdk');
var Q = require('q');
var gutil = require('gulp-util');
module.exports = function(options) {
var cloudfront = new AWS.CloudFront({
accessKeyId: options.key,
secretAccessKey: options.secret
});
var updateDefaultRootObject = function (defaultRootObject) {
var deferred = Q.defer();
// Get the existing distribution id
cloudfront.getDistribution({ Id: options.distributionId }, function(err, data) {
if (err) {
deferred.reject(err);
} else {
// AWS Service returns errors if we don't fix these
if (data.DistributionConfig.Comment === null) data.DistributionConfig.Comment = '';
if (data.DistributionConfig.Logging.Enabled === false) {
data.DistributionConfig.Logging.Bucket = '';
data.DistributionConfig.Logging.Prefix = '';
}
// Causing problems on a default cloudfront setup, why is this needed?
if (data.DistributionConfig.Origins.Items instanceof Array && data.DistributionConfig.Origins.Items[0].S3OriginConfig.OriginAccessIdentity === null) {
data.DistributionConfig.Origins.Items[0].S3OriginConfig.OriginAccessIdentity = '';
}
if (data.DistributionConfig.DefaultRootObject === defaultRootObject.substr(1)) {
gutil.log('gulp-cloudfront:', "DefaultRootObject hasn't changed, not updating.");
return deferred.resolve();
}
// Update the distribution with the new default root object (trim the precedeing slash)
data.DistributionConfig.DefaultRootObject = defaultRootObject.substr(1);
cloudfront.updateDistribution({
IfMatch: data.ETag,
Id: options.distributionId,
DistributionConfig: data.DistributionConfig
}, function(err, data) {
if (err) {
deferred.reject(err);
} else {
gutil.log('gulp-cloudfront:', 'DefaultRootObject updated to [' + defaultRootObject.substr(1) + '].');
deferred.resolve();
}
});
}
});
return deferred.promise;
};
return {
updateDefaultRootObject: updateDefaultRootObject
};
};