improve log output of emit

This commit is contained in:
Nik Graf 2017-08-11 07:33:42 +02:00
parent 65ce09bad5
commit 3e8d8477eb
No known key found for this signature in database
GPG Key ID: 7A97512F916175F1
2 changed files with 27 additions and 9 deletions

View File

@ -5,6 +5,15 @@ const fdk = require('@serverless/fdk');
const path = require('path');
const stdin = require('get-stdin');
const userStats = require('../../utils/userStats');
const chalk = require('chalk');
const prettifyValue = value => {
const prettified = JSON.stringify(value, null, 2).replace(
new RegExp('\\n', 'g'),
`\n ${chalk.yellow('|')} `
);
return ` ${chalk.yellow('|')} ${prettified}`;
};
class Emit {
constructor(serverless, options) {
@ -114,17 +123,20 @@ class Emit {
if (this.options.datatype) {
emitParams.dataType = this.options.datatype;
}
const prefix = chalk.yellow(' Serverless | ');
return eventGateway
.emit(emitParams)
.then(() => {
const msg = `Successfully emitted the event ${name} as datatype ${emitParams.dataType ||
'application/json'} with:`;
this.serverless.cli.consoleLog(`${msg}\n${JSON.stringify(data)}`);
const msg = `${prefix}Emitted the event ${name} as datatype ${emitParams.dataType ||
'application/json'}:`;
this.serverless.cli.consoleLog(`${msg}`);
this.serverless.cli.consoleLog(prettifyValue(data));
})
.catch(() => {
const msg = `Failed to emit the event ${name} as datatype ${emitParams.dataType ||
'application/json'} with:`;
this.serverless.cli.consoleLog(`${msg}\n${JSON.stringify(data)}`);
const msg = `${prefix}Failed to emit the event ${name} as datatype ${emitParams.dataType ||
'application/json'}:`;
this.serverless.cli.consoleLog(`${msg}`);
this.serverless.cli.consoleLog(prettifyValue(data));
throw new Error(`Failed to emit the event ${name}`);
});
}

View File

@ -6,11 +6,13 @@ const caret = colorText('|');
const ignoredMessage = ['Running in development mode with embedded etcd.'];
const prettifyValue = value =>
JSON.stringify(value, null, 2).replace(
const prettifyValue = value => {
const prettified = JSON.stringify(value, null, 2).replace(
new RegExp('\\n', 'g'),
`\n ${caret} `
);
return ` ${caret} ${prettified}`;
};
const transformMessages = parsedMsg => {
if (parsedMsg.msg === 'Function registered.') {
@ -22,11 +24,15 @@ const transformMessages = parsedMsg => {
const text = `Received event ${event.event}\n`;
const dataTypeText = ` ${caret} dataType: ${event.dataType}\n`;
const ppData = prettifyValue(event.data);
const dataText = ` ${caret} data:\n ${caret} ${ppData}`;
const dataText = ` ${caret} data:\n${ppData}`;
return `${text}${dataTypeText}${dataText}\n`;
} else if (parsedMsg.msg === 'Function invoked.') {
const text = `Invoked function ${parsedMsg.functionId}\n`;
return `${text}`;
} else if (parsedMsg.msg === 'Function invocation failed.') {
const text = `Failed to invoke function ${parsedMsg.functionId}\n`;
const errorText = ` ${caret} error: ${parsedMsg.error}\n`;
return `${text}${errorText}`;
}
return `${parsedMsg.msg.trim()}\n`;
};