forked from jburgner/gulp-cloudfront-invalidate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (60 loc) · 1.92 KB
/
index.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
var log = require('fancy-log')
, PluginError = require('plugin-error')
, through = require('through2')
, aws = require('aws-sdk');
module.exports = function (options) {
options.wait = (options.wait === undefined || !options.wait) ? false : true;
var cloudfront = new aws.CloudFront();
if ('credentials' in options) {
cloudfront.config.update({
credentials: options.credentials
});
}
else {
cloudfront.config.update({
accessKeyId: options.accessKeyId || process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: options.secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: options.sessionToken || process.env.AWS_SESSION_TOKEN
});
}
var complain = function (err, msg, callback) {
return callback(new PluginError('gulp-cloudfront-invalidate', msg + ': ' + err));
};
var check = function (id, callback) {
cloudfront.getInvalidation({
DistributionId: options.distribution,
Id: id
}, function (err, res) {
if (err) return complain(err, 'Could not check on invalidation', callback);
if (res.Invalidation.Status === 'Completed') {
return callback();
} else {
setTimeout(function () {
check(id, callback);
}, 1000);
}
})
};
var work = function (callback) {
cloudfront.createInvalidation({
DistributionId: options.distribution,
InvalidationBatch: {
CallerReference: Date.now().toString(),
Paths: {
Quantity: options.paths.length,
Items: options.paths
}
}
}, function (err, res) {
if (err) return complain(err, 'Could not invalidate cloudfront', callback);
log('Cloudfront invalidation created: ' + res.Invalidation.Id);
if (!options.wait) {
return callback();
}
check(res.Invalidation.Id, callback);
});
};
return through.obj(function (file, enc, callback) {
callback();
}, work);
};