diff --git a/README.md b/README.md index 72784607..21c78dba 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,19 @@ a callback function. These will still work for now for backward compatibility, v0.10.36 is still supported, and can be targeted by changing the `AWS_RUNTIME` value to `nodejs` in the `.env` file. ## Post install script -When running `node-lambda deploy` if you need to do some action after `npm install --production` and before deploying to AWS Lambda (i.e. replace some modules with precompiled ones or download some libraries) you can create `post_install.sh` script. If the file exists the script will be executed (and output shown after execution) if not it is skipped. Make sure that the script is executable. +When running `node-lambda deploy` if you need to do some action after `npm install --production` and before deploying to AWS Lambda (e.g. replace some modules with precompiled ones or download some libraries, replace some config file depending on environment) you can create `post_install.sh` script. If the file exists the script will be executed (and output shown after execution) if not it is skipped. Environment string is passed to script as first parameter so you can use it if needed. Make sure that the script is executable. + +Example `post_install.sh`: +``` +printf "\n\n###### Post install script ###### \n" +ENV="production"; +if [ ! -z $1 ] + then + ENV=$1; +fi +cp -v "config_$ENV.js" "config.js" \ +&& printf "###### DONE! ###### \n\n" +``` ## Prebuilt packages The `--prebuiltDirectory` flag is useful for working with Webpack for example. It skips `npm install --production` and `post_install.sh` and simply packages the specified directory. diff --git a/lib/main.js b/lib/main.js index 288e5bcd..15fd094f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -184,18 +184,18 @@ Lambda.prototype._npmInstall = function (program, codeDirectory, callback) { }); }; -Lambda.prototype._postInstallScript = function (codeDirectory, callback) { +Lambda.prototype._postInstallScript = function (program, codeDirectory, callback) { var script_filename = 'post_install.sh'; - var cmd = './' + script_filename; + var cmd = './' + script_filename + ' ' + program.environment; var filePath = [codeDirectory, script_filename].join('/'); fs.exists(filePath, function (exists) { if (exists) { - console.log('=> Running post install script '+script_filename); - exec(cmd, { cwd: codeDirectory,maxBuffer: 50 * 1024 * 1024 }, function (error, stdout, stderr) { + console.log('=> Running post install script ' + script_filename); + exec(cmd, { cwd: codeDirectory, maxBuffer: 50 * 1024 * 1024 }, function (error, stdout, stderr) { if (error) { - callback(error + " stdout: " + stdout + "stderr" + stderr); + callback(error + " stdout: " + stdout + " stderr: " + stderr); } else { console.log("\t\t" + stdout); callback(null); @@ -403,7 +403,7 @@ Lambda.prototype._buildAndArchive = function (program, archive_callback) { return archive_callback(err); } - _this._postInstallScript(codeDirectory, function (err) { + _this._postInstallScript(program, codeDirectory, function (err) { if (err) { return archive_callback(err); } diff --git a/test/main.js b/test/main.js index 1e471097..396cf3fa 100644 --- a/test/main.js +++ b/test/main.js @@ -196,6 +196,55 @@ describe('node-lambda', function () { }); }); + describe('_postInstallScript', function () { + var hook; + /** + * Capture console output + */ + function captureStream(stream){ + var oldWrite = stream.write; + var buf = ''; + stream.write = function(chunk, encoding, callback){ + buf += chunk.toString(); // chunk is a String or Buffer + oldWrite.apply(stream, arguments); + } + + return { + unhook: function unhook(){ + stream.write = oldWrite; + }, + captured: function(){ + return buf; + } + }; + } + beforeEach(function(){ + hook = captureStream(process.stdout); + }); + afterEach(function(){ + hook.unhook(); + }); + + + it('should not throw any errors if no script', function (done) { + lambda._postInstallScript(program, codeDirectory, function (err) { + assert.equal(err, null); + done(); + }); + }); + + it('running script gives expected output', function (done) { + fs.writeFileSync(codeDirectory + '/post_install.sh', fs.readFileSync('test/post_install.sh')); + fs.chmodSync(codeDirectory + '/post_install.sh', '755'); + lambda._postInstallScript(program, codeDirectory, function (err) { + assert.equal(err, null); + assert.equal("=> Running post install script post_install.sh\n\t\tYour environment is "+program.environment+"\n", hook.captured()); + fs.unlinkSync(codeDirectory + '/post_install.sh'); + done(); + }); + }); + }); + describe('_zip', function () { beforeEach(function (done) { this.timeout(30000); // give it time to build the node modules diff --git a/test/post_install.sh b/test/post_install.sh new file mode 100755 index 00000000..3005785b --- /dev/null +++ b/test/post_install.sh @@ -0,0 +1,2 @@ +#!/bin/bash +printf "Your environment is $1"