Skip to content

Commit 4304629

Browse files
cola119MichaelDeBoey
andauthoredMar 10, 2020
chore(gatsby-cli): migrate messages to typescript (#22084)
* migrate constant.js to ts * migrate messages component and utils to typescript * change constant objects to enums * Update packages/gatsby-cli/src/reporter/loggers/ink/components/messages.tsx Co-Authored-By: Michaël De Boey <info@michaeldeboey.be> * Update packages/gatsby-cli/src/reporter/loggers/ink/components/messages.tsx Co-Authored-By: Michaël De Boey <info@michaeldeboey.be> * Update packages/gatsby-cli/src/reporter/loggers/ink/components/messages.tsx Co-Authored-By: Michaël De Boey <info@michaeldeboey.be> * formatted Co-authored-by: Michaël De Boey <info@michaeldeboey.be>
1 parent 94247c1 commit 4304629

File tree

4 files changed

+114
-100
lines changed

4 files changed

+114
-100
lines changed
 

‎packages/gatsby-cli/src/reporter/constants.js

-44
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
export enum Actions {
2+
LogAction = `LOG_ACTION`,
3+
SetStatus = `SET_STATUS`,
4+
Log = `LOG`,
5+
SetLogs = `SET_LOGS`,
6+
7+
StartActivity = `ACTIVITY_START`,
8+
EndActivity = `ACTIVITY_END`,
9+
UpdateActivity = `ACTIVITY_UPDATE`,
10+
PendingActivity = `ACTIVITY_PENDING`,
11+
CancelActivity = `ACTIVITY_CANCEL`,
12+
ActivityErrored = `ACTIVITY_ERRORED`,
13+
}
14+
15+
export enum LogLevels {
16+
Debug = `DEBUG`,
17+
Success = `SUCCESS`,
18+
Info = `INFO`,
19+
Warning = `WARNING`,
20+
Log = `LOG`,
21+
Error = `ERROR`,
22+
}
23+
24+
export enum ActivityLogLevels {
25+
Success = `ACTIVITY_SUCCESS`,
26+
Failed = `ACTIVITY_FAILED`,
27+
Interrupted = `ACTIVITY_INTERRUPTED`,
28+
}
29+
30+
export enum ActivityStatuses {
31+
InProgress = `IN_PROGRESS`,
32+
NotStarted = `NOT_STARTED`,
33+
Interrupted = `INTERRUPTED`,
34+
Failed = `FAILED`,
35+
Success = `SUCCESS`,
36+
Cancelled = `CANCELLED`,
37+
}
38+
39+
export enum ActivityTypes {
40+
Spinner = `spinner`,
41+
Hidden = `hidden`,
42+
Progress = `progress`,
43+
Pending = `pending`,
44+
}

‎packages/gatsby-cli/src/reporter/loggers/ink/components/messages.js

-56
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React, { FunctionComponent } from "react"
2+
import { Box, Color, ColorProps } from "ink"
3+
4+
import { ActivityLogLevels, LogLevels } from "../../../constants"
5+
6+
const ColorSwitcher: FunctionComponent<ColorProps> = ({
7+
children,
8+
...props
9+
}) => <Color {...props}>{children}</Color>
10+
11+
const createLabel = (
12+
text: string,
13+
color: string
14+
): FunctionComponent<ColorProps> => (...props): JSX.Element => (
15+
<ColorSwitcher {...{ [color]: true, ...props }}>{text}</ColorSwitcher>
16+
)
17+
18+
const getLabel = (
19+
level: ActivityLogLevels | LogLevels
20+
): ReturnType<typeof createLabel> => {
21+
switch (level) {
22+
case ActivityLogLevels.Success:
23+
case LogLevels.Success:
24+
return createLabel(`success`, `green`)
25+
case LogLevels.Warning:
26+
return createLabel(`warn`, `yellow`)
27+
case LogLevels.Debug:
28+
return createLabel(`verbose`, `gray`)
29+
case LogLevels.Info:
30+
return createLabel(`info`, `blue`)
31+
case ActivityLogLevels.Failed:
32+
return createLabel(`failed`, `red`)
33+
case ActivityLogLevels.Interrupted:
34+
return createLabel(`not finished`, `gray`)
35+
36+
default:
37+
return createLabel(level, `blue`)
38+
}
39+
}
40+
41+
interface IProps {
42+
level: ActivityLogLevels | LogLevels
43+
text: string
44+
duration: number
45+
statusText: string
46+
}
47+
export const Message = React.memo<IProps>(
48+
({ level, text, duration, statusText }) => {
49+
let message = text
50+
if (duration) {
51+
message += ` - ${duration.toFixed(3)}s`
52+
}
53+
if (statusText) {
54+
message += ` - ${statusText}`
55+
}
56+
if (!level || level === `LOG`) {
57+
return <>{message}</>
58+
}
59+
60+
const TextLabel = getLabel(level)
61+
62+
return (
63+
<Box textWrap="wrap" flexDirection="row">
64+
<TextLabel />
65+
{` `}
66+
{message}
67+
</Box>
68+
)
69+
}
70+
)

0 commit comments

Comments
 (0)
Please sign in to comment.