├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Pavan Kumar Sunkara 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-cloudfront-invalidate 2 | A gulp plugin that allows you to invalidate paths in AWS cloudfront 3 | 4 | ## Installation 5 | ``` 6 | npm install gulp-cloudfront-invalidate 7 | ``` 8 | 9 | ## Usage 10 | 11 | ```js 12 | var gulp = require('gulp') 13 | , cloudfront = require('gulp-cloudfront-invalidate'); 14 | 15 | var settings = { 16 | distribution: 'E2A654H2YRPD0W', // Cloudfront distribution ID 17 | paths: ['/index.html'] // Paths to invalidate 18 | accessKeyId: '...', // AWS Access Key ID 19 | secretAccessKey: '...', // AWS Secret Access Key 20 | sessionToken: '...', // Optional AWS Session Token 21 | wait: true // Whether to wait until invalidation is completed (default: false) 22 | } 23 | 24 | gulp.task('invalidate', function () { 25 | return gulp.src('*') 26 | .pipe(cloudfront(settings)); 27 | }); 28 | ``` 29 | 30 | 31 | If you like this project, please watch this and follow me. 32 | 33 | ## Contributors 34 | Here is a list of [Contributors](http://github.com/jburgner/gulp-cloudfront-invalidate/contributors) 35 | 36 | __I accept pull requests and guarantee a reply back within a day__ 37 | 38 | ## License 39 | MIT/X11 40 | 41 | ## Bug Reports 42 | Report [here](http://github.com/jburgner/gulp-cloudfront-invalidate/issues). __Guaranteed reply within a day__. 43 | 44 | ## Contact 45 | Josh Burgner (jburgner@gmail.com) 46 | 47 | Follow me on [github](https://github.com/users/follow?target=jburgner) 48 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var log = require('fancy-log') 2 | , PluginError = require('plugin-error') 3 | , through = require('through2') 4 | , aws = require('aws-sdk'); 5 | 6 | module.exports = function (options) { 7 | options.wait = (options.wait === undefined || !options.wait) ? false : true; 8 | 9 | var cloudfront = new aws.CloudFront(); 10 | 11 | if ('credentials' in options) { 12 | cloudfront.config.update({ 13 | credentials: options.credentials 14 | }); 15 | } 16 | else { 17 | cloudfront.config.update({ 18 | accessKeyId: options.accessKeyId || process.env.AWS_ACCESS_KEY_ID, 19 | secretAccessKey: options.secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY, 20 | sessionToken: options.sessionToken || process.env.AWS_SESSION_TOKEN 21 | }); 22 | } 23 | 24 | var complain = function (err, msg, callback) { 25 | return callback(new PluginError('gulp-cloudfront-invalidate', msg + ': ' + err)); 26 | }; 27 | 28 | var check = function (id, callback) { 29 | cloudfront.getInvalidation({ 30 | DistributionId: options.distribution, 31 | Id: id 32 | }, function (err, res) { 33 | if (err) return complain(err, 'Could not check on invalidation', callback); 34 | 35 | if (res.Invalidation.Status === 'Completed') { 36 | return callback(); 37 | } else { 38 | setTimeout(function () { 39 | check(id, callback); 40 | }, 1000); 41 | } 42 | }) 43 | }; 44 | 45 | var work = function (callback) { 46 | cloudfront.createInvalidation({ 47 | DistributionId: options.distribution, 48 | InvalidationBatch: { 49 | CallerReference: Date.now().toString(), 50 | Paths: { 51 | Quantity: options.paths.length, 52 | Items: options.paths 53 | } 54 | } 55 | }, function (err, res) { 56 | if (err) return complain(err, 'Could not invalidate cloudfront', callback); 57 | 58 | log('Cloudfront invalidation created: ' + res.Invalidation.Id); 59 | 60 | if (!options.wait) { 61 | return callback(); 62 | } 63 | 64 | check(res.Invalidation.Id, callback); 65 | }); 66 | }; 67 | 68 | return through.obj(function (file, enc, callback) { 69 | callback(); 70 | }, work); 71 | }; 72 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-cloudfront-invalidate", 3 | "version": "0.1.6", 4 | "author": "Pavan Kumar Sunkara (http://pksunkara.github.com)", 5 | "description": "A gulp plugin that allows you to invalidate paths in AWS cloudfront", 6 | "homepage": "https://github.com/confyio/gulp-cloudfront-invalidate", 7 | "main": "index.js", 8 | "repository": "confyio/gulp-cloudfront-invalidate", 9 | "keywords": [ 10 | "gulp", 11 | "cloudfront", 12 | "invalidate", 13 | "aws", 14 | "invalidation" 15 | ], 16 | "dependencies": { 17 | "aws-sdk": "2.814.x", 18 | "fancy-log": "1.3.x", 19 | "plugin-error": "1.0.x", 20 | "through2": "2.0.x" 21 | }, 22 | "bugs": { 23 | "url": "https://github.com/confyio/gulp-cloudfront-invalidate/issues" 24 | }, 25 | "licenses": [ 26 | { 27 | "type": "MIT", 28 | "url": "https://raw.githubusercontent.com/confyio/gulp-cloudfront-invalidate/master/LICENSE" 29 | } 30 | ] 31 | } 32 | --------------------------------------------------------------------------------