Skip to content

Commit

Permalink
Update json logger in gatsby-cli to TS (#24140)
Browse files Browse the repository at this point in the history
* change json logger to ts

* refactor: make sanitizeAction clean

* 🔨 refactor onLogAction to export function

* 🚑 linting and object spreading
  • Loading branch information
wiput1999 authored May 19, 2020
1 parent ba02866 commit 16a8228
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
18 changes: 0 additions & 18 deletions packages/gatsby-cli/src/reporter/loggers/json/index.js

This file was deleted.

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

const isStringPayload = (action: ActionsUnion): action is ISetStatus =>
typeof action.payload === `string`

const sanitizeAction = (action: ActionsUnion): ActionsUnion => {
const copiedAction = cloneDeep(action)

if (isStringPayload(copiedAction)) {
return copiedAction
}

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

return copiedAction
}

export function initializeJSONLogger(): void {
onLogAction((action: ActionsUnion) => {
const sanitizedAction = sanitizeAction(action)

process.stdout.write(JSON.stringify(sanitizedAction) + `\n`)
})
}

0 comments on commit 16a8228

Please sign in to comment.