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

chore(cli): migrate messages to typescript #22084

Merged
merged 8 commits into from
Mar 10, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const Actions = {
PendingActivity: `ACTIVITY_PENDING`,
CancelActivity: `ACTIVITY_CANCEL`,
ActivityErrored: `ACTIVITY_ERRORED`,
}
} as const

export const LogLevels = {
Debug: `DEBUG`,
Expand All @@ -19,13 +19,13 @@ export const LogLevels = {
Warning: `WARNING`,
Log: `LOG`,
Error: `ERROR`,
}
} as const

export const ActivityLogLevels = {
Success: `ACTIVITY_SUCCESS`,
Failed: `ACTIVITY_FAILED`,
Interrupted: `ACTIVITY_INTERRUPTED`,
}
} as const

export const ActivityStatuses = {
InProgress: `IN_PROGRESS`,
Expand All @@ -34,11 +34,11 @@ export const ActivityStatuses = {
Failed: `FAILED`,
Success: `SUCCESS`,
Cancelled: `CANCELLED`,
}
} as const

export const ActivityTypes = {
Spinner: `spinner`,
Hidden: `hidden`,
Progress: `progress`,
Pending: `pending`,
}
} as const
cola119 marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from "react"
cola119 marked this conversation as resolved.
Show resolved Hide resolved
import { Box, Color, ColorProps } from "ink"

import { ActivityLogLevels, LogLevels } from "../../../constants"

const ColorSwitcher: React.FC<ColorProps> = ({ children, ...props }) => (
cola119 marked this conversation as resolved.
Show resolved Hide resolved
<Color {...props}>{children}</Color>
)

const createLabel = (text: string, color: string): React.FC<ColorProps> => (
cola119 marked this conversation as resolved.
Show resolved Hide resolved
...props
): JSX.Element => (
<ColorSwitcher {...{ [color]: true, ...props }}>{text}</ColorSwitcher>
)
cola119 marked this conversation as resolved.
Show resolved Hide resolved

type ValueOf<T> = T[keyof T]
cola119 marked this conversation as resolved.
Show resolved Hide resolved

const getLabel = (
level: ValueOf<typeof ActivityLogLevels> | ValueOf<typeof LogLevels>
): ReturnType<typeof createLabel> => {
switch (level) {
case ActivityLogLevels.Success:
case LogLevels.Success:
return createLabel(`success`, `green`)
case LogLevels.Warning:
return createLabel(`warn`, `yellow`)
case LogLevels.Debug:
return createLabel(`verbose`, `gray`)
case LogLevels.Info:
return createLabel(`info`, `blue`)
case ActivityLogLevels.Failed:
return createLabel(`failed`, `red`)
case ActivityLogLevels.Interrupted:
return createLabel(`not finished`, `gray`)

default:
return createLabel(level, `blue`)
}
}

interface IProps {
level: ValueOf<typeof ActivityLogLevels> | ValueOf<typeof LogLevels>
text: string
duration: number
statusText: string
}
export const Message = React.memo<IProps>(
({ level, text, duration, statusText }) => {
let message = text
if (duration) {
message += ` - ${duration.toFixed(3)}s`
}
if (statusText) {
message += ` - ${statusText}`
}
if (!level || level === `LOG`) {
return <>{message}</>
}

const TextLabel = getLabel(level)

return (
<Box textWrap="wrap" flexDirection="row">
<TextLabel />
{` `}
{message}
</Box>
)
}
)