From 53525bb9057ff299db1f440d3e185c062079e8de Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 19 May 2017 12:57:01 +0900 Subject: [PATCH 01/13] Fix to use '===' instead of '==' (Including similar modifications) --- lib/main.js | 12 ++++++------ lib/schedule_events.js | 2 +- test/main.js | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/main.js b/lib/main.js index 47f63ad3..94d4b546 100644 --- a/lib/main.js +++ b/lib/main.js @@ -87,7 +87,7 @@ Lambda.prototype._runHandler = function (handler, event, program, context) { return timeout - (currentTime - startTime); }; - if (['nodejs4.3', 'nodejs6.10'].indexOf(program.runtime) == -1) { + if (['nodejs4.3', 'nodejs6.10'].indexOf(program.runtime) === -1) { console.error("Runtime [" + program.runtime + "] is not supported."); process.exit(254); } @@ -203,11 +203,11 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c // Formatting for `filter` of `fs.copy` const dirBlobs = []; const pattern = '{' + excludes.map(function (str) { - if (str.charAt(str.length - 1) == path.sep) { + if (str.charAt(str.length - 1) === path.sep) { str = str.substr(0, str.length - 1); dirBlobs.push(str); } - if (str.charAt(0) == path.sep) + if (str.charAt(0) === path.sep) return path.join(srcAbsolutePath, str); if (str.indexOf(path.sep) >= 0) return path.join(path.resolve('/**'), str); @@ -452,7 +452,7 @@ Lambda.prototype._archivePrebuilt = function (program, archiveCallback) { // (Used if there is a problem with `_ fileCopy`) // If there is no problem even if deleting `_rsync`, this switching process is deleted var copyFunction = '_fileCopy'; - if (process.env.NODE_LAMBDA_COPY_FUNCTION == 'rsync') { + if (process.env.NODE_LAMBDA_COPY_FUNCTION === 'rsync') { console.log('=> INFO: Use rsync for copy'); copyFunction = '_rsync'; } @@ -494,7 +494,7 @@ Lambda.prototype._buildAndArchive = function (program, archiveCallback) { // (Used if there is a problem with `_ fileCopy`) // If there is no problem even if deleting `_rsync`, this switching process is deleted var copyFunction = '_fileCopy'; - if (process.env.NODE_LAMBDA_COPY_FUNCTION == 'rsync') { + if (process.env.NODE_LAMBDA_COPY_FUNCTION === 'rsync') { console.log('=> INFO: Use rsync for copy'); copyFunction = '_rsync'; } @@ -782,7 +782,7 @@ Lambda.prototype.deploy = function (program) { return result.filter(function(res) { return res.length > 0; }).length > 0; - }).length == 0; + }).length === 0; if (!resultsIsEmpty) { console.log('=> All tasks done. Results follow: '); console.log(JSON.stringify(results, null, ' ')); diff --git a/lib/schedule_events.js b/lib/schedule_events.js index a204a04e..b9edab89 100644 --- a/lib/schedule_events.js +++ b/lib/schedule_events.js @@ -60,7 +60,7 @@ ScheduleEvents.prototype = { const _params = _this._addPermissionParams(params); _this.lambda.addPermission(_params, (err, data) => { if (err) { - if (err.code != 'ResourceConflictException') throw err; + if (err.code !== 'ResourceConflictException') throw err; // If it exists it will result in an error but there is no problem. resolve('Already exists permission'); } diff --git a/test/main.js b/test/main.js index 1aa02b55..9e429feb 100644 --- a/test/main.js +++ b/test/main.js @@ -40,13 +40,13 @@ 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. - if (process.platform != 'win32') { + if (process.platform !== 'win32') { params.this.timeout(params.sec * 1000); } } describe('lib/main', function () { - if (process.platform == 'win32') { + if (process.platform === 'win32') { // It seems that it takes time for file operation in Windows. // So set `timeout(60000)` for the whole test. this.timeout(60000); @@ -272,7 +272,7 @@ describe('lib/main', function () { assert.notInclude(contents, 'fuga', 'Target: "__unittest/fuga"'); contents = fs.readdirSync(path.join(codeDirectory, '__unittest', 'hoge')); - assert.isTrue(contents.length == 0, 'directory:__unittest/hoge is empty'); + assert.equal(contents.length, 0, 'directory:__unittest/hoge is empty'); done(); }); }); @@ -309,7 +309,7 @@ describe('lib/main', function () { } describe('_fileCopy', function() { rsyncTests('_fileCopy'); }); - if (process.platform == 'win32') { + if (process.platform === 'win32') { it('For Windows, `_rsync` tests pending'); } else { describe('_rsync', function() { rsyncTests('_rsync'); }); @@ -343,7 +343,7 @@ describe('lib/main', function () { }); describe('_postInstallScript', function () { - if (process.platform == 'win32') { + if (process.platform === 'win32') { return it('`_postInstallScript` test does not support Windows.'); } From 1b4905663a50da86ba4ea908baa64c01ba2770d0 Mon Sep 17 00:00:00 2001 From: abetomo Date: Mon, 22 May 2017 13:50:43 +0900 Subject: [PATCH 02/13] Fix using strings for single quotes --- lib/main.js | 6 +++--- test/main.js | 9 ++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/main.js b/lib/main.js index 3f2a927f..0b973a96 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); @@ -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); }); }; diff --git a/test/main.js b/test/main.js index 9e429feb..555f1abd 100644 --- a/test/main.js +++ b/test/main.js @@ -156,8 +156,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 () { @@ -398,7 +398,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(); }); }); From b3971cf74f7fa63eae31e37fc1acafea5f1d2415 Mon Sep 17 00:00:00 2001 From: abetomo Date: Mon, 22 May 2017 13:57:52 +0900 Subject: [PATCH 03/13] Fix using let Fix for not being careful with `VARIABLE is already defined` in JavaScript Standard Style --- lib/main.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/main.js b/lib/main.js index 0b973a96..60f4203a 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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; From 5a044810d8db92b8a8d37dfb9aae4d5cfcf34ed1 Mon Sep 17 00:00:00 2001 From: abetomo Date: Mon, 22 May 2017 14:09:49 +0900 Subject: [PATCH 04/13] Add space and format the code Fix to prevent the message like the following from being displayed - Missing space before function parentheses. - Missing space before opening brace. --- index.js | 2 +- lib/main.js | 40 +++++++++++++------------- lib/schedule_events.js | 16 +++++------ test/main.js | 62 ++++++++++++++++++++--------------------- test/schedule_events.js | 2 +- 5 files changed, 61 insertions(+), 61 deletions(-) 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 60f4203a..5c4d32ce 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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) { @@ -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 555f1abd..11dfcf95 100644 --- a/test/main.js +++ b/test/main.js @@ -36,7 +36,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. @@ -97,7 +97,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); @@ -109,33 +109,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); @@ -197,7 +197,7 @@ describe('lib/main', function () { }); }); - function rsyncTests(funcName) { + function rsyncTests (funcName) { before(function () { fs.mkdirSync('build'); fs.mkdirsSync(path.join('__unittest', 'hoge')); @@ -279,7 +279,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(); @@ -288,7 +288,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 () { rimraf.sync(buildDir, fs); }); @@ -298,7 +298,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"'); @@ -308,11 +308,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 () { @@ -352,27 +352,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); @@ -461,7 +461,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 () { rimraf.sync(buildDir, fs); }); @@ -493,7 +493,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) { @@ -503,7 +503,7 @@ describe('lib/main', function () { }); }); - after(function() { + after(function () { fs.unlinkSync(testZipFile); }); @@ -597,7 +597,7 @@ describe('lib/main', function () { ]; after(function () { - targetFiles.forEach(function(file) { + targetFiles.forEach(function (file) { fs.unlinkSync(file); }); program.eventSourceFile = ''; @@ -607,7 +607,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( @@ -637,7 +637,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 }] @@ -723,7 +723,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) { @@ -776,7 +776,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) { @@ -790,7 +790,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) { @@ -821,7 +821,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); }); }); @@ -836,7 +836,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', From d29c70af02a85144ef8e64efffd948759155054f Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 23 May 2017 12:55:27 +0900 Subject: [PATCH 05/13] Add comment about global function --- test/main.js | 1 + test/node-lambda.js | 1 + test/schedule_events.js | 1 + 3 files changed, 3 insertions(+) diff --git a/test/main.js b/test/main.js index 48be948f..9f5422e0 100644 --- a/test/main.js +++ b/test/main.js @@ -44,6 +44,7 @@ function _timeout (params) { } } +/* global before, after, beforeEach, afterEach, describe, it */ describe('lib/main', function () { if (process.platform === 'win32') { // It seems that it takes time for file operation in Windows. diff --git a/test/node-lambda.js b/test/node-lambda.js index 0bef84ee..c43323a1 100644 --- a/test/node-lambda.js +++ b/test/node-lambda.js @@ -7,6 +7,7 @@ const spawn = require('child_process').spawn; const execSync = require('child_process').execSync; const nodeLambdaPath = path.join(__dirname, '..', 'bin', 'node-lambda'); +/* global before, after, beforeEach, afterEach, describe, it */ // The reason for specifying the node command in this test is to support Windows. describe('bin/node-lambda', () => { describe('node-lambda run', () => { diff --git a/test/schedule_events.js b/test/schedule_events.js index 7e3f3985..4aead1ab 100644 --- a/test/schedule_events.js +++ b/test/schedule_events.js @@ -38,6 +38,7 @@ const mockResponse = { var schedule = null; +/* global before, after, beforeEach, afterEach, describe, it */ describe('lib/schedule_events', () => { before(() => { aws.mock('CloudWatchEvents', 'putRule', (params, callback) => { From 014144caf60d1a65a64279b4f9e3468d76f092b1 Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 23 May 2017 13:04:39 +0900 Subject: [PATCH 06/13] Remove unused function names --- test/node-lambda.js | 2 +- test/schedule_events.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/node-lambda.js b/test/node-lambda.js index c43323a1..8bff1504 100644 --- a/test/node-lambda.js +++ b/test/node-lambda.js @@ -7,7 +7,7 @@ const spawn = require('child_process').spawn; const execSync = require('child_process').execSync; const nodeLambdaPath = path.join(__dirname, '..', 'bin', 'node-lambda'); -/* global before, after, beforeEach, afterEach, describe, it */ +/* global before, after, describe, it */ // The reason for specifying the node command in this test is to support Windows. describe('bin/node-lambda', () => { describe('node-lambda run', () => { diff --git a/test/schedule_events.js b/test/schedule_events.js index 4aead1ab..3ee2507c 100644 --- a/test/schedule_events.js +++ b/test/schedule_events.js @@ -38,7 +38,7 @@ const mockResponse = { var schedule = null; -/* global before, after, beforeEach, afterEach, describe, it */ +/* global before, after, describe, it */ describe('lib/schedule_events', () => { before(() => { aws.mock('CloudWatchEvents', 'putRule', (params, callback) => { From d851cd740c0f1d0c09f4892929a4c4dcff2c02c6 Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 23 May 2017 13:05:14 +0900 Subject: [PATCH 07/13] Add test of variable `err` --- test/main.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/main.js b/test/main.js index 9f5422e0..3abf4555 100644 --- a/test/main.js +++ b/test/main.js @@ -185,6 +185,7 @@ describe('lib/main', function () { it('`codeDirectory` is empty. (For `codeDirectory` where the file was present)', function (done) { lambda._fileCopy(program, '.', codeDirectory, true, function (err, result) { + assert.isNull(err); const contents = fs.readdirSync(codeDirectory); assert.isTrue(contents.length > 0); lambda._cleanDirectory(codeDirectory, function () { @@ -218,6 +219,7 @@ describe('lib/main', function () { it(funcName + ' an index.js as well as other files', function (done) { lambda[funcName](program, '.', codeDirectory, true, function (err, result) { + assert.isNull(err); var contents = fs.readdirSync(codeDirectory); ['index.js', 'package.json'].forEach(function (needle) { assert.include(contents, needle, `Target: "${needle}"`); @@ -245,6 +247,7 @@ describe('lib/main', function () { it(funcName + ' an index.js as well as other files', function (done) { lambda[funcName](program, '.', codeDirectory, true, function (err, result) { + assert.isNull(err); var contents = fs.readdirSync(codeDirectory); ['index.js', 'package.json'].forEach(function (needle) { assert.include(contents, needle, `Target: "${needle}"`); @@ -255,6 +258,7 @@ describe('lib/main', function () { it(funcName + ' excludes files matching excludeGlobs', function (done) { lambda[funcName](program, '.', codeDirectory, true, function (err, result) { + assert.isNull(err); var contents = fs.readdirSync(codeDirectory); ['__unittest', 'fuga'].forEach(function (needle) { assert.include(contents, needle, `Target: "${needle}"`); @@ -280,6 +284,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) { + assert.isNull(err); var contents = fs.readdirSync(codeDirectory); assert.include(contents, 'package.json'); done(); @@ -299,6 +304,7 @@ describe('lib/main', function () { program.excludeGlobs = '*.json'; program.prebuiltDirectory = buildDir; lambda[funcName](program, buildDir, codeDirectory, true, function (err, result) { + assert.isNull(err); var contents = fs.readdirSync(codeDirectory); assert.notInclude(contents, 'package.json', 'Target: "packages.json"'); assert.include(contents, 'testa', 'Target: "testa"'); @@ -335,6 +341,7 @@ describe('lib/main', function () { _timeout({ this: this, sec: 30 }); // give it time to build the node modules lambda._npmInstall(program, codeDirectory, function (err, result) { + assert.isNull(err); var contents = fs.readdirSync(codeDirectory); assert.include(contents, 'node_modules'); done(); @@ -433,6 +440,7 @@ describe('lib/main', function () { _timeout({ this: this, sec: 30 }); // give it time to zip lambda._zip(program, codeDirectory, function (err, data) { + assert.isNull(err); var archive = new zip(data); var contents = Object.keys(archive.files).map(function (k) { return archive.files[k].name.toString(); @@ -448,6 +456,7 @@ describe('lib/main', function () { _timeout({ this: this, sec: 30 }); // give it time to zip lambda._archive(program, function (err, data) { + assert.isNull(err); var archive = new zip(data); var contents = Object.keys(archive.files).map(function (k) { return archive.files[k].name.toString(); @@ -474,6 +483,7 @@ describe('lib/main', function () { program.prebuiltDirectory = buildDir; lambda._archive(program, function (err, data) { + assert.isNull(err); var archive = new zip(data); var contents = Object.keys(archive.files).map(function (k) { return archive.files[k].name.toString(); @@ -497,6 +507,7 @@ describe('lib/main', function () { _timeout({ this: this, sec: 30 }); // give it time to zip lambda._zip(program, codeDirectory, function (err, data) { + assert.isNull(err); bufferExpected = data; fs.writeFileSync(testZipFile, data); done(); @@ -542,6 +553,7 @@ describe('lib/main', function () { const _program = Object.assign({ deployZipfile: filePath }, program); _timeout({ this: this, sec: 30 }); // give it time to zip lambda._archive(_program, function (err, data) { + assert.isNull(err); // same test as "installs and zips with an index.js file and node_modules/async" var archive = new zip(data); var contents = Object.keys(archive.files).map(function (k) { From 4054a654071ce366387a0569537cfc8517876034 Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 23 May 2017 13:12:03 +0900 Subject: [PATCH 08/13] Remove unnecessary end of line commas --- lib/main.js | 6 +++--- lib/schedule_events.js | 2 +- test/main.js | 12 ++++++------ test/node-lambda.js | 14 +++++++------- test/schedule_events.js | 6 +++--- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/main.js b/lib/main.js index 5c4d32ce..4c01cca7 100644 --- a/lib/main.js +++ b/lib/main.js @@ -284,7 +284,7 @@ Lambda.prototype._rsync = function (program, src, dest, excludeNodeModules, call Lambda.prototype._npmInstall = function (program, codeDirectory, callback) { const installOptions = [ `--prefix ${codeDirectory}`, - process.platform === 'win32' ? `--cwd ${codeDirectory}` : null, + process.platform === 'win32' ? `--cwd ${codeDirectory}` : null ].join(' '); var command = program.dockerImage ? 'docker run --rm -v ' + codeDirectory + ':/var/task ' + program.dockerImage + ' npm -s install --production' : @@ -399,7 +399,7 @@ Lambda.prototype._uploadExisting = function (lambda, params, cb) { 'VpcConfig': params.VpcConfig, 'Environment': params.Environment, 'DeadLetterConfig': params.DeadLetterConfig, - 'TracingConfig': params.TracingConfig, + 'TracingConfig': params.TracingConfig }, function (err, data) { return cb(err, data); }); @@ -561,7 +561,7 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE 'EventSourceArn': eventSourceList[i]['EventSourceArn'], 'Enabled': eventSourceList[i]['Enabled'] ? eventSourceList[i]['Enabled'] : false, 'BatchSize': eventSourceList[i]['BatchSize'] ? eventSourceList[i]['BatchSize'] : 100, - 'StartingPosition': eventSourceList[i]['StartingPosition'] ? eventSourceList[i]['StartingPosition'] : 'LATEST', + 'StartingPosition': eventSourceList[i]['StartingPosition'] ? eventSourceList[i]['StartingPosition'] : 'LATEST' }); } } diff --git a/lib/schedule_events.js b/lib/schedule_events.js index 835f3c9b..f3c2d573 100644 --- a/lib/schedule_events.js +++ b/lib/schedule_events.js @@ -100,7 +100,7 @@ ScheduleEvents.prototype = { }).then((data) => { return _this._putTargets(params); }); - }, + } }; module.exports = ScheduleEvents; diff --git a/test/main.js b/test/main.js index 3abf4555..3325a349 100644 --- a/test/main.js +++ b/test/main.js @@ -30,7 +30,7 @@ var originalProgram = { eventSourceFile: '', contextFile: 'context.json', deployTimeout: 120000, - prebuiltDirectory: '', + prebuiltDirectory: '' }; var codeDirectory = lambda._codeDirectory(); @@ -668,7 +668,7 @@ describe('lib/main', function () { program.eventSourceFile = 'only_EventSourceMappings.json'; const expected = { EventSourceMappings: [{ test: 1 }], - ScheduleEvents: [], + ScheduleEvents: [] }; assert.deepEqual(lambda._eventSourceList(program), expected); }); @@ -677,7 +677,7 @@ describe('lib/main', function () { program.eventSourceFile = 'only_ScheduleEvents.json'; const expected = { EventSourceMappings: [], - ScheduleEvents: [{ test: 2 }], + ScheduleEvents: [{ test: 2 }] }; assert.deepEqual(lambda._eventSourceList(program), expected); }); @@ -689,13 +689,13 @@ describe('lib/main', function () { BatchSize: 100, Enabled: true, EventSourceArn: 'your event source arn', - StartingPosition: 'LATEST', + StartingPosition: 'LATEST' }], ScheduleEvents: [{ ScheduleName: 'node-lambda-test-schedule', ScheduleState: 'ENABLED', ScheduleExpression: 'rate(1 hour)' - }], + }] }; assert.deepEqual(lambda._eventSourceList(program), expected); }); @@ -706,7 +706,7 @@ describe('lib/main', function () { BatchSize: 100, Enabled: true, EventSourceArn: 'your event source arn', - StartingPosition: 'LATEST', + StartingPosition: 'LATEST' }]; const fileName = 'event_sources_old_style.json'; diff --git a/test/node-lambda.js b/test/node-lambda.js index 8bff1504..9c194847 100644 --- a/test/node-lambda.js +++ b/test/node-lambda.js @@ -15,7 +15,7 @@ describe('bin/node-lambda', () => { const run = spawn('node', [ nodeLambdaPath, 'run', '--handler', '__test.handler', - '--eventFile', 'event.json', + '--eventFile', 'event.json' ]); var stdoutString = ''; run.stdout.on('data', (data) => { @@ -88,21 +88,21 @@ describe('bin/node-lambda', () => { it('`node-lambda run` exitCode is `0` (callback(null))', (done) => { _generateEventFile(Object.assign(eventObj, { - callbackCode: 'callback(null);', + callbackCode: 'callback(null);' })); _testMain({ stdoutRegExp: /Success:sleep 3500 msec$/, exitCode: 0 }, done); }); it('`node-lambda run` exitCode is `0` (callback(null, "text"))', (done) => { _generateEventFile(Object.assign(eventObj, { - callbackCode: 'callback(null, "text");', + callbackCode: 'callback(null, "text");' })); _testMain({ stdoutRegExp: /Success:"text"sleep 3500 msec$/, exitCode: 0 }, done); }); it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', (done) => { _generateEventFile(Object.assign(eventObj, { - callbackCode: 'callback(new Error("e"));', + callbackCode: 'callback(new Error("e"));' })); _testMain({ stdoutRegExp: /Error: Error: esleep 3500 msec$/, exitCode: 255 }, done); }); @@ -116,21 +116,21 @@ describe('bin/node-lambda', () => { it('`node-lambda run` exitCode is `0` (callback(null))', (done) => { _generateEventFile(Object.assign(eventObj, { - callbackCode: 'callback(null);', + callbackCode: 'callback(null);' })); _testMain({ stdoutRegExp: /Success:$/, exitCode: 0 }, done); }); it('`node-lambda run` exitCode is `0` (callback(null, "text"))', (done) => { _generateEventFile(Object.assign(eventObj, { - callbackCode: 'callback(null, "text");', + callbackCode: 'callback(null, "text");' })); _testMain({ stdoutRegExp: /Success:"text"$/, exitCode: 0 }, done); }); it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', (done) => { _generateEventFile(Object.assign(eventObj, { - callbackCode: 'callback(new Error("e"));', + callbackCode: 'callback(new Error("e"));' })); _testMain({ stdoutRegExp: /Error: Error: e$/, exitCode: 255 }, done); }); diff --git a/test/schedule_events.js b/test/schedule_events.js index 3ee2507c..1058dbf7 100644 --- a/test/schedule_events.js +++ b/test/schedule_events.js @@ -33,7 +33,7 @@ const mockResponse = { putTargets: { FailedEntries: [], FailedEntryCount: 0 - }, + } }; var schedule = null; @@ -108,7 +108,7 @@ describe('lib/schedule_events', () => { FunctionName: 'node-lambda-test-function', Principal: 'events.amazonaws.com', SourceArn: 'arn:aws:events:hoge:fuga', - StatementId: 'node-lambda-test-schedule', + StatementId: 'node-lambda-test-schedule' }; const _params = Object.assign(params, mockResponse.putRule); assert.deepEqual(schedule._addPermissionParams(_params), expected); @@ -121,7 +121,7 @@ describe('lib/schedule_events', () => { Rule: 'node-lambda-test-schedule', Targets: [{ Arn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function', - Id: 'node-lambda-test-function', + Id: 'node-lambda-test-function' }] }; assert.deepEqual(schedule._putTargetsParams(params), expected); From d22c06d388d45d0ec0eec64ee90394b15a44233d Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 23 May 2017 13:13:51 +0900 Subject: [PATCH 09/13] Remove unnecessary line feeds --- test/main.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/main.js b/test/main.js index 3325a349..0a02f5a5 100644 --- a/test/main.js +++ b/test/main.js @@ -587,7 +587,6 @@ describe('lib/main', function () { }); it('should inject environment variables at runtime', function () { - // Run it... lambda._setRunTimeEnvironmentVars({ configFile: 'tmp.env' @@ -596,7 +595,6 @@ describe('lib/main', function () { assert.equal(process.env.FOO, 'bar'); assert.equal(process.env.BAZ, 'bing'); }); - }); describe('create sample files', function () { From e7fd96dae0b6ae45e397e4219642ecbc7efce0dd Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 23 May 2017 13:18:43 +0900 Subject: [PATCH 10/13] Fix to adjust the space --- lib/main.js | 10 +++++----- lib/schedule_events.js | 2 +- test/main.js | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/main.js b/lib/main.js index 4c01cca7..54ce130f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -162,7 +162,7 @@ Lambda.prototype._eventSourceList = function (program) { const list = (function () { try { return fs.readJsonSync(program.eventSourceFile); - } catch(err) { + } catch (err) { throw err; } })(); @@ -247,7 +247,7 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c // `_rsync` will be replaced by` _fileCopy`. Lambda.prototype._rsync = function (program, src, dest, excludeNodeModules, callback) { var excludes = ['.git*', '*.swp', '.editorconfig', '.lambda', 'deploy.env', '*.log', '/build/'], - excludeGlobs = []; + excludeGlobs = []; if (program.excludeGlobs) { excludeGlobs = program.excludeGlobs.split(' '); } @@ -384,7 +384,7 @@ Lambda.prototype._uploadExisting = function (lambda, params, cb) { 'ZipFile': params.Code.ZipFile, 'Publish': params.Publish }, function (err, data) { - if(err) { + if (err) { return cb(err, data); } @@ -586,7 +586,7 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE } return async.map(updateEventSourceList, function (updateEventSource, _cb) { - switch(updateEventSource['type']) { + switch (updateEventSource['type']) { case 'create': delete updateEventSource['type']; lambda.createEventSourceMapping(updateEventSource, function (err, data) { @@ -640,7 +640,7 @@ Lambda.prototype.package = function (program) { if (!isDir) { throw program.packageDirectory + ' is not a directory!'; } - } catch(err) { + } catch (err) { if (err.code === 'ENOENT') { console.log('=> Creating package directory'); fs.mkdirsSync(program.packageDirectory); diff --git a/lib/schedule_events.js b/lib/schedule_events.js index f3c2d573..d4646745 100644 --- a/lib/schedule_events.js +++ b/lib/schedule_events.js @@ -85,7 +85,7 @@ ScheduleEvents.prototype = { const _params = _this._putTargetsParams(params); _this.cloudwatchevents.putTargets(_params, (err, data) => { // even if it is already registered, it will not be an error. - if (err) throw(err); + if (err) throw (err); resolve(data); }); }); diff --git a/test/main.js b/test/main.js index 0a02f5a5..73ea80db 100644 --- a/test/main.js +++ b/test/main.js @@ -369,7 +369,7 @@ describe('lib/main', function () { return { unhook: function unhook () { - stream.write = oldWrite; + stream.write = oldWrite; }, captured: function () { return buf; From 12f834fb2d5484e6312b1e07fc0420757bd1e64f Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 23 May 2017 14:02:33 +0900 Subject: [PATCH 11/13] Remove unnecessary line feed --- lib/main.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index 54ce130f..f99c5519 100644 --- a/lib/main.js +++ b/lib/main.js @@ -790,5 +790,4 @@ Lambda.prototype.deploy = function (program) { }); }; - module.exports = new Lambda(); From a3b009e228808834e8d8ed61bcf5cda8ab7df373 Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 23 May 2017 14:03:54 +0900 Subject: [PATCH 12/13] Fix to start variable name with uppper case For variables like class --- test/main.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/main.js b/test/main.js index 73ea80db..7a5a7ade 100644 --- a/test/main.js +++ b/test/main.js @@ -7,7 +7,7 @@ var program = require('commander'); var fs = require('fs-extra'); var Hoek = require('hoek'); var lambda = require(path.join(__dirname, '..', 'lib', 'main')); -var zip = require('node-zip'); +var Zip = require('node-zip'); var assert = chai.assert; @@ -441,7 +441,7 @@ describe('lib/main', function () { lambda._zip(program, codeDirectory, function (err, data) { assert.isNull(err); - var archive = new zip(data); + var archive = new Zip(data); var contents = Object.keys(archive.files).map(function (k) { return archive.files[k].name.toString(); }); @@ -457,7 +457,7 @@ describe('lib/main', function () { lambda._archive(program, function (err, data) { assert.isNull(err); - var archive = new zip(data); + var archive = new Zip(data); var contents = Object.keys(archive.files).map(function (k) { return archive.files[k].name.toString(); }); @@ -484,7 +484,7 @@ describe('lib/main', function () { program.prebuiltDirectory = buildDir; lambda._archive(program, function (err, data) { assert.isNull(err); - var archive = new zip(data); + var archive = new Zip(data); var contents = Object.keys(archive.files).map(function (k) { return archive.files[k].name.toString(); }); @@ -555,7 +555,7 @@ describe('lib/main', function () { lambda._archive(_program, function (err, data) { assert.isNull(err); // same test as "installs and zips with an index.js file and node_modules/async" - var archive = new zip(data); + var archive = new Zip(data); var contents = Object.keys(archive.files).map(function (k) { return archive.files[k].name.toString(); }); From 58baefabe7f690c0a0fae47d4d53ab863d9e067d Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 23 May 2017 14:05:54 +0900 Subject: [PATCH 13/13] Fix to enclose it in parentheses to make it easier to understand --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index f99c5519..c7de33cb 100644 --- a/lib/main.js +++ b/lib/main.js @@ -7,7 +7,7 @@ var fs = require('fs-extra'); var packageJson = require(path.join(__dirname, '..', 'package.json')); var minimatch = require('minimatch'); var async = require('async'); -var zip = new require('node-zip')(); +var zip = new (require('node-zip'))(); var dotenv = require('dotenv'); var ScheduleEvents = require(path.join(__dirname, 'schedule_events'));