Last active
August 29, 2015 14:11
-
-
Save supinf/63e44b9198860b9563bd to your computer and use it in GitHub Desktop.
AWS Lambda script:: Compiling JavaScript files which are triggered by s3 events.
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
var AWS = require('aws-sdk'); | |
var async = require('async'); | |
var path = require("path"); | |
var mkdir = require("mkdirp"); | |
var fs = require('fs'); | |
var spawn = require('child_process').spawn; | |
exports.handler = function (event, context) { | |
// console.log('Event: '+JSON.stringify(event, true, ' ')); | |
var key = event.Records[0].s3.object.key; | |
if (! /\.js$/.test(key)) { | |
context.done(null, ''); | |
} | |
if (/\.min\.js$/.test(key)) { | |
context.done(null, ''); | |
} | |
AWS.config.region = event.Records[0].awsRegion; | |
var s3 = new AWS.S3(), | |
bucket = event.Records[0].s3.bucket.name, | |
min = key.replace(/\.js$/, '.min.js'); | |
async.series([ | |
function (callback) { | |
_get(s3, bucket, key, function () { | |
callback(null, 'get'); | |
}); | |
}, | |
/** | |
* using google closure-compiler | |
*/ | |
// function (callback) { | |
// _get(bucket, 'compiler.jar', function () { | |
// callback(null, 'jar'); | |
// }); | |
// }, | |
// function (callback) { | |
// var command = 'java', | |
// args = ['-Xms6m', '-Xmx128m', '-jar', '/tmp/compiler.jar', | |
// '--compilation_level', 'SIMPLE_OPTIMIZATIONS', | |
// '--charset', 'UTF-8', '--js', '/tmp/'+key, | |
// '--js_output_file', '/tmp/'+min | |
// ]; | |
// _command(command, args, null, function () { | |
// callback(null, 'java'); | |
// }); | |
// }, | |
/** | |
* using yui compressor | |
*/ | |
function (callback) { | |
_get(s3, bucket, 'yuicompressor-2.4.8.jar', function () { | |
callback(null, 'jar'); | |
}); | |
}, | |
function (callback) { | |
var command = 'java', | |
args = ['-Xms6m', '-Xmx128m', '-jar', '/tmp/yuicompressor-2.4.8.jar', | |
'/tmp/'+key, '-o', '/tmp/'+min, '--charset', 'UTF-8' | |
]; | |
_command(command, args, null, function () { | |
callback(null, 'java'); | |
}); | |
}, | |
function (callback) { | |
_put(s3, bucket, min, function () { | |
callback(null, 'post'); | |
}); | |
} | |
], function (err) { | |
context.done(null, ''); | |
}); | |
}; | |
function _get(s3, bucket, key, callback) { | |
mkdir(path.dirname('/tmp/'+key), function (err) { | |
if (err) throw err; | |
var file = fs.createWriteStream('/tmp/'+key); | |
s3.getObject({Bucket: bucket, Key: key}).on('error', function (err) { | |
throw err; | |
}).createReadStream().pipe(file).on('close', function () { | |
callback(); | |
}); | |
}); | |
} | |
function _put(s3, bucket, key, callback) { | |
var stream = fs.createReadStream('/tmp/'+key); | |
s3.putObject({Bucket: bucket, Key: key, Body: stream}, function (err) { | |
if (err) throw err; | |
callback(); | |
}); | |
} | |
function _command(command, args, options, callback) { | |
var child = spawn(command, args, options); | |
child.stdout.on('data', function (data) { | |
console.log('stdout: ' + data); | |
}); | |
child.stderr.on('data', function (data) { | |
console.log('stderr: ' + data); | |
}); | |
child.on('close', function (code) { | |
if (code !== 0) { | |
throw new Error("Process exited with non-zero status code"); | |
} | |
callback(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment