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

Ts migration/convert yurnalist logger #24224

Merged
merged 9 commits into from
May 20, 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 jest-transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = require(`babel-jest`).createTransformer({
overrides: [
...(babelPreset.overrides || []),
{
test: `**/*.ts`,
test: [`**/*.ts`, `**/*.tsx`],
plugins: [[`@babel/plugin-transform-typescript`, { isTSX: true }]],
},
],
Expand Down
12 changes: 7 additions & 5 deletions packages/gatsby-cli/src/reporter/loggers/ink/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ const ConnectedCLI: React.FC = (): React.ReactElement => {
return <CLI showStatusBar={Boolean(showStatusBar)} logs={state.logs} />
}

render(
<StoreStateProvider>
<ConnectedCLI />
</StoreStateProvider>
)
export function initializeINKLogger(): void {
render(
<StoreStateProvider>
<ConnectedCLI />
</StoreStateProvider>
)
}
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/reporter/loggers/ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const sanitizeAction = (action: ActionsUnion): ActionsUnion => {
return copiedAction
}

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

Expand Down
125 changes: 0 additions & 125 deletions packages/gatsby-cli/src/reporter/loggers/yurnalist/index.js

This file was deleted.

138 changes: 138 additions & 0 deletions packages/gatsby-cli/src/reporter/loggers/yurnalist/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { onLogAction } from "../../redux"
import {
Actions,
LogLevels,
ActivityLogLevels,
ActivityTypes,
} from "../../constants"

import { createReporter } from "yurnalist"
import ProgressBar from "progress"
import chalk from "chalk"
import { IUpdateActivity } from "../../redux/types"

interface IYurnalistActivities {
[activityId: string]: {
text: string | undefined
statusText: string | undefined
update(payload: IUpdateActivity["payload"]): void
end(): void
}
}

export function initializeYurnalistLogger(): void {
const activities: IYurnalistActivities = {}
const yurnalist = createReporter({ emoji: true, verbose: true })

const levelToYurnalist = {
[LogLevels.Log]: yurnalist.log.bind(yurnalist),
[LogLevels.Warning]: yurnalist.warn.bind(yurnalist),
[LogLevels.Error]: yurnalist.error.bind(yurnalist),
[LogLevels.Info]: yurnalist.info.bind(yurnalist),
[LogLevels.Success]: yurnalist.success.bind(yurnalist),
[ActivityLogLevels.Success]: yurnalist.success.bind(yurnalist),
[ActivityLogLevels.Failed]: (text: string): void => {
yurnalist.log(`${chalk.red(`failed`)} ${text}`)
},
[ActivityLogLevels.Interrupted]: (text: string): void => {
yurnalist.log(`${chalk.gray(`not finished`)} ${text}`)
},
}

onLogAction(action => {
switch (action.type) {
case Actions.Log: {
const yurnalistMethod = levelToYurnalist[action.payload.level]
if (!yurnalistMethod) {
process.stdout.write(`NO "${action.payload.level}" method\n`)
} else {
let message = action.payload.text
if (action.payload.duration) {
message += ` - ${action.payload.duration.toFixed(3)}s`
}
if (action.payload.statusText) {
message += ` - ${action.payload.statusText}`
}
yurnalistMethod(message)
}
break
}
case Actions.StartActivity: {
if (action.payload.type === ActivityTypes.Spinner) {
const spinner = yurnalist.activity()
spinner.tick(action.payload.text)

const activity = {
text: action.payload.text,
statusText: action.payload.statusText,
update(payload: any): void {
// TODO: I'm not convinced that this is ever called with a text property.
// From searching the codebase it appears that we do not ever assign a text
// property during the IUpdateActivity action.
if (payload.text) {
activity.text = payload.text
}
if (payload.statusText) {
activity.statusText = payload.statusText
}

let message = activity.text
if (activity.statusText) {
message += ` - ${activity.statusText}`
}

message += ` id:"${action.payload.id}"`

spinner.tick(message)
},
end(): void {
spinner.end()
},
}
activities[action.payload.id] = activity
} else if (action.payload.type === ActivityTypes.Progress) {
const bar = new ProgressBar(
` [:bar] :current/:total :elapsed s :percent ${action.payload.text}`,
{
total: action.payload.total,
curr: action.payload.current,
width: 30,
clear: true,
}
)

activities[action.payload.id] = {
text: undefined,
statusText: undefined,
update(payload): void {
if (payload.total) {
bar.total = payload.total
}
if (payload.current) {
bar.curr = payload.current
}

bar.tick(0)
},
end(): void {},
}
}
break
}
case Actions.UpdateActivity: {
const activity = activities[action.payload.id]
if (activity) {
activity.update(action.payload)
}
break
}
case Actions.EndActivity: {
const activity = activities[action.payload.id]
if (activity) {
activity.end()
delete activities[action.payload.id]
}
}
}
})
}
13 changes: 8 additions & 5 deletions packages/gatsby-cli/src/reporter/start-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
*/
import semver from "semver"
import { isCI } from "gatsby-core-utils"
import { ipcLogger } from "./loggers/ipc"
import { initializeIPCLogger } from "./loggers/ipc"
import { initializeJSONLogger } from "./loggers/json"
import { initializeYurnalistLogger } from "./loggers/yurnalist"
import { initializeINKLogger } from "./loggers/ink"

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

ipcLogger()
initializeIPCLogger()
}

if (process.env.GATSBY_LOGGER.includes(`json`)) {
require(`./loggers/json`)
initializeJSONLogger()
} else if (process.env.GATSBY_LOGGER.includes(`yurnalist`)) {
require(`./loggers/yurnalist`)
initializeYurnalistLogger()
} else {
require(`./loggers/ink`)
initializeINKLogger()
}
}