Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: message map #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 50 additions & 24 deletions src/outputProcessor/HtmlOutputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const COLORS = {

type Colors = ValueOf<typeof COLORS>;

const MessageConfigMap: Record<
const getMessageConfigMap = (
options: PluginOptions
): Record<
LogType,
(options: PluginOptions) => {
{
typeColor: Colors;
icon: LogSymbols;
/**
Expand All @@ -30,43 +32,64 @@ const MessageConfigMap: Record<
*/
trim?: number;
}
> = {
[LOG_TYPES.PLUGIN_LOG_TYPE]: () => ({typeColor: COLORS.DARK_CYAN, icon: LOG_SYMBOLS.INFO}),
[LOG_TYPES.BROWSER_CONSOLE_WARN]: () => ({typeColor: COLORS.YELLOW, icon: LOG_SYMBOLS.WARNING}),
[LOG_TYPES.BROWSER_CONSOLE_ERROR]: () => ({typeColor: COLORS.RED, icon: LOG_SYMBOLS.ERROR}),
[LOG_TYPES.BROWSER_CONSOLE_DEBUG]: () => ({typeColor: COLORS.BLUE, icon: LOG_SYMBOLS.DEBUG}),
[LOG_TYPES.BROWSER_CONSOLE_INFO]: () => ({typeColor: COLORS.DARK_CYAN, icon: LOG_SYMBOLS.INFO}),
[LOG_TYPES.BROWSER_CONSOLE_LOG]: () => ({typeColor: COLORS.DARK_CYAN, icon: LOG_SYMBOLS.INFO}),
[LOG_TYPES.CYPRESS_LOG]: () => ({typeColor: COLORS.DARK_CYAN, icon: LOG_SYMBOLS.INFO}),
[LOG_TYPES.CYPRESS_XHR]: (options) => ({
> => ({
[LOG_TYPES.PLUGIN_LOG_TYPE]: {
typeColor: COLORS.DARK_CYAN,
icon: LOG_SYMBOLS.INFO,
},
[LOG_TYPES.BROWSER_CONSOLE_WARN]: {
typeColor: COLORS.YELLOW,
icon: LOG_SYMBOLS.WARNING,
},
[LOG_TYPES.BROWSER_CONSOLE_ERROR]: {
typeColor: COLORS.RED,
icon: LOG_SYMBOLS.ERROR,
},
[LOG_TYPES.BROWSER_CONSOLE_DEBUG]: {
typeColor: COLORS.BLUE,
icon: LOG_SYMBOLS.DEBUG,
},
[LOG_TYPES.BROWSER_CONSOLE_INFO]: {
typeColor: COLORS.DARK_CYAN,
icon: LOG_SYMBOLS.INFO,
},
[LOG_TYPES.BROWSER_CONSOLE_LOG]: {
typeColor: COLORS.DARK_CYAN,
icon: LOG_SYMBOLS.INFO,
},
[LOG_TYPES.CYPRESS_LOG]: {
typeColor: COLORS.DARK_CYAN,
icon: LOG_SYMBOLS.INFO,
},
[LOG_TYPES.CYPRESS_XHR]: {
typeColor: COLORS.LIGHT_GREY,
icon: LOG_SYMBOLS.ROUTE,
messageColor: COLORS.LIGHT_GREY,
trim: options.routeTrimLength,
}),
[LOG_TYPES.CYPRESS_FETCH]: (options) => ({
},
[LOG_TYPES.CYPRESS_FETCH]: {
typeColor: COLORS.GREEN,
icon: LOG_SYMBOLS.ROUTE,
trim: options.routeTrimLength,
messageColor: COLORS.GREY,
}),
[LOG_TYPES.CYPRESS_INTERCEPT]: (options) => ({
},
[LOG_TYPES.CYPRESS_INTERCEPT]: {
typeColor: COLORS.GREEN,
icon: LOG_SYMBOLS.ROUTE,
messageColor: COLORS.GREY,
trim: options.routeTrimLength,
}),
[LOG_TYPES.CYPRESS_REQUEST]: (options) => ({
},
[LOG_TYPES.CYPRESS_REQUEST]: {
typeColor: COLORS.GREEN,
icon: LOG_SYMBOLS.SUCCESS,
messageColor: COLORS.GREY,
trim: options.routeTrimLength,
}),
[LOG_TYPES.CYPRESS_COMMAND]: () => ({
},
[LOG_TYPES.CYPRESS_COMMAND]: {
typeColor: COLORS.GREEN,
icon: LOG_SYMBOLS.SUCCESS,
}),
};
},
});

// https://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript
export function escapeHtml(html: string) {
Expand All @@ -86,10 +109,13 @@ export function escapeHtml(html: string) {
* - Apply proper spacing, newlines, and HTML syntax.
*/
export function formatMessage({type, message, severity}: Log, options: PluginOptions) {
const messageConfig = MessageConfigMap[type](options);

let {
typeColor,
icon,
messageColor,
trim = options.defaultTrimLength,
} = getMessageConfigMap(options)[type];
let processedMessage = message;
let {typeColor, icon, messageColor, trim = options.defaultTrimLength} = messageConfig;

if (severity === 'error') {
typeColor = COLORS.RED;
Expand Down
60 changes: 31 additions & 29 deletions src/outputProcessor/consoleProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,70 @@ const LOG_SYMBOLS = (() =>

const BOLD_COLORS: Colors[] = [COLORS.RED, COLORS.YELLOW];

const TYPE_COMPUTE: Record<
const getTypeConfigMap = (
options: PluginOptions
): Record<
LogType,
(options: PluginOptions) => {
{
icon: LogSymbols;
color: Colors;
trim?: number;
}
> = {
[LOG_TYPES.PLUGIN_LOG_TYPE]: () => ({
> => ({
[LOG_TYPES.PLUGIN_LOG_TYPE]: {
color: COLORS.WHITE,
icon: '-',
}),
[LOG_TYPES.BROWSER_CONSOLE_WARN]: () => ({
},
[LOG_TYPES.BROWSER_CONSOLE_WARN]: {
color: COLORS.YELLOW,
icon: LOG_SYMBOLS.WARNING,
}),
[LOG_TYPES.BROWSER_CONSOLE_ERROR]: () => ({
},
[LOG_TYPES.BROWSER_CONSOLE_ERROR]: {
color: COLORS.RED,
icon: LOG_SYMBOLS.WARNING,
}),
[LOG_TYPES.BROWSER_CONSOLE_DEBUG]: () => ({
},
[LOG_TYPES.BROWSER_CONSOLE_DEBUG]: {
color: COLORS.BLUE,
icon: LOG_SYMBOLS.DEBUG,
}),
[LOG_TYPES.BROWSER_CONSOLE_LOG]: () => ({
},
[LOG_TYPES.BROWSER_CONSOLE_LOG]: {
color: COLORS.WHITE,
icon: LOG_SYMBOLS.INFO,
}),
[LOG_TYPES.BROWSER_CONSOLE_INFO]: () => ({
},
[LOG_TYPES.BROWSER_CONSOLE_INFO]: {
color: COLORS.WHITE,
icon: LOG_SYMBOLS.INFO,
}),
[LOG_TYPES.CYPRESS_LOG]: () => ({
},
[LOG_TYPES.CYPRESS_LOG]: {
color: COLORS.GREEN,
icon: LOG_SYMBOLS.INFO,
}),
[LOG_TYPES.CYPRESS_XHR]: (options) => ({
},
[LOG_TYPES.CYPRESS_XHR]: {
color: COLORS.GREEN,
icon: LOG_SYMBOLS.ROUTE,
trim: options.routeTrimLength,
}),
[LOG_TYPES.CYPRESS_FETCH]: (options) => ({
},
[LOG_TYPES.CYPRESS_FETCH]: {
color: COLORS.GREEN,
icon: LOG_SYMBOLS.ROUTE,
trim: options.routeTrimLength,
}),
[LOG_TYPES.CYPRESS_INTERCEPT]: (options) => ({
},
[LOG_TYPES.CYPRESS_INTERCEPT]: {
color: COLORS.GREEN,
icon: LOG_SYMBOLS.ROUTE,
trim: options.routeTrimLength,
}),
[LOG_TYPES.CYPRESS_REQUEST]: (options) => ({
},
[LOG_TYPES.CYPRESS_REQUEST]: {
color: COLORS.GREEN,
icon: LOG_SYMBOLS.SUCCESS,
trim: options.routeTrimLength,
}),
[LOG_TYPES.CYPRESS_COMMAND]: (options) => ({
},
[LOG_TYPES.CYPRESS_COMMAND]: {
color: COLORS.GREEN,
icon: LOG_SYMBOLS.SUCCESS,
trim: options.routeTrimLength,
}),
};
},
});

const TYPE_STRING_CACHE: Record<string, string> = {};

Expand Down Expand Up @@ -112,7 +114,7 @@ function consoleProcessor(messages: Log[], options: PluginOptions, data: Message

messages.forEach(({type, message, severity, timeString}) => {
let processedMessage = message;
let {color, icon, trim = options.defaultTrimLength} = TYPE_COMPUTE[type](options);
let {color, icon, trim = options.defaultTrimLength} = getTypeConfigMap(options)[type];

if (severity === CONSTANTS.SEVERITY.ERROR) {
color = COLORS.RED;
Expand Down