Refactor jawsDebug to auto find context.

This commit is contained in:
Aaron Boushley 2015-09-25 10:06:14 -07:00
parent 61cfce4fc3
commit 77f264eaff
7 changed files with 103 additions and 44 deletions

View File

@ -74,7 +74,7 @@ CMD.prototype.run = Promise.method(function() {
.then(_this._validate)
.then(_this._getTaggedLambdaPaths)
.then(function() {
utils.jawsDebug('deploy_lambda', 'Deploying to stage:', _this._stage);
utils.jawsDebug('Deploying to stage:', _this._stage);
return _this._regions;
})
.each(function(region) {
@ -137,8 +137,8 @@ CMD.prototype._setRegions = Promise.method(function() {
this._regions = projJson.stages[stage];
}
utils.jawsDebug('deploy_lambda', 'Deploying to regions:');
utils.jawsDebug('deploy_lambda', this._regions);
utils.jawsDebug('Deploying to regions:');
utils.jawsDebug(this._regions);
});
/**
@ -268,7 +268,7 @@ Deployer.prototype.deploy = Promise.method(function() {
var createOrUpdate,
cfDeferred;
utils.jawsDebug('deploy_lambda', 'Deploying with lambda role arn ' + lambdaRoleArn);
utils.jawsDebug('Deploying with lambda role arn ' + lambdaRoleArn);
if (existingStack) {
cfDeferred = AWSUtils.cfUpdateLambdasStack(_this._JAWS, _this._stage, _this._region, lambdaRoleArn);
@ -327,7 +327,7 @@ Deployer.prototype._generateLambdaCf = function(taggedLambdaPkgs, lambdaRoleArn)
throw new JawsError(e.message, JawsError.errorCodes.UNKNOWN);
}
utils.jawsDebug('deploy_lambda', 'no exsting lambda stack');
utils.jawsDebug('no exsting lambda stack');
existingStack = false;
return false;
})
@ -353,15 +353,15 @@ Deployer.prototype._generateLambdaCf = function(taggedLambdaPkgs, lambdaRoleArn)
Ref: "aaLambdaRoleArn"
};
utils.jawsDebug('deploy_lambda', 'adding Resource ' + pkg.lambdaName + ': ');
utils.jawsDebug('deploy_lambda', lResource);
utils.jawsDebug('adding Resource ' + pkg.lambdaName + ': ');
utils.jawsDebug(lResource);
lambdaCf.Resources[pkg.lambdaName] = lResource;
});
// If existing lambdas CF template
if (cfTemplateBody) {
utils.jawsDebug('deploy_lambda', 'existing stack detected');
utils.jawsDebug('existing stack detected');
// Find all lambdas in project, and copy ones that are in existing lambda-cf
var existingTemplate = JSON.parse(cfTemplateBody);
@ -371,7 +371,7 @@ Deployer.prototype._generateLambdaCf = function(taggedLambdaPkgs, lambdaRoleArn)
Object.keys(existingTemplate.Resources).forEach(function(resource) {
if (!lambdaCf.Resources[resource] && allLambdaNames.indexOf(resource) != -1) {
utils.jawsDebug('deploy_lambda', 'Adding exsiting lambda ' + resource);
utils.jawsDebug('Adding exsiting lambda ' + resource);
lambdaCf.Resources[resource] = existingTemplate.Resources[resource];
}
});
@ -391,7 +391,7 @@ Deployer.prototype._generateLambdaCf = function(taggedLambdaPkgs, lambdaRoleArn)
'lambdas-cf.json'
);
utils.jawsDebug('deploy_lambda', 'Wrting to ' + lambdasCfPath);
utils.jawsDebug('Wrting to ' + lambdasCfPath);
return utils.writeFile(lambdasCfPath, JSON.stringify(lambdaCfTemplate, null, 2))
.then(function() {
@ -423,7 +423,7 @@ Packager.prototype.createLambdaName = function() {
var _this = this,
name = utils.generateLambdaName(_this._awsmJson);
utils.jawsDebug('deploy_lambda', 'computed lambdaName: ' + name);
utils.jawsDebug('computed lambdaName: ' + name);
return name;
};
@ -508,9 +508,9 @@ Packager.prototype._createDistFolder = Promise.method(function() {
}
);
utils.jawsDebug('deploy_lambda', 'Packaging stage & region:');
utils.jawsDebug('deploy_lambda', _this._stage);
utils.jawsDebug('deploy_lambda', _this._region);
utils.jawsDebug('Packaging stage & region:');
utils.jawsDebug(_this._stage);
utils.jawsDebug(_this._region);
// Get ENV file from S3
return _this._JAWS.getEnvFile(_this._region, _this._stage)

View File

@ -80,10 +80,10 @@ CMD.prototype._installCore = Promise.method(function() {
if (!!jawsCoreName) {
var existingJawsCorePath = path.join(_this._JAWS._meta.projectRootPath, 'back', 'aws_modules', jawsCoreName);
utils.jawsDebug('module_create', 'Looking for existing jaws core in ' + existingJawsCorePath);
utils.jawsDebug('Looking for existing jaws core in ' + existingJawsCorePath);
if (!utils.dirExistsSync(existingJawsCorePath)) {
utils.jawsDebug('module_create', jawsCoreName + ' DNE, installing');
utils.jawsDebug(jawsCoreName + ' DNE, installing');
return modInstall.install(_this._JAWS, 'https://github.com/jaws-framework/' + jawsCoreName, true, false);
}
}

View File

@ -113,7 +113,7 @@ CMD.prototype._downloadMod = Promise.method(function() {
return temp.mkdirAsync('awsm')
.then(function(tempDirPath) {
return new Promise(function(resolve, reject) {
utils.jawsDebug('module_install', 'Downloading awsm to ' + tempDirPath);
utils.jawsDebug('Downloading awsm to ' + tempDirPath);
JawsCLI.log('Downloading aws-module ...');
spinner.start();

View File

@ -34,7 +34,7 @@ Promise.promisifyAll(fs);
*/
module.exports.run = function(name, stage, s3Bucket, domain, region, notificationEmail, profile, noCf) {
utils.jawsDebug('new_project', 'Running new project:', name);
utils.jawsDebug('Running new project:', name);
var command = new CMD(
name,
stage,
@ -123,7 +123,7 @@ CMD.prototype.run = Promise.method(function() {
CMD.prototype._prompt = Promise.method(function() {
utils.jawsDebug('new_project', 'Prompting for new project information');
utils.jawsDebug('Prompting for new project information');
var _this = this;
var nameDescription = 'Enter a project name: ' + os.EOL;

View File

@ -84,7 +84,7 @@ module.exports.profilesMap = function() {
* @param secretKey
*/
module.exports.profilesSet = function(awsProfile, awsRegion, accessKeyId, secretKey) {
utils.jawsDebug('utils_aws', 'Setting new AWS profile:', awsProfile);
utils.jawsDebug('Setting new AWS profile:', awsProfile);
var configDir = this.getConfigDir(),
credsPath = path.join(configDir, 'credentials'),
configPath = path.join(configDir, 'config');
@ -695,7 +695,7 @@ exports.getEnvFile = function(awsProfile, awsRegion, bucketName, projectName, st
Key: key,
};
utils.jawsDebug('utils_aws', 'env var s3 key: ' + key);
utils.jawsDebug('env var s3 key: ' + key);
return this.getS3Object(awsProfile, awsRegion, params);
};
@ -741,7 +741,7 @@ exports.putLambdaZip = function(awsProfile, awsRegion, bucketName, projectName,
Body: zipBuffer,
};
utils.jawsDebug('utils_aws', 'lambda zip s3 key: ' + key);
utils.jawsDebug('lambda zip s3 key: ' + key);
return this.putS3Object(awsProfile, awsRegion, params)
.then(function() {

View File

@ -139,7 +139,7 @@ Select._clear = function() {
// Private: Close
Select._close = function(cb) {
utils.jawsDebug('utils_cli', 'Closing select listener');
utils.jawsDebug('Closing select listener');
var _this = this;
process.stdin.pause();

View File

@ -176,26 +176,6 @@ exports.findAllAwsmJsons = function(startPath) {
});
};
/**
* Write to console.log if process.env.JAWS_VERBOSE is true
*
* If we ever want to get more complicated with log levels we should use winston
*
* @param str
*/
var debuggerCache = {};
exports.jawsDebug = function(context) {
if (process.env.DEBUG) {
context = 'jaws:' + context;
if (!debuggerCache[context]) {
debuggerCache[context] = rawDebug(context);
}
debuggerCache[context].apply(null, Array.prototype.slice.call(arguments, 1));
}
};
/**
* Writes file and makes any parent dirs if necessary
*
@ -204,7 +184,7 @@ exports.jawsDebug = function(context) {
* @returns {Promise}
*/
exports.writeFile = function(filePath, contents) {
this.jawsDebug('util', 'Writing file:', filePath);
this.jawsDebug('Writing file:', filePath);
if (contents === undefined) {
contents = '';
@ -336,3 +316,82 @@ exports.generateResourcesCf = function(projRootPath, projName, stage, region, no
JSON.stringify(cfTemplate, null, 2)
);
};
/**
* Write to console.log if process.env.JAWS_VERBOSE is true
*
* If we ever want to get more complicated with log levels we should use winston
*
* @param str
*/
var debuggerCache = {};
exports.jawsDebugWithContext = function(context) {
if (process.env.DEBUG) {
context = 'jaws:' + context;
if (!debuggerCache[context]) {
debuggerCache[context] = rawDebug(context);
}
debuggerCache[context].apply(null, Array.prototype.slice.call(arguments, 1));
}
};
exports.jawsDebug = function() {
if (process.env.DEBUG) {
var caller = getCaller();
var context = pathToContext(caller);
var args = Array.prototype.slice.call(arguments);
args.unshift(context);
this.jawsDebugWithContext.apply(this, args);
}
};
function pathToContext(path) {
// Match files under lib, tests, or bin so we only report the
// relevant part of the file name as the context
var pathRegex = /\/((lib|tests|bin)\/.*?)\.js$/i;
var match = pathRegex.exec(path);
if (match.length >= 2) {
return match[1].replace(/[\/\\]/g, '.');
} else {
return path;
}
}
function getCaller() {
var stack = getStack();
// Remove unwanted function calls on stack -- ourselves and our caller
stack.shift();
stack.shift();
// Now the top of the stack is the CallSite we want
// See this for available methods:
// https://code.google.com/p/v8-wiki/wiki/JavaScriptStackTraceApi
var path = stack[0].getFileName();
return path;
}
function getStack() {
// Save original Error.prepareStackTrace
var origPrepareStackTrace = Error.prepareStackTrace;
// Override with function that just returns `stack`
Error.prepareStackTrace = function (_, stack) {
return stack;
};
var err = new Error();
// Get `err.stack`, which calls our new `Error.prepareStackTrace`
var stack = err.stack;
// Restore original `Error.prepareStackTrace`
Error.prepareStackTrace = origPrepareStackTrace;
// Remove ourselves from the stack
stack.shift();
return stack;
}