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 #31 from Financial-Times/source-map
Browse files Browse the repository at this point in the history
Fix js source maps
  • Loading branch information
matthew-andrews committed Mar 20, 2015
2 parents 18f2df7 + f211f5c commit 1b1dbd8
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions tasks/deploy-hashed-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ function hashAndUpload(opts) {
'Authorization': 'token ' + token
};
var api = 'https://api.github.com/repos/Financial-Times/next-hashed-assets/contents/';
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), {
return fetch(api + encodeURIComponent(app + '/' + file.hashedName), {
method: 'PUT',
headers: authorizedHeaders,
body: JSON.stringify({
Expand All @@ -36,12 +34,12 @@ function hashAndUpload(opts) {
})
.then(function(response) {
if (response.status === 201) {
console.log('Successfully pushed ' + file.name + ' to GitHub for app ' + app);
console.log('Successfully pushed ' + file.hashedName + ' 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 ' + app + '/' + hashedName + ' already exists');
console.log('Hashed file ' + app + '/' + file.hashedName + ' already exists');
} else {
throw new Error(err.message);
}
Expand All @@ -51,7 +49,7 @@ function hashAndUpload(opts) {
.then(function() {
return {
name: file.name,
hashedName: hashedName
hashedName: file.hashedName
};
});

Expand All @@ -75,6 +73,27 @@ module.exports = function(app) {
});
}));
})
.then(function(files) {
var mapHashName = '';
return files
.map(function(file) {
var hash = crypto.createHash('sha1').update(file.content.toString('utf8')).digest('hex');
file.hashedName = file.name.replace(/(.*)(\.[a-z0-9])/i, '$1-' + hash.substring(0, 8) + '$2');
if (file.name === 'main.js.map') {
mapHashName = file.hashedName;
}
return file;
})
.map(function(file) {
var content;
if (file.name === 'main.js') {
content = file.content.toString('utf8');
content = content.replace('/# sourceMappingURL=/' + app + '/' + file.name + '.map', '/# sourceMappingURL=/' + app + '/' + mapHashName);
file.content = new Buffer(content, 'utf8');
}
return file;
});
})
.then(function(files) {
var promise = files.reduce(function(promise, file) {
return promise.then(function(obj) {
Expand Down

0 comments on commit 1b1dbd8

Please sign in to comment.