Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #29 from Financial-Times/hash
Browse files Browse the repository at this point in the history
Allow apps to opt into hashing in a single command — eliminating gulp
  • Loading branch information
matthew-andrews committed Mar 20, 2015
2 parents a085a0f + b7ed05c commit 1829cc1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
41 changes: 28 additions & 13 deletions tasks/deploy-hashed-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
};
});
Expand All @@ -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',
Expand All @@ -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));
});
};

0 comments on commit 1829cc1

Please sign in to comment.