diff --git a/.gitignore b/.gitignore index 2ccbe465..e3c36494 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /node_modules/ +.idea/ diff --git a/README.md b/README.md index 2a497496..48fed8a2 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ deploy-hashed-assets deploys hashed asset files to S3 (if AWS keys set correctly) build build javascript and css destroy [app] deletes the app from heroku + purge [options] purges the given url from the Fastly cache. Requires a FASTLY_KEY environment variable set to your fastly api key * Options: diff --git a/bin/next-build-tools.js b/bin/next-build-tools.js index 5846ff51..86aae4c5 100755 --- a/bin/next-build-tools.js +++ b/bin/next-build-tools.js @@ -13,6 +13,7 @@ var verify = require('../tasks/verify'); var build = require('../tasks/build'); var verifyLayoutDeps = require('../tasks/verify-layout-deps'); var destroy = require('../tasks/destroy'); +var purge = require('../tasks/purge'); var nightwatch = require('../tasks/nightwatch'); var downloadConfiguration = require('../tasks/download-configuration'); var deployHashedAssets = require('../tasks/deploy-hashed-assets'); @@ -129,6 +130,18 @@ program } }); + program + .command('purge [url') + .option('-s, --soft ', 'Perform a "Soft Purge (will invalidate the content rather than remove it"') + .description('Purges the given url. Requires a FASTLY_KEY environment variable') + .action(function(url, options){ + if(url){ + purge(url, options).catch(exit); + }else{ + exit('Please provide a url'); + } + }); + program .command('*') .description('') @@ -136,6 +149,8 @@ program exit("The command ‘" + app + "’ is not known"); }); + + program.parse(process.argv); if (!process.argv.slice(2).length) { diff --git a/tasks/purge.js b/tasks/purge.js new file mode 100644 index 00000000..58c7c0f4 --- /dev/null +++ b/tasks/purge.js @@ -0,0 +1,29 @@ +'use strict'; + +var denodeify = require('denodeify'); +var exec = denodeify(require('child_process').exec, function(err, stdout, stderr) { + console.log(stdout); + console.log(stderr); + return [err]; +}); + +var FASTLY_KEY = process.env.FASTLY_KEY; + +module.exports = function(url, opts){ + if(!FASTLY_KEY){ + throw new Error('No Fastly Key Found!'); + } + + var options = opts || {}; + var soft = options.soft || false; + var command = 'curl ' + + '-x 10.113.219.30:8080 ' + + '-s 1 ' + + '-i ' + + '--request PURGE ' + + '--header "Fastly-Key: ' + FASTLY_KEY + '" ' + + '--header "Content-Accept: application/json" ' + + (soft ? '--header "Fastly-Soft-Purge:1' : '') + + url; + return exec(command); +};