Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ Lambda.prototype._setEnvironmentVars = function (program, codeDirectory) {
var configValues = fs.readFileSync(program.configFile);
var prefix = '////////////////////////////////////\n// "Environment Variables"\n';
var config = dotenv.parse(configValues);
var contentStr = contents.toString();

for (var k in config) {
if (!config.hasOwnProperty(k)) {
Expand All @@ -280,7 +281,14 @@ Lambda.prototype._setEnvironmentVars = function (program, codeDirectory) {
}
prefix += '////////////////////////////////////\n\n';

fs.writeFileSync(handlerFileName, prefix + contents.toString());
// If the first line of the file is 'use strict', append after
if (contentStr.trim().indexOf('use strict') === 1) {
contentStr = contentStr.replace(/([`'"]use strict[`'"][;]?)/, '$1\n' + prefix);
} else {
contentStr = prefix + contentStr;
}

fs.writeFileSync(handlerFileName, contentStr);
};

Lambda.prototype._uploadExisting = function(lambda, params, cb) {
Expand Down
27 changes: 27 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,31 @@ describe('node-lambda', function () {
});

});

describe('environment variable injection - "use strict" allowance', function () {
beforeEach(function () {
// Prep...
fs.writeFileSync('tmp.env', 'FOO=bar\nBAZ=bing\n');
fs.writeFileSync('test.js', '\'use strict\';');
});

afterEach(function () {
fs.unlinkSync('tmp.env');
fs.unlinkSync('test.js');
});

it('should inject environment variables at the top of the entry point file', function () {

// Run it...
lambda._setEnvironmentVars({
configFile: 'tmp.env',
handler: 'test.handler',
}, process.cwd());

assert.equal(fs.readFileSync('test.js').toString(), '\'use strict\';\n////////////////////////////////////\n' +
'// "Environment Variables"\nprocess.env["FOO"]="bar";\n' +
'process.env["BAZ"]="bing";\n////////////////////////////////////\n\n');
});

});
});