-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtool.js
More file actions
87 lines (67 loc) · 3.14 KB
/
Copy pathtool.js
File metadata and controls
87 lines (67 loc) · 3.14 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
});
if (options.pushstate === true) {
options.pushstate = [403, 404];
}
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);
var errors = data.DistributionConfig.CustomErrorResponses;
if (errors && errors.length && options.pushstate) {
for (var i = 0; i < errors.Quantity; i++) {
var error = errors.Items[i];
if (options.pushstate.indexOf(error.ErrorCode) >= 0) {
error.ResponsePagePath = '/' + data.DistributionConfig.DefaultRootObject;
}
}
}
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
};
};