Skip to content

Commit 1d6405a

Browse files
navihtotDeviaVir
authored andcommitted
Post install script environment (#154)
* passing environment to post install script * Readme update * post install script tests
1 parent 41d0de4 commit 1d6405a

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,19 @@ a callback function. These will still work for now for backward compatibility,
143143
v0.10.36 is still supported, and can be targeted by changing the `AWS_RUNTIME` value to `nodejs` in the `.env` file.
144144

145145
## Post install script
146-
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.
146+
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.
147+
148+
Example `post_install.sh`:
149+
```
150+
printf "\n\n###### Post install script ###### \n"
151+
ENV="production";
152+
if [ ! -z $1 ]
153+
then
154+
ENV=$1;
155+
fi
156+
cp -v "config_$ENV.js" "config.js" \
157+
&& printf "###### DONE! ###### \n\n"
158+
```
147159

148160
## Prebuilt packages
149161
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.

lib/main.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,18 @@ Lambda.prototype._npmInstall = function (program, codeDirectory, callback) {
184184
});
185185
};
186186

187-
Lambda.prototype._postInstallScript = function (codeDirectory, callback) {
187+
Lambda.prototype._postInstallScript = function (program, codeDirectory, callback) {
188188
var script_filename = 'post_install.sh';
189-
var cmd = './' + script_filename;
189+
var cmd = './' + script_filename + ' ' + program.environment;
190190

191191
var filePath = [codeDirectory, script_filename].join('/');
192192

193193
fs.exists(filePath, function (exists) {
194194
if (exists) {
195-
console.log('=> Running post install script '+script_filename);
196-
exec(cmd, { cwd: codeDirectory,maxBuffer: 50 * 1024 * 1024 }, function (error, stdout, stderr) {
195+
console.log('=> Running post install script ' + script_filename);
196+
exec(cmd, { cwd: codeDirectory, maxBuffer: 50 * 1024 * 1024 }, function (error, stdout, stderr) {
197197
if (error) {
198-
callback(error + " stdout: " + stdout + "stderr" + stderr);
198+
callback(error + " stdout: " + stdout + " stderr: " + stderr);
199199
} else {
200200
console.log("\t\t" + stdout);
201201
callback(null);
@@ -403,7 +403,7 @@ Lambda.prototype._buildAndArchive = function (program, archive_callback) {
403403
return archive_callback(err);
404404
}
405405

406-
_this._postInstallScript(codeDirectory, function (err) {
406+
_this._postInstallScript(program, codeDirectory, function (err) {
407407
if (err) {
408408
return archive_callback(err);
409409
}

test/main.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,55 @@ describe('node-lambda', function () {
196196
});
197197
});
198198

199+
describe('_postInstallScript', function () {
200+
var hook;
201+
/**
202+
* Capture console output
203+
*/
204+
function captureStream(stream){
205+
var oldWrite = stream.write;
206+
var buf = '';
207+
stream.write = function(chunk, encoding, callback){
208+
buf += chunk.toString(); // chunk is a String or Buffer
209+
oldWrite.apply(stream, arguments);
210+
}
211+
212+
return {
213+
unhook: function unhook(){
214+
stream.write = oldWrite;
215+
},
216+
captured: function(){
217+
return buf;
218+
}
219+
};
220+
}
221+
beforeEach(function(){
222+
hook = captureStream(process.stdout);
223+
});
224+
afterEach(function(){
225+
hook.unhook();
226+
});
227+
228+
229+
it('should not throw any errors if no script', function (done) {
230+
lambda._postInstallScript(program, codeDirectory, function (err) {
231+
assert.equal(err, null);
232+
done();
233+
});
234+
});
235+
236+
it('running script gives expected output', function (done) {
237+
fs.writeFileSync(codeDirectory + '/post_install.sh', fs.readFileSync('test/post_install.sh'));
238+
fs.chmodSync(codeDirectory + '/post_install.sh', '755');
239+
lambda._postInstallScript(program, codeDirectory, function (err) {
240+
assert.equal(err, null);
241+
assert.equal("=> Running post install script post_install.sh\n\t\tYour environment is "+program.environment+"\n", hook.captured());
242+
fs.unlinkSync(codeDirectory + '/post_install.sh');
243+
done();
244+
});
245+
});
246+
});
247+
199248
describe('_zip', function () {
200249
beforeEach(function (done) {
201250
this.timeout(30000); // give it time to build the node modules

test/post_install.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
printf "Your environment is $1"

0 commit comments

Comments
 (0)