Skip to content

Commit

Permalink
refactor(CLI): Modern logs for invoke command
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Oct 4, 2021
1 parent 5c5eec9 commit 2af95c0
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/plugins/aws/invoke.js
Expand Up @@ -6,6 +6,7 @@ const validate = require('./lib/validate');
const stdin = require('get-stdin');
const formatLambdaLogEvent = require('./utils/formatLambdaLogEvent');
const ServerlessError = require('../../serverless-error');
const { legacy, writeText, style } = require('@serverless/utils/log');

class AwsInvoke {
constructor(serverless, options) {
Expand Down Expand Up @@ -106,25 +107,28 @@ class AwsInvoke {
if (invocationReply.Payload) {
const response = JSON.parse(invocationReply.Payload);

this.consoleLog(color(JSON.stringify(response, null, 4)));
legacy.consoleLog(color(JSON.stringify(response, null, 4)));
writeText(JSON.stringify(response, null, 4));
}

if (invocationReply.LogResult) {
this.consoleLog(
legacy.consoleLog(
chalk.gray('--------------------------------------------------------------------')
);
writeText(
style.aside('--------------------------------------------------------------------')
);
const logResult = Buffer.from(invocationReply.LogResult, 'base64').toString();
logResult.split('\n').forEach((line) => this.consoleLog(formatLambdaLogEvent(line)));
logResult.split('\n').forEach((line) => {
legacy.consoleLog(formatLambdaLogEvent(line));
writeText(formatLambdaLogEvent(line, { isModern: true }));
});
}

if (invocationReply.FunctionError) {
throw new ServerlessError('Invoked function failed', 'AWS_LAMBDA_INVOCATION_FAILED');
}
}

consoleLog(msg) {
process.stdout.write(`${msg}\n`);
}
}

module.exports = AwsInvoke;

0 comments on commit 2af95c0

Please sign in to comment.