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(gatsby-cli): migrate loggers/ipc to typescript #23960

Merged
merged 4 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 0 additions & 40 deletions packages/gatsby-cli/src/reporter/loggers/ipc/index.js

This file was deleted.

57 changes: 57 additions & 0 deletions packages/gatsby-cli/src/reporter/loggers/ipc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { onLogAction } from "../../redux/index"
import { ISetStatus, ActionsUnion } from "../../redux/types"
import { Actions, LogLevels } from "../../constants"
import stripAnsi from "strip-ansi"

const isISetStatus = (action: ActionsUnion): action is ISetStatus =>
typeof action.payload === `string`
anoriqq marked this conversation as resolved.
Show resolved Hide resolved

/**
* Payload can either be a String or an Object
* See more at integration-tests/structured-logging/__tests__/to-do.js
*/
const sanitizeAction = (action: ActionsUnion): ActionsUnion => {
if (isISetStatus(action)) {
return action
}

if (`text` in action.payload && action.payload.text) {
action.payload.text = stripAnsi(action.payload.text)
}
if (`statusText` in action.payload && action.payload.statusText) {
action.payload.statusText = stripAnsi(action.payload.statusText)
}

return action
anoriqq marked this conversation as resolved.
Show resolved Hide resolved
}

export const ipcLogger = (): void => {
onLogAction((action: ActionsUnion) => {
if (!process.send) return

const sanitizedAction = sanitizeAction(action)

// we mutate sanitizedAction but this is already deep copy of action so we should be good
if (sanitizedAction.type === Actions.Log) {
// Don't emit Debug over IPC
if (
[LogLevels.Debug].includes(sanitizedAction.payload.level as LogLevels)
) {
return
}
// Override Success and Log types to Info over IPC
if (
[LogLevels.Success, LogLevels.Log].includes(
sanitizedAction.payload.level as LogLevels
)
) {
sanitizedAction.payload.level = LogLevels.Info
}
}

process.send({
type: Actions.LogAction,
action: sanitizedAction,
})
})
}
3 changes: 2 additions & 1 deletion packages/gatsby-cli/src/reporter/start-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import semver from "semver"
import { isCI } from "gatsby-core-utils"
import { ipcLogger } from "./loggers/ipc"

export const startLogger = (): void => {
let inkExists = false
Expand All @@ -27,7 +28,7 @@ export const startLogger = (): void => {
if (process.send) {
// process.env.FORCE_COLOR = `0`

require(`./loggers/ipc`)
ipcLogger()
}

if (process.env.GATSBY_LOGGER.includes(`json`)) {
Expand Down