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 error component to typescript #22098

Merged
merged 2 commits into from
Mar 11, 2020
Merged
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
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/reporter/loggers/ink/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Spinner from "../ink/components/spinner"
import { ProgressBar } from "../ink/components/progress-bar"

import { Message } from "../ink/components/messages"
import Error from "./components/error"
import { Error } from "./components/error"
import Develop from "../ink/components/develop"

const showProgress = isTTY()
Expand Down
80 changes: 0 additions & 80 deletions packages/gatsby-cli/src/reporter/loggers/ink/components/error.js

This file was deleted.

99 changes: 99 additions & 0 deletions packages/gatsby-cli/src/reporter/loggers/ink/components/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React, { FunctionComponent } from "react"
import path from "path"
import { Color, Box } from "ink"
import { get } from "lodash"

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

let locString = ``
if (typeof lineNumber !== `undefined`) {
locString += `:${lineNumber}`
const columnNumber = get(location, `start.column`)
if (typeof columnNumber !== `undefined`) {
locString += `:${columnNumber}`
}
}

return (
<Color blue>
{path.relative(process.cwd(), filePath)}
{locString}
</Color>
)
}

interface IDocsLinkProps {
docsUrl: string
}
const DocsLink: FunctionComponent<IDocsLinkProps> = ({ docsUrl }) => {
// TODO: when there's no specific docsUrl, add helpful message describing how
// to submit an issue
if (docsUrl === `https://gatsby.dev/issue-how-to`) return null
blainekasten marked this conversation as resolved.
Show resolved Hide resolved
return (
<Box marginTop={1}>
See our docs page for more info on this error: {docsUrl}
</Box>
)
}

interface IErrorProps {
details: {
level: string
code?: string
type?: string
text: string
filePath?: string
location: string
docsUrl: string
}
}

export const Error: FunctionComponent<IErrorProps> = React.memo(
({ details }) => (
// const stackLength = get(details, `stack.length`, 0

<Box marginY={1} flexDirection="column">
<Box flexDirection="column">
<Box flexDirection="column">
<Box>
<Box marginRight={1}>
<Color black bgRed>
{` ${details.level} `}
{details.code ? `#${details.code} ` : ``}
</Color>
<Color red>{details.type ? ` ` + details.type : ``}</Color>
</Box>
</Box>
<Box marginTop={1}>{details.text}</Box>
{details.filePath && (
<Box marginTop={1}>
File:{` `}
<File filePath={details.filePath} location={details.location} />
</Box>
)}
</Box>
<DocsLink docsUrl={details.docsUrl} />
</Box>
{/* TODO: use this to replace errorFormatter.render in reporter.error func
{stackLength > 0 && (
<Box>
<Color>
<Box flexDirection="column">
<Box>Error stack:</Box>
{details.stack.map((item, id) => (
<Box key={id}>
{item.fileName && `${item.fileName} line ${item.lineNumber}`}
</Box>
))}
</Box>
</Color>
</Box>
)} */}
</Box>
)
)