Skip to content

Commit

Permalink
Clean up after merging the redux master changes
Browse files Browse the repository at this point in the history
  • Loading branch information
blainekasten committed Apr 21, 2020
1 parent 75f7fb6 commit b050220
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 81 deletions.
35 changes: 10 additions & 25 deletions packages/gatsby-cli/src/reporter/loggers/ink/cli.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,18 @@ import { Box, Static } from "ink"
import { isTTY } from "../../../util/is-tty"
import { trackBuildError } from "gatsby-telemetry"
import { Spinner } from "./components/spinner"
import { ProgressBar, IProgressbarProps } from "./components/progress-bar"
import { ProgressBar } from "./components/progress-bar"
import { Message, IMessageProps } from "./components/messages"
import { Error as ErrorComponent, IErrorDetails } from "./components/error"
import { Error as ErrorComponent } from "./components/error"
import Develop from "./components/develop"
import {
ActivityLogLevels,
ActivityStatuses,
ActivityTypes,
} from "../../constants"
import { IGatsbyCLIState, IActivity } from "../../redux/types"
import { ActivityLogLevels } from "../../constants"
import { IStructuredError } from "../../../structured-errors/types"

const showProgress = isTTY()

interface IActivity {
id: string
text: string
total: IProgressbarProps["total"]
current: IProgressbarProps["current"]
startTime: IProgressbarProps["startTime"]
status: ActivityStatuses
type: ActivityTypes
}

interface ICLIProps {
logs: {
messages: Array<IErrorDetails | IMessageProps>
activities: Record<string, IActivity>
}
logs: IGatsbyCLIState
showStatusBar: boolean
}

Expand Down Expand Up @@ -94,7 +79,7 @@ class CLI extends React.Component<ICLIProps, ICLIState> {
const msg = messages[index]
this.memoizedReactElementsForMessages.push(
msg.level === `ERROR` ? (
<ErrorComponent details={msg as IErrorDetails} key={index} />
<ErrorComponent details={msg as IStructuredError} key={index} />
) : (
<Message key={index} {...(msg as IMessageProps)} />
)
Expand Down Expand Up @@ -132,9 +117,9 @@ class CLI extends React.Component<ICLIProps, ICLIState> {
<ProgressBar
key={activity.id}
message={activity.text}
total={activity.total}
current={activity.current}
startTime={activity.startTime}
total={activity.total || 0}
current={activity.current || 0}
startTime={activity.startTime || [0, 0]}
/>
))}
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,10 @@ const useTerminalResize = (): Array<number> => {
return sizes
}

enum mapConstantToStatus {
IN_PROGRESS = `In Progress`,
NOT_STARTED = `Not Started`,
INTERRUPTED = `Interrupted`,
FAILED = `Failed`,
SUCCESS = `Success`,
CANCELLED = `Cancelled`,
}

interface IDevelopProps {
pagesCount: number
appName: string
status: keyof mapConstantToStatus
status: string
}

const Develop: React.FC<IDevelopProps> = ({ pagesCount, appName, status }) => {
Expand All @@ -43,7 +34,7 @@ const Develop: React.FC<IDevelopProps> = ({ pagesCount, appName, status }) => {
<Box height={1} flexDirection="row">
<Color>{pagesCount} pages</Color>
<Box flexGrow={1} />
<Color>{mapConstantToStatus[status]}</Color>
<Color>{status}</Color>
<Box flexGrow={1} />
<Color>{appName}</Color>
</Box>
Expand All @@ -56,9 +47,9 @@ const ConnectedDevelop: React.FC = () => {

return (
<Develop
pagesCount={state.pages ? state.pages.size : 0}
appName={state.program ? state.program.sitePackageJson.name || `` : ``}
status={state.logs ? state.logs.status : ``}
pagesCount={state.pages?.size || 0}
appName={state.program?.sitePackageJson.name || ``}
status={state.logs?.status || ``}
/>
)
}
Expand Down
24 changes: 8 additions & 16 deletions packages/gatsby-cli/src/reporter/loggers/ink/components/error.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import React, { FunctionComponent } from "react"
import path from "path"
import { Color, Box } from "ink"
import { get } from "lodash"
import { IStructuredError } from "../../../../structured-errors/types"

interface IFileProps {
filePath: string
location: string
location: IStructuredError["location"]
}

const File: FunctionComponent<IFileProps> = ({ filePath, location }) => {
const lineNumber = get(location, `start.line`)
const lineNumber = location?.start.line

let locString = ``
if (typeof lineNumber !== `undefined`) {
locString += `:${lineNumber}`
const columnNumber = get(location, `start.column`)
const columnNumber = location?.start.column
if (typeof columnNumber !== `undefined`) {
locString += `:${columnNumber}`
}
Expand All @@ -28,8 +29,9 @@ const File: FunctionComponent<IFileProps> = ({ filePath, location }) => {
}

interface IDocsLinkProps {
docsUrl: string
docsUrl: string | undefined
}

const DocsLink: FunctionComponent<IDocsLinkProps> = ({ docsUrl }) => {
// TODO: when there's no specific docsUrl, add helpful message describing how
// to submit an issue
Expand All @@ -41,18 +43,8 @@ const DocsLink: FunctionComponent<IDocsLinkProps> = ({ docsUrl }) => {
)
}

export interface IErrorDetails {
level: string
code?: string
type?: string
text: string
filePath?: string
location: string
docsUrl: string
}

export interface IErrorProps {
details: IErrorDetails
details: IStructuredError
}

export const Error: FunctionComponent<IErrorProps> = React.memo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface IMessageProps {
duration?: number
statusText?: string
}

export const Message = React.memo<IMessageProps>(
({ level, text, duration, statusText }) => {
let message = text
Expand Down
15 changes: 12 additions & 3 deletions packages/gatsby-cli/src/reporter/loggers/ink/context.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import React, { useState, useEffect, createContext } from "react"
import { getStore, onLogAction } from "../../redux"
import { IGatsbyState } from "gatsby/src/redux/types"

const StoreStateContext = createContext(getStore().getState())
// These weird castings we are doing in this file is because the way gatsby-cli works is that it starts with it's own store
// but then quickly swaps it out with the store from the installed gatsby. This would benefit from a refactor later on
// to not use it's own store temporarily.
// By the time this is actually running, it will become an `IGatsbyState`
const StoreStateContext = createContext<IGatsbyState>(
(getStore().getState() as any) as IGatsbyState
)

export const StoreStateProvider: React.FC = ({
children,
}): React.ReactElement => {
const [state, setState] = useState(getStore().getState())
const [state, setState] = useState(
(getStore().getState() as any) as IGatsbyState
)

useEffect(
() =>
onLogAction(() => {
setState(getStore().getState())
setState((getStore().getState() as any) as IGatsbyState)
}),
[]
)
Expand Down
6 changes: 2 additions & 4 deletions packages/gatsby-cli/src/reporter/loggers/ink/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import CLI from "./cli"
const ConnectedCLI: React.FC = (): React.ReactElement => {
const state = useContext(StoreStateContext)
const showStatusBar =
state.program &&
state.program._ &&
state.program._[0] === `develop` &&
state.program.status === `BOOTSTRAP_FINISHED`
state.program?._?.[0] === `develop` &&
state.program?.status === `BOOTSTRAP_FINISHED`

return <CLI showStatusBar={Boolean(showStatusBar)} logs={state.logs} />
}
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface ICert {
}

export interface IProgram {
_: `develop` | `build` | `clean` | `feedback` | `repl` | `serve`
status?: string // I think this type should not exist here. It seems to be added in the reducer, but not applicable to the caller site from gatsby-cli
useYarn: boolean
open: boolean
openTracingConfigFile: string
Expand Down
21 changes: 2 additions & 19 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { IProgram } from "../commands/types"
import { GraphQLFieldExtensionDefinition } from "../schema/extensions"
import { DocumentNode, GraphQLSchema } from "graphql"
import { SchemaComposer } from "graphql-compose"
import { IGatsbyCLIState } from "gatsby-cli/src/reporter/redux/types"

type SystemPath = string
type Identifier = string
type StructuredLog = any // TODO this should come from structured log interface

export interface IRedirect {
fromPath: string
Expand Down Expand Up @@ -177,24 +177,7 @@ export interface IGatsbyState {
types: any[] // TODO
}
themes: any // TODO
logs: {
messages: StructuredLog[]
activities: {
[key: string]: {
id: Identifier
uuid: Identifier
text: string
type: string // TODO make enum
status: string // TODO make enum
startTime: [number, number]
statusText: string
current: undefined | any // TODO
total: undefined | any // TODO
duration: number
}
}
status: string // TODO make enum
}
logs: IGatsbyCLIState
inferenceMetadata: {
step: string // TODO make enum or union
typeMap: {
Expand Down

0 comments on commit b050220

Please sign in to comment.