This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Financial-Times/assets-on-s3
Assets on s3
- Loading branch information
Showing
4 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
"use strict"; | ||
|
||
var packageJson = require(process.cwd() + '/package.json'); | ||
var denodeify = require('denodeify'); | ||
var normalizeName = require('../lib/normalize-name'); | ||
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; | ||
var aws = require('aws-sdk'); | ||
|
||
aws.config.update({ | ||
accessKeyId: process.env.aws_access_hashed_assets, | ||
secretAccessKey: process.env.aws_secret_hashed_assets, | ||
region: 'eu-west-1' | ||
}); | ||
|
||
function hashAndUpload(opts) { | ||
|
||
var file = opts.file; | ||
var app = opts.app; | ||
var bucket = 'ft-next-hashed-assets-prod'; | ||
var key = app + '/' + file.hashedName; | ||
|
||
return new Promise(function(resolve, reject) { | ||
var s3bucket = new aws.S3({params: {Bucket: bucket}}); | ||
var params = { | ||
Key: key, | ||
Body: file.content, | ||
ACL: 'public-read', | ||
CacheControl: 'public, max-age=604800000' | ||
}; | ||
s3bucket.upload(params, function(err, data) { | ||
if (err) { | ||
console.log("Error uploading data: ", err); | ||
reject(err); | ||
} else { | ||
resolve({ | ||
name: file.name, | ||
hashedName: file.hashedName | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = function(app) { | ||
if(!(process.env.aws_access && process.env.aws_secret)) { | ||
console.warn("Missing AWS keys. Hashed assets will not deploy to S3"); | ||
return Promise.resolve(); | ||
} | ||
|
||
app = app || normalizeName(packageJson.name, { version: false }); | ||
|
||
console.log('Deploying hashed assets to S3...'); | ||
return glob(process.cwd() + '/public/*.@(css|js|map)') | ||
.then(function(files) { | ||
return Promise.all(files.map(function(file) { | ||
return readFile(file) | ||
.then(function(content) { | ||
return { | ||
name: basename(file), | ||
content: content | ||
}; | ||
}); | ||
})); | ||
}) | ||
.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=/next-hashed-assets/' + 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) { | ||
return hashAndUpload({ app: app, file: file }) | ||
.then(function(file) { | ||
obj[file.name] = file.hashedName; | ||
return obj; | ||
}); | ||
}); | ||
}, Promise.resolve({})); | ||
return promise; | ||
}) | ||
.then(function(hashes) { | ||
console.log("Writing public/asset-hashes.json"); | ||
return writeFile(process.cwd() + '/public/asset-hashes.json', JSON.stringify(hashes, undefined, 2)); | ||
}); | ||
}; |