diff --git a/package.json b/package.json index 1a70172d..c578c878 100644 --- a/package.json +++ b/package.json @@ -13,9 +13,10 @@ "debug": "^2.1.1", "denodeify": "^1.2.0", "es6-promise": "^2.0.1", + "fetchres": "^1.0.4", + "glob": "^5.0.3", "haikro": "^1.13.0", "heroku-client": "^1.9.1", - "fetchres": "^1.0.4", "isomorphic-fetch": "^2.0.0" }, "devDependencies": { diff --git a/tasks/deploy-hashed-assets.js b/tasks/deploy-hashed-assets.js index fec787c3..e285387e 100644 --- a/tasks/deploy-hashed-assets.js +++ b/tasks/deploy-hashed-assets.js @@ -3,9 +3,11 @@ var packageJson = require(process.cwd() + '/package.json'); var denodeify = require('denodeify'); var normalizeName = require('../lib/normalize-name'); -var fs = require('fs'); -var readdir = denodeify(fs.readdir); -var readFile = denodeify(fs.readFile); +var readFile = denodeify(require('fs').readFile); +var writeFile = denodeify(require('fs').writeFile); +var glob = denodeify(require('glob')); +var crypto = require('crypto'); +var basename = require('path').basename; module.exports = function(app) { var token = process.env.GITHUB_AUTH_TOKEN; @@ -16,15 +18,14 @@ module.exports = function(app) { 'Authorization': 'token ' + token }; var api = 'https://api.github.com/repos/Financial-Times/next-hashed-assets/contents/'; - var dir = process.cwd() + '/hashed-assets'; - return readdir(dir) + return glob(process.cwd() + '/public/*.@(css|js|map)') .then(function(files) { return Promise.all(files.map(function(file) { - return readFile(dir + '/' + file, { encoding: 'base64' }) + return readFile(file) .then(function(content) { return { - name: file, + name: basename(file), content: content }; }); @@ -33,13 +34,14 @@ module.exports = function(app) { .then(function(files) { return Promise.all(files.map(function(file) { // PUT /repos/:owner/:repo/contents/:path - return fetch(api + encodeURIComponent(app + '/' + file.name), { + var hash = crypto.createHash('sha1').update(file.content.toString('utf8')).digest('hex'); + var hashedName = file.name.replace(/(.*)(\.[a-z0-9])/i, '$1-' + hash.substring(0, 8) + '$2'); + return fetch(api + encodeURIComponent(app + '/' + hashedName), { method: 'PUT', headers: authorizedHeaders, body: JSON.stringify({ - path: 'test.txt', message: 'Create ' + file.name + ' for ' + app, - content: file.content, + content: file.content.toString('base64'), branch: 'gh-pages', committer: { name: 'Next Team', @@ -49,19 +51,32 @@ module.exports = function(app) { }) .then(function(response) { if (response.status === 201) { - console.log('Successfully pushed ' + file.name + ' to GitHub'); + console.log('Successfully pushed ' + file.name + ' to GitHub for app' + app); } else { return response.json() .then(function(err) { if (err.message === 'Invalid request.\n\n"sha" wasn\'t supplied.') { - console.log('Hashed file ' + file.name + ' already exists'); + console.log('Hashed file ' + app + '/' + hashedName + ' already exists'); } else { throw new Error(err.message); } }); } - + }) + .then(function() { + return { + name: file.name, + hashedName: hashedName + }; }); })); + }) + .then(function(hashes) { + var hashMap = hashes.reduce(function(obj, file) { + obj[file.name] = file.hashedName; + return obj; + }, {}); + console.log("Writing public/asset-hashes.json"); + return writeFile(process.cwd() + '/public/asset-hashes.json', JSON.stringify(hashMap, undefined, 2)); }); };