Skip to content

Commit

Permalink
Unified in Camel Case & Remove unnecessary arguments (#278)
Browse files Browse the repository at this point in the history
* Delete unnecessary variables from `_codeDirectory`

Also added tests

* Fix to unify variable to camel case

* Remove argument from `_codeDirectory` in test

Argument is no longer needed

* Remove unnecessary arguments
  • Loading branch information
abetomo authored and DeviaVir committed May 12, 2017
1 parent 73cbee5 commit 605f95f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
54 changes: 26 additions & 28 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ Lambda.prototype._eventSourceList = function (program) {
* @deprecated
*/
Lambda.prototype._zipfileTmpPath = function (program) {
var ms_since_epoch = +new Date();
var filename = program.functionName + '-' + ms_since_epoch + '.zip';
var msSinceEpoch = +new Date();
var filename = program.functionName + '-' + msSinceEpoch + '.zip';
var zipfile = path.join(os.tmpdir(), filename);

return zipfile;
Expand Down Expand Up @@ -371,9 +371,7 @@ Lambda.prototype._nativeZip = function (program, codeDirectory, callback) {
});
};

Lambda.prototype._codeDirectory = function (program) {
var epoch_time = +new Date();

Lambda.prototype._codeDirectory = function () {
return path.resolve('.', '.lambda');
};

Expand Down Expand Up @@ -454,25 +452,25 @@ Lambda.prototype._uploadNew = function (lambda, params, cb) {
return request;
};

Lambda.prototype._readArchive = function (program, archive_callback) {
Lambda.prototype._readArchive = function (program, archiveCallback) {
if (!fs.existsSync(program.deployZipfile)) {
var err = new Error('No such Zipfile [' + program.deployZipfile + ']');
return archive_callback(err);
return archiveCallback(err);
}
fs.readFile(program.deployZipfile, archive_callback);
fs.readFile(program.deployZipfile, archiveCallback);
};

Lambda.prototype._archive = function (program, archive_callback) {
Lambda.prototype._archive = function (program, archiveCallback) {
if (program.deployZipfile && fs.existsSync(program.deployZipfile)) {
return this._readArchive(program, archive_callback);
return this._readArchive(program, archiveCallback);
}
return program.prebuiltDirectory ?
this._archivePrebuilt(program, archive_callback) :
this._buildAndArchive(program, archive_callback);
this._archivePrebuilt(program, archiveCallback) :
this._buildAndArchive(program, archiveCallback);
};

Lambda.prototype._archivePrebuilt = function (program, archive_callback) {
var codeDirectory = this._codeDirectory(program);
Lambda.prototype._archivePrebuilt = function (program, archiveCallback) {
var codeDirectory = this._codeDirectory();
var _this = this;

// It is switched to `_ rsync` by environment variable.
Expand All @@ -485,18 +483,18 @@ Lambda.prototype._archivePrebuilt = function (program, archive_callback) {
}
this[copyFunction](program, program.prebuiltDirectory, codeDirectory, false, function (err) {
if (err) {
return archive_callback(err);
return archiveCallback(err);
}

console.log('=> Zipping deployment package');
var archive = process.platform !== 'win32' ? _this._nativeZip : _this._zip;
archive = archive.bind(_this);

archive(program, codeDirectory, archive_callback);
archive(program, codeDirectory, archiveCallback);
});
};

Lambda.prototype._buildAndArchive = function (program, archive_callback) {
Lambda.prototype._buildAndArchive = function (program, archiveCallback) {
if (!fs.existsSync('.env')) {
console.warn('[Warning] `.env` file does not exist.');
console.info('Execute `node-lambda setup` as necessary and set it up.');
Expand All @@ -511,12 +509,12 @@ Lambda.prototype._buildAndArchive = function (program, archive_callback) {
}

var _this = this;
var codeDirectory = _this._codeDirectory(program);
var codeDirectory = _this._codeDirectory();
var lambdaSrcDirectory = program.sourceDirectory ? program.sourceDirectory.replace(/\/$/, '') : '.';

_this._cleanDirectory(codeDirectory, function (err) {
if (err) {
return archive_callback(err);
return archiveCallback(err);
}
console.log('=> Moving files to temporary directory');

Expand All @@ -531,25 +529,25 @@ Lambda.prototype._buildAndArchive = function (program, archive_callback) {
// Move files to tmp folder
_this[copyFunction](program, lambdaSrcDirectory, codeDirectory, true, function (err) {
if (err) {
return archive_callback(err);
return archiveCallback(err);
}
console.log('=> Running npm install --production');
_this._npmInstall(program, codeDirectory, function (err) {
if (err) {
return archive_callback(err);
return archiveCallback(err);
}

_this._postInstallScript(program, codeDirectory, function (err) {
if (err) {
return archive_callback(err);
return archiveCallback(err);
}

console.log('=> Zipping deployment package');

var archive = process.platform !== 'win32' ? _this._nativeZip : _this._zip;
archive = archive.bind(_this);

archive(program, codeDirectory, archive_callback);
archive(program, codeDirectory, archiveCallback);
});
});
});
Expand Down Expand Up @@ -719,7 +717,7 @@ Lambda.prototype.deploy = function (program) {
console.log('=> Uploading zip file to AWS Lambda ' + region + ' with parameters:');
console.log(params);

var aws_security = {
var awsSecurity = {
region: region
};

Expand All @@ -728,19 +726,19 @@ Lambda.prototype.deploy = function (program) {
profile: program.profile
});
} else {
aws_security.accessKeyId = program.accessKey;
aws_security.secretAccessKey = program.secretKey;
awsSecurity.accessKeyId = program.accessKey;
awsSecurity.secretAccessKey = program.secretKey;
}

if (program.sessionToken) {
aws_security.sessionToken = program.sessionToken;
awsSecurity.sessionToken = program.sessionToken;
}

if (program.deployTimeout) {
aws.config.httpOptions.timeout = parseInt(program.deployTimeout);
}

aws.config.update(aws_security);
aws.config.update(awsSecurity);

var lambda = new aws.Lambda({
apiVersion: '2015-03-31'
Expand Down
8 changes: 7 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var originalProgram = {
prebuiltDirectory: '',
};

var codeDirectory = lambda._codeDirectory(Hoek.clone(originalProgram));
var codeDirectory = lambda._codeDirectory();

function _timeout(params) {
// Even if timeout is set for the whole test for Windows,
Expand Down Expand Up @@ -65,6 +65,12 @@ describe('node-lambda', function () {
assert.equal(lambda.version, '0.10.0');
});

describe('_codeDirectory', function () {
it('.lambda in the current directory', function () {
assert.equal(lambda._codeDirectory(), path.resolve('.', '.lambda'));
});
});

describe('_params', function () {
// http://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html#SSS-CreateFunction-request-FunctionName
const functionNamePattern =
Expand Down

0 comments on commit 605f95f

Please sign in to comment.