diff --git a/index.js b/index.js index c849218c..6d2e1394 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ // For development/testing purposes -exports.handler = function(event, context, callback) { +exports.handler = function (event, context, callback) { console.log('Running index.handler'); console.log('=================================='); console.log('event', event); diff --git a/lib/main.js b/lib/main.js index 3f2a927f..5c4d32ce 100644 --- a/lib/main.js +++ b/lib/main.js @@ -87,7 +87,7 @@ Lambda.prototype._runHandler = function (handler, event, program, context) { }; if (['nodejs4.3', 'nodejs6.10'].indexOf(program.runtime) === -1) { - console.error("Runtime [" + program.runtime + "] is not supported."); + console.error(`Runtime [${program.runtime}] is not supported.`); process.exit(254); } handler(event, context, callback); @@ -159,7 +159,7 @@ Lambda.prototype._eventSourceList = function (program) { ScheduleEvents: null }; } - const list = (function() { + const list = (function () { try { return fs.readJsonSync(program.eventSourceFile); } catch(err) { @@ -317,9 +317,9 @@ Lambda.prototype._postInstallScript = function (program, codeDirectory, callback maxBuffer: maxBufferSize }, function (error, stdout, stderr) { if (error) { - return callback(error + " stdout: " + stdout + " stderr: " + stderr); + return callback(error + ' stdout: ' + stdout + ' stderr: ' + stderr); } - console.log("\t\t" + stdout); + console.log('\t\t' + stdout); callback(null); }); }; @@ -369,7 +369,7 @@ Lambda.prototype._setRunTimeEnvironmentVars = function (program) { var configValues = fs.readFileSync(program.configFile); var config = dotenv.parse(configValues); - for (var k in config) { + for (let k in config) { if (!config.hasOwnProperty(k)) { continue; } @@ -537,9 +537,9 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE } var updateEventSourceList = []; // Checking new and update event sources - for (var i in eventSourceList) { - var isExisting = false; - for (var j in existingEventSourceList) { + for (let i in eventSourceList) { + let isExisting = false; + for (let j in existingEventSourceList) { if (eventSourceList[i]['EventSourceArn'] === existingEventSourceList[j]['EventSourceArn']) { isExisting = true; updateEventSourceList.push({ @@ -567,9 +567,9 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE } // Checking delete event sources - for (var i in existingEventSourceList) { - var isExisting = false; - for (var j in eventSourceList) { + for (let i in existingEventSourceList) { + let isExisting = false; + for (let j in eventSourceList) { if (eventSourceList[j]['EventSourceArn'] === existingEventSourceList[i]['EventSourceArn']) { isExisting = true; break; @@ -606,7 +606,7 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE }); break; } - }, function(err, results) { + }, function (err, results) { return cb(err, results); }); }; @@ -615,8 +615,8 @@ Lambda.prototype._updateScheduleEvents = function (scheduleEvents, functionArn, if (scheduleList == null) { return cb(null, []); } - return async.series(scheduleList.map(function(schedule) { - return function(_cb) { + return async.series(scheduleList.map(function (schedule) { + return function (_cb) { const params = Object.assign(schedule, { FunctionArn: functionArn }); scheduleEvents.add(params).then(function (data) { _cb(null, params); @@ -624,7 +624,7 @@ Lambda.prototype._updateScheduleEvents = function (scheduleEvents, functionArn, _cb(err); }); }; - }), function(err, results) { + }), function (err, results) { cb(err, results); }); }; @@ -718,7 +718,7 @@ Lambda.prototype.deploy = function (program) { }, function (err) { if (err) { // Function does not exist - return _this._uploadNew(lambda, params, function(err, results) { + return _this._uploadNew(lambda, params, function (err, results) { if (err) { throw err; } @@ -726,18 +726,18 @@ Lambda.prototype.deploy = function (program) { console.log(results); async.parallel([ - function(_callback) { + function (_callback) { // Updating event source(s) - _this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function(err, results) { + _this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function (err, results) { _callback(null, results); }); }, - function(_callback) { - _this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function(err, results) { + function (_callback) { + _this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function (err, results) { _callback(err, results); }); } - ], function(err, results) { + ], function (err, results) { cb(err, results); }); }); @@ -746,29 +746,29 @@ Lambda.prototype.deploy = function (program) { // Function exists _this._listEventSourceMappings(lambda, { 'FunctionName': params.FunctionName - }, function(err, existingEventSourceList) { + }, function (err, existingEventSourceList) { if (err) { throw err; } return async.parallel([ - function(_callback) { - _this._uploadExisting(lambda, params, function(err, results) { + function (_callback) { + _this._uploadExisting(lambda, params, function (err, results) { if (err) { throw err; } console.log('=> Zip file(s) done uploading. Results follow: '); console.log(results); - _this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function(err, results) { + _this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function (err, results) { _callback(err, results); }); }); }, - function(_callback) { - _this._updateEventSources(lambda, params.FunctionName, existingEventSourceList, eventSourceList.EventSourceMappings, function(err, results) { + function (_callback) { + _this._updateEventSources(lambda, params.FunctionName, existingEventSourceList, eventSourceList.EventSourceMappings, function (err, results) { _callback(err, results); }); } - ], function(err, results) { + ], function (err, results) { cb(err, results); }); }); @@ -777,8 +777,8 @@ Lambda.prototype.deploy = function (program) { if (err) { throw err; } - const resultsIsEmpty = results.filter(function(result) { - return result.filter(function(res) { + const resultsIsEmpty = results.filter(function (result) { + return result.filter(function (res) { return res.length > 0; }).length > 0; }).length === 0; diff --git a/lib/schedule_events.js b/lib/schedule_events.js index b9edab89..835f3c9b 100644 --- a/lib/schedule_events.js +++ b/lib/schedule_events.js @@ -1,6 +1,6 @@ 'use strict'; -const ScheduleEvents = function(aws) { +const ScheduleEvents = function (aws) { // Authenticated `aws` object in `lib/main.js` this.lambda = new aws.Lambda({ apiVersion: '2015-03-31' @@ -23,7 +23,7 @@ ScheduleEvents.prototype = { return params.FunctionArn.split(':').pop(); }, - _putRulePrams: function(params) { + _putRulePrams: function (params) { return { Name: params.ScheduleName, Description: this._ruleDescription(params), @@ -32,7 +32,7 @@ ScheduleEvents.prototype = { }; }, - _putRule: function(params) { + _putRule: function (params) { const _this = this; // return RuleArn if created return new Promise((resolve) => { @@ -44,7 +44,7 @@ ScheduleEvents.prototype = { }); }, - _addPermissionParams: function(params) { + _addPermissionParams: function (params) { return { Action: 'lambda:InvokeFunction', FunctionName: this._functionName(params), @@ -54,7 +54,7 @@ ScheduleEvents.prototype = { }; }, - _addPermission: function(params) { + _addPermission: function (params) { const _this = this; return new Promise((resolve) => { const _params = _this._addPermissionParams(params); @@ -69,7 +69,7 @@ ScheduleEvents.prototype = { }); }, - _putTargetsParams: function(params) { + _putTargetsParams: function (params) { return { Rule: params.ScheduleName, Targets: [{ @@ -79,7 +79,7 @@ ScheduleEvents.prototype = { }; }, - _putTargets: function(params) { + _putTargets: function (params) { const _this = this; return new Promise((resolve) => { const _params = _this._putTargetsParams(params); @@ -91,7 +91,7 @@ ScheduleEvents.prototype = { }); }, - add: function(params) { + add: function (params) { const _this = this; return Promise.resolve().then(() => { return _this._putRule(params); diff --git a/test/main.js b/test/main.js index 6b94c43f..48be948f 100644 --- a/test/main.js +++ b/test/main.js @@ -35,7 +35,7 @@ var originalProgram = { var codeDirectory = lambda._codeDirectory(); -function _timeout(params) { +function _timeout (params) { // Even if timeout is set for the whole test for Windows, // if it is set in local it will be valid. // For Windows, do not set it with local. @@ -96,7 +96,7 @@ describe('lib/main', function () { assert.match(params.FunctionName, functionNamePattern); }); - it('appends VpcConfig to params when vpc params set', function() { + it('appends VpcConfig to params when vpc params set', function () { program.vpcSubnets = 'subnet-00000000,subnet-00000001,subnet-00000002'; program.vpcSecurityGroups = 'sg-00000000,sg-00000001,sg-00000002'; var params = lambda._params(program); @@ -108,33 +108,33 @@ describe('lib/main', function () { assert.equal(params.VpcConfig.SecurityGroupIds[2], program.vpcSecurityGroups.split(',')[2]); }); - it('does not append VpcConfig when params are not set', function() { + it('does not append VpcConfig when params are not set', function () { var params = lambda._params(program); assert.equal(Object.keys(params.VpcConfig.SubnetIds).length, 0); assert.equal(Object.keys(params.VpcConfig.SecurityGroupIds).length, 0); }); - it('appends DeadLetterConfig to params when DLQ params set', function() { - ['', 'arn:aws:sqs:test'].forEach(function(v) { + it('appends DeadLetterConfig to params when DLQ params set', function () { + ['', 'arn:aws:sqs:test'].forEach(function (v) { program.deadLetterConfigTargetArn = v; const params = lambda._params(program); assert.equal(params.DeadLetterConfig.TargetArn, v, v); }); }); - it('does not append DeadLetterConfig when params are not set', function() { + it('does not append DeadLetterConfig when params are not set', function () { delete program.deadLetterConfigTargetArn; var params = lambda._params(program); assert.isNull(params.DeadLetterConfig.TargetArn); }); - it('appends TracingConfig to params when params set', function() { + it('appends TracingConfig to params when params set', function () { program.tracingConfig = 'Active'; const params = lambda._params(program); assert.equal(params.TracingConfig.Mode, 'Active'); }); - it('does not append TracingConfig when params are not set', function() { + it('does not append TracingConfig when params are not set', function () { program.tracingConfig = ''; const params = lambda._params(program); assert.isNull(params.TracingConfig.Mode); @@ -155,8 +155,8 @@ describe('lib/main', function () { it('adds variables when configFile param is set', function () { program.configFile = 'tmp.env'; var params = lambda._params(program); - assert.equal(params.Environment.Variables['FOO'], "bar"); - assert.equal(params.Environment.Variables['BAZ'], "bing"); + assert.equal(params.Environment.Variables['FOO'], 'bar'); + assert.equal(params.Environment.Variables['BAZ'], 'bing'); }); it('when configFile param is set but it is an empty file', function () { @@ -196,7 +196,7 @@ describe('lib/main', function () { }); }); - function rsyncTests(funcName) { + function rsyncTests (funcName) { before(function () { fs.mkdirSync('build'); fs.mkdirsSync(path.join('__unittest', 'hoge')); @@ -278,7 +278,7 @@ describe('lib/main', function () { it(funcName + ' should not exclude package.json, even when excluded by excludeGlobs', function (done) { program.excludeGlobs = '*.json'; - lambda[funcName](program, '.', codeDirectory, true, function(err, result) { + lambda[funcName](program, '.', codeDirectory, true, function (err, result) { var contents = fs.readdirSync(codeDirectory); assert.include(contents, 'package.json'); done(); @@ -287,7 +287,7 @@ describe('lib/main', function () { it(funcName + ' should not include package.json when --prebuiltDirectory is set', function (done) { var buildDir = '.build_' + Date.now(); - after(function() { + after(function () { fs.removeSync(buildDir); }); @@ -297,7 +297,7 @@ describe('lib/main', function () { program.excludeGlobs = '*.json'; program.prebuiltDirectory = buildDir; - lambda[funcName](program, buildDir, codeDirectory, true, function(err, result) { + lambda[funcName](program, buildDir, codeDirectory, true, function (err, result) { var contents = fs.readdirSync(codeDirectory); assert.notInclude(contents, 'package.json', 'Target: "packages.json"'); assert.include(contents, 'testa', 'Target: "testa"'); @@ -307,11 +307,11 @@ describe('lib/main', function () { }); } - describe('_fileCopy', function() { rsyncTests('_fileCopy'); }); + describe('_fileCopy', function () { rsyncTests('_fileCopy'); }); if (process.platform === 'win32') { it('For Windows, `_rsync` tests pending'); } else { - describe('_rsync', function() { rsyncTests('_rsync'); }); + describe('_rsync', function () { rsyncTests('_rsync'); }); } describe('_npmInstall', function () { @@ -351,27 +351,27 @@ describe('lib/main', function () { /** * Capture console output */ - function captureStream(stream){ + function captureStream (stream) { var oldWrite = stream.write; var buf = ''; - stream.write = function(chunk, encoding, callback){ + stream.write = function (chunk, encoding, callback) { buf += chunk.toString(); // chunk is a String or Buffer oldWrite.apply(stream, arguments); } return { - unhook: function unhook(){ + unhook: function unhook () { stream.write = oldWrite; }, - captured: function(){ + captured: function () { return buf; } }; } - beforeEach(function(){ + beforeEach(function () { hook = captureStream(process.stdout); }); - afterEach(function(){ + afterEach(function () { hook.unhook(); if (fs.existsSync(postInstallScriptPath)) fs.unlinkSync(postInstallScriptPath); @@ -397,7 +397,10 @@ describe('lib/main', function () { fs.chmodSync(path.join(codeDirectory, 'post_install.sh'), '755'); lambda._postInstallScript(program, codeDirectory, function (err) { assert.isNull(err); - assert.equal("=> Running post install script post_install.sh\n\t\tYour environment is "+program.environment+"\n", hook.captured()); + assert.equal( + `=> Running post install script post_install.sh\n\t\tYour environment is ${program.environment}\n`, + hook.captured() + ); done(); }); }); @@ -457,7 +460,7 @@ describe('lib/main', function () { it('packages a prebuilt module without installing', function (done) { _timeout({ this: this, sec: 30 }); // give it time to zip var buildDir = '.build_' + Date.now(); - after(function() { + after(function () { fs.removeSync(buildDir); }); @@ -489,7 +492,7 @@ describe('lib/main', function () { describe('_readArchive', function () { const testZipFile = path.join(os.tmpdir(), 'node-lambda-test.zip'); var bufferExpected = null; - before(function(done) { + before(function (done) { _timeout({ this: this, sec: 30 }); // give it time to zip lambda._zip(program, codeDirectory, function (err, data) { @@ -499,7 +502,7 @@ describe('lib/main', function () { }); }); - after(function() { + after(function () { fs.unlinkSync(testZipFile); }); @@ -593,7 +596,7 @@ describe('lib/main', function () { ]; after(function () { - targetFiles.forEach(function(file) { + targetFiles.forEach(function (file) { fs.unlinkSync(file); }); program.eventSourceFile = ''; @@ -603,7 +606,7 @@ describe('lib/main', function () { lambda.setup(program); const libPath = path.join(__dirname, '..', 'lib'); - targetFiles.forEach(function(targetFile) { + targetFiles.forEach(function (targetFile) { const boilerplateFile = path.join(libPath, `${targetFile}.example`); assert.equal( @@ -633,7 +636,7 @@ describe('lib/main', function () { ); }); - describe('program.eventSourceFile is valid value', function() { + describe('program.eventSourceFile is valid value', function () { before(function () { fs.writeFileSync('only_EventSourceMappings.json', JSON.stringify({ EventSourceMappings: [{ test: 1 }] @@ -719,7 +722,7 @@ describe('lib/main', function () { program.eventSourceFile = ''; const eventSourceList = lambda._eventSourceList(program); return new Promise(function (resolve) { - lambda._updateEventSources(lambda, '', [], eventSourceList.EventSourceMappings, function(err, results) { + lambda._updateEventSources(lambda, '', [], eventSourceList.EventSourceMappings, function (err, results) { resolve({ err: err, results: results }); }); }).then(function (actual) { @@ -772,7 +775,7 @@ describe('lib/main', function () { program.eventSourceFile = ''; const eventSourceList = lambda._eventSourceList(program); return new Promise(function (resolve) { - lambda._updateScheduleEvents(schedule, '', eventSourceList.ScheduleEvents, function(err, results) { + lambda._updateScheduleEvents(schedule, '', eventSourceList.ScheduleEvents, function (err, results) { resolve({ err: err, results: results }); }); }).then(function (actual) { @@ -786,7 +789,7 @@ describe('lib/main', function () { const eventSourceList = lambda._eventSourceList(program); const functionArn = 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function'; return new Promise(function (resolve) { - lambda._updateScheduleEvents(schedule, functionArn, eventSourceList.ScheduleEvents, function(err, results) { + lambda._updateScheduleEvents(schedule, functionArn, eventSourceList.ScheduleEvents, function (err, results) { resolve({ err: err, results: results }); }); }).then(function (actual) { @@ -817,7 +820,7 @@ describe('lib/main', function () { afterEach(function () { fs.unlinkSync('newContext.json'); fs.unlinkSync('newEvent.json'); - filesCreatedBySetup.forEach(function(file) { + filesCreatedBySetup.forEach(function (file) { fs.unlinkSync(file); }); }); @@ -832,7 +835,7 @@ describe('lib/main', function () { assert.equal(fs.readFileSync('newEvent.json').toString(), '{"FOO"="bar"}'); const libPath = path.join(__dirname, '..', 'lib'); - filesCreatedBySetup.forEach(function(targetFile) { + filesCreatedBySetup.forEach(function (targetFile) { const boilerplateFile = path.join(libPath, `${targetFile}.example`); assert.equal( diff --git a/test/schedule_events.js b/test/schedule_events.js index 7806009a..7e3f3985 100644 --- a/test/schedule_events.js +++ b/test/schedule_events.js @@ -103,7 +103,7 @@ describe('lib/schedule_events', () => { describe('_addPermissionParams', () => { it('correct value', () => { const expected = { - Action:'lambda:InvokeFunction', + Action: 'lambda:InvokeFunction', FunctionName: 'node-lambda-test-function', Principal: 'events.amazonaws.com', SourceArn: 'arn:aws:events:hoge:fuga',