Last active
August 29, 2015 13:58
-
-
Save alvincrespo/10335555 to your computer and use it in GitHub Desktop.
An S3 deploy script for ember-cli
This file contains hidden or 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
/** | |
* Example: node deploy.js | |
* | |
* Getting Started | |
* Change BUCKET_NAME | |
* Create AwsConfig.json in same directory as deploy.js | |
* | |
* References: Ken Tabor (NodeJS Deploying Files to AWS S3)[http://blog.katworksgames.com/2014/01/26/nodejs-deploying-files-to-aws-s3/] | |
*/ | |
var aws = require('aws-sdk'); | |
aws.config.loadFromPath('./AwsConfig.json'); | |
var s3 = new aws.S3(); | |
var fs = require('fs'); | |
var BUCKET_NAME = ''; | |
function deployDist() { | |
var dir = 'dist/'; | |
var filelist = getFileList(dir); | |
filelist.forEach(function(entry){ | |
// Removes the dist directory from the filename | |
// e.g. dist/assets/app.css => assets/app.css | |
var remotefilename = entry.replace(/^dist\//, ''); | |
var filename = './' + entry; | |
upload(remotefilename, filename); | |
}); | |
} | |
function upload(remotefilename, filename) { | |
var fileBuffer = fs.readFileSync(filename); | |
var metaData = getContentType(filename); | |
var params = { | |
ACL: 'public-read', | |
Bucket: BUCKET_NAME, | |
Key: remotefilename, | |
Body: fileBuffer, | |
ContentType: metaData | |
}; | |
function callback(error, response){ | |
if (error) { | |
console.log("FIRE"); | |
console.log(error); | |
} else { | |
console.log('Uploaded file[' + filename + '] to [' + remotefilename + '] as [' + metaData + ']'); | |
} | |
} | |
// Call S3 and Push Objects | |
s3.putObject(params, callback); | |
} | |
function getFileList(path) { | |
var i, fileInfo, filesFound; | |
var fileList = []; | |
filesFound = fs.readdirSync(path); | |
for (i = 0; i < filesFound.length; i++) { | |
fileInfo = fs.lstatSync(path + filesFound[i]); | |
if (fileInfo.isFile()) { | |
// Checks to ensure that we do not push files that start with a dot | |
// e.g. .gitkeep | |
if (!filesFound[i].match(/^\.\w*/)) { | |
fileList.push(path + filesFound[i]); | |
} | |
} | |
if(fileInfo.isDirectory()){ | |
if (!filesFound[i].match(/^\.\w*/)) { | |
var foundDirs = getFileList(path + filesFound[i] + "/"); | |
fileList = fileList.concat(foundDirs); | |
} | |
} | |
} | |
return fileList; | |
} | |
function getContentType(filename) { | |
var rc, fileType; | |
filename = filename.toLowerCase(); | |
fileType = filename.match(/\.([^\.]+)$/)[1]; | |
switch (fileType){ | |
case 'html': | |
rc = 'text/html'; | |
break; | |
case 'css': | |
rc = 'text/css'; | |
break; | |
case 'json': | |
rc = 'application/json'; | |
break; | |
case 'js': | |
rc = 'application/javascript'; | |
break; | |
case 'png': | |
rc = 'image/png'; | |
break; | |
case 'jpg': | |
rc = 'image/jpg'; | |
break; | |
default: | |
rc = 'application/octet-stream'; | |
break; | |
} | |
return rc; | |
} | |
deployDist(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment