Created
December 22, 2015 03:32
-
-
Save tomdale/09a54aecfcaca5b65b9f to your computer and use it in GitHub Desktop.
Script for deploying zip of FastBoot build to S3
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
#!/usr/bin/env node | |
var AWS = require('aws-sdk'); | |
var RSVP = require('rsvp'); | |
var exec = RSVP.denodeify(require('child_process').exec); | |
var fs = require('fs-promise'); | |
var chalk = require('chalk'); | |
var crypto = require('crypto'); | |
var s3 = new AWS.S3({ | |
params: { | |
Bucket: process.env.FASTBOOT_S3_BUCKET | |
} | |
}); | |
performBrowserBuild() | |
.then(performFastBootBuild) | |
.then(copyBrowserPathsToFastBoot) | |
.then(createZip) | |
.then(fingerprintZip) | |
.then(function(zip) { | |
return upload(zip); | |
}) | |
.then(function(data) { | |
log("Uploaded to S3"); | |
log(data.Location); | |
log("\n\nDone."); | |
}) | |
.catch(function(e) { | |
console.log(chalk.red(e.stack)); | |
}); | |
function performBrowserBuild() { | |
log("Deploying browser build to CloudFront"); | |
return exec("ember build -prod") | |
.then(function() { | |
return exec("ember deploy production"); | |
}); | |
} | |
function performFastBootBuild() { | |
log("Building for FastBoot"); | |
return exec("ember fastboot:build -prod"); | |
} | |
function copyBrowserPathsToFastBoot() { | |
log("Rewriting FastBoot index.html assets"); | |
var browserAssetMap = JSON.parse(fs.readFileSync('dist/assets/assetMap.json')); | |
var fastBootAssetMap = JSON.parse(fs.readFileSync('fastboot-dist/assets/assetMap.json')); | |
var prepend = browserAssetMap.prepend; | |
var indexHTML = fs.readFileSync('fastboot-dist/index.html').toString(); | |
var newAssets = browserAssetMap.assets; | |
var oldAssets = fastBootAssetMap.assets; | |
for (var key in oldAssets) { | |
var value = oldAssets[key]; | |
indexHTML = indexHTML.replace(prepend + value, prepend + newAssets[key]); | |
} | |
fs.writeFileSync('fastboot-dist/index.html', indexHTML); | |
} | |
function createZip() { | |
return exec("zip -r fastboot-dist.zip fastboot-dist"); | |
} | |
function fingerprintZip() { | |
var zipBuf = fs.readFileSync('fastboot-dist.zip'); | |
var hash = md5Hash(zipBuf); | |
var hashedZip = 'fastboot-dist-' + hash + '.zip'; | |
return fs.rename('fastboot-dist.zip', hashedZip) | |
.then(function() { | |
log("Created " + hashedZip); | |
return hashedZip; | |
}); | |
} | |
function md5Hash(buf) { | |
var md5 = crypto.createHash('md5'); | |
md5.update(buf); | |
return md5.digest('hex'); | |
} | |
function upload(file) { | |
return new RSVP.Promise(function(resolve, reject) { | |
var params = { Key: 'fastboot/' + file }; | |
params.Body = fs.createReadStream(file); | |
s3.upload(params, function(err, data) { | |
if (err) { | |
reject(err); | |
} | |
resolve(data); | |
}); | |
}); | |
} | |
function log() { | |
var args = Array.prototype.map.call(arguments, function(val) { | |
return typeof val === 'string' ? chalk.green(val) : val; | |
}); | |
console.log.apply(console, args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment