Skip to content

Commit

Permalink
fix(gatsby-cli): fix pagetree global cli (#33200)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Sep 15, 2021
1 parent df36732 commit d515044
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 44 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-cli/src/reporter/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum Actions {
SetStatus = `SET_STATUS`,
Log = `LOG`,
SetLogs = `SET_LOGS`,
RenderPageTree = `RENDER_PAGE_TREE`,

StartActivity = `ACTIVITY_START`,
EndActivity = `ACTIVITY_END`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ const ConnectedDevelop: React.FC = () => {

return (
<Develop
// @ts-ignore - program exists on state but we should refactor this
pagesCount={state.pages?.size || 0}
// @ts-ignore - program exists on state but we should refactor this
appName={state.program?.sitePackageJson.name || ``}
status={state.logs?.status || ``}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import React, { ReactElement, useContext } from "react"
import { Box, Text, BoxProps, Spacer } from "ink"
import path from "path"
Expand Down Expand Up @@ -114,16 +115,17 @@ const ConnectedPageTree: React.FC = function ConnectedPageTree() {
const state = useContext(StoreStateContext)

const componentWithPages = new Map<string, IComponentWithPageModes>()
for (const { componentPath, pages } of state.components.values()) {

for (const { componentPath, pages } of state.pageTree!.components.values()) {
const pagesByMode = {
SSG: new Set<string>(),
DSG: new Set<string>(),
SSR: new Set<string>(),
FN: new Set<string>(),
}
pages.forEach(pagePath => {
const gatsbyPage = state.pages.get(pagePath)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const gatsbyPage = state.pageTree!.pages.get(pagePath)

pagesByMode[gatsbyPage!.mode].add(pagePath)
})

Expand All @@ -133,7 +135,7 @@ const ConnectedPageTree: React.FC = function ConnectedPageTree() {
for (const {
originalAbsoluteFilePath,
functionRoute,
} of state.functions.values()) {
} of state.pageTree!.functions.values()) {
componentWithPages.set(originalAbsoluteFilePath, {
SSG: new Set<string>(),
DSG: new Set<string>(),
Expand All @@ -143,7 +145,7 @@ const ConnectedPageTree: React.FC = function ConnectedPageTree() {
}

return (
<PageTree components={componentWithPages} root={state.program.directory} />
<PageTree components={componentWithPages} root={state.pageTree!.root} />
)
}

Expand Down
21 changes: 8 additions & 13 deletions packages/gatsby-cli/src/reporter/loggers/ink/context.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import React, { useState, useLayoutEffect, createContext } from "react"
import { getStore, onLogAction } from "../../redux"
// TODO remove and copy types
import { IGatsbyState } from "gatsby/src/redux/types"
import { IGatsbyCLIState } from "../../redux/types"
import { IRenderPageArgs } from "../../types"

// 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
)
const StoreStateContext = createContext<{
logs: IGatsbyCLIState
pageTree: IRenderPageArgs | null
}>(getStore().getState())

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

useLayoutEffect(
() =>
onLogAction(() => {
setState(getStore().getState() as any as IGatsbyState)
setState(getStore().getState())
}),
[]
)
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby-cli/src/reporter/loggers/ink/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import CLI from "./cli"
const ConnectedCLI: React.FC = (): React.ReactElement => {
const state = useContext(StoreStateContext)
const showStatusBar =
// @ts-ignore - program exists on state but we should refactor this
state.program?._?.[0] === `develop` &&
// @ts-ignore - program exists on state but we should refactor this
state.program?.status === `BOOTSTRAP_FINISHED`
const showPageTree =
state.program?._?.[0] === `build` &&
state.logs.messages.find(message => message?.text?.includes(`onPostBuild`))
const showPageTree = !!state.pageTree

return (
<CLI
Expand Down
25 changes: 13 additions & 12 deletions packages/gatsby-cli/src/reporter/loggers/yurnalist/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path"
import { getStore, onLogAction } from "../../redux"
import { onLogAction } from "../../redux"
import {
Actions,
LogLevels,
Expand All @@ -16,8 +16,7 @@ import {
generatePageTree,
IComponentWithPageModes,
} from "../../../util/generate-page-tree"
// TODO remove and copy types
import { IGatsbyState } from "gatsby/src/redux/types"
import { IRenderPageArgs } from "../../types"

interface IYurnalistActivities {
[activityId: string]: {
Expand All @@ -28,11 +27,11 @@ interface IYurnalistActivities {
}
}

function generatePageTreeToConsole(yurnalist: any): void {
const state = getStore().getState() as IGatsbyState

// TODO use program
const root = state.program.directory
function generatePageTreeToConsole(
yurnalist: any,
state: IRenderPageArgs
): void {
const root = state.root
const componentWithPages = new Map<string, IComponentWithPageModes>()
for (const { componentPath, pages } of state.components.values()) {
const pagesByMode = {
Expand Down Expand Up @@ -159,10 +158,6 @@ export function initializeYurnalistLogger(): void {
yurnalistMethod(message)
}

if (action.payload.text?.includes(`onPostBuild`)) {
generatePageTreeToConsole(yurnalist)
}

break
}
case Actions.StartActivity: {
Expand Down Expand Up @@ -252,6 +247,12 @@ export function initializeYurnalistLogger(): void {
activity.end()
delete activities[action.payload.id]
}
break
}

case Actions.RenderPageTree: {
generatePageTreeToConsole(yurnalist, action.payload)
break
}
}
})
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-cli/src/reporter/redux/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ export const setActivityTotal =
boundActions.setActivityTotal as typeof actions.setActivityTotal
export const activityTick =
boundActions.activityTick as typeof actions.activityTick
export const renderPageTree =
boundActions.renderPageTree as typeof actions.renderPageTree
18 changes: 11 additions & 7 deletions packages/gatsby-cli/src/reporter/redux/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { createStore, combineReducers, Store } from "redux"
import { reducer } from "./reducer"
import { reducer as logsReducer } from "./reducers/logs"
import { reducer as pageTreeReducer } from "./reducers/page-tree"
import { ActionsUnion, ISetLogs, IGatsbyCLIState } from "./types"
import { isInternalAction } from "./utils"
import { createStructuredLoggingDiagnosticsMiddleware } from "./diagnostics"
import { Actions } from "../constants"
import { IRenderPageArgs } from "../../reporter/types"

let store: Store<{ logs: IGatsbyCLIState }> = createStore(
combineReducers({
logs: reducer,
}),
{}
)
let store: Store<{ logs: IGatsbyCLIState; pageTree: IRenderPageArgs }> =
createStore(
combineReducers({
logs: logsReducer,
pageTree: pageTreeReducer,
}),
{}
)

const diagnosticsMiddleware =
createStructuredLoggingDiagnosticsMiddleware(store)
Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby-cli/src/reporter/redux/internal-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
IActivityErrored,
IGatsbyCLIState,
ISetLogs,
IRenderPageTree,
} from "./types"
import {
delayedCall,
Expand All @@ -30,6 +31,7 @@ import {
} from "./utils"
import { IStructuredError } from "../../structured-errors/types"
import { ErrorCategory } from "../../structured-errors/error-map"
import { IRenderPageArgs } from "../types"

const ActivityStatusToLogLevel = {
[ActivityStatuses.Interrupted]: ActivityLogLevels.Interrupted,
Expand Down Expand Up @@ -379,3 +381,10 @@ export const setLogs = (logs: IGatsbyCLIState): ISetLogs => {
payload: logs,
}
}

export const renderPageTree = (payload: IRenderPageArgs): IRenderPageTree => {
return {
type: Actions.RenderPageTree,
payload,
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ActionsUnion, IGatsbyCLIState, ISetLogs } from "./types"
import { Actions } from "../constants"
import { ActionsUnion, IGatsbyCLIState, ISetLogs } from "../types"
import { Actions } from "../../constants"

export const reducer = (
state: IGatsbyCLIState = {
Expand Down
16 changes: 16 additions & 0 deletions packages/gatsby-cli/src/reporter/redux/reducers/page-tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ActionsUnion } from "../types"
import { IRenderPageArgs } from "../../types"
import { Actions } from "../../constants"

export const reducer = (
state: IRenderPageArgs | null = null,
action: ActionsUnion
): IRenderPageArgs | null => {
switch (action.type) {
case Actions.RenderPageTree: {
return action.payload
}
}

return state
}
7 changes: 7 additions & 0 deletions packages/gatsby-cli/src/reporter/redux/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Actions, ActivityStatuses, ActivityTypes } from "../constants"
import { IStructuredError } from "../../structured-errors/types"
import { ErrorCategory } from "../../structured-errors/error-map"
import { IRenderPageArgs } from "../types"

export interface IGatsbyCLIState {
messages: Array<ILog>
Expand All @@ -20,6 +21,7 @@ export type ActionsUnion =
| IUpdateActivity
| IActivityErrored
| ISetLogs
| IRenderPageTree

export interface IActivity {
startTime?: [number, number]
Expand Down Expand Up @@ -125,3 +127,8 @@ export interface ISetLogs {
type: Actions.SetLogs
payload: IGatsbyCLIState
}

export interface IRenderPageTree {
type: Actions.RenderPageTree
payload: IRenderPageArgs
}
11 changes: 10 additions & 1 deletion packages/gatsby-cli/src/reporter/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import { IConstructError, IStructuredError } from "../structured-errors/types"
import { createTimerReporter, ITimerReporter } from "./reporter-timer"
import { createPhantomReporter, IPhantomReporter } from "./reporter-phantom"
import { createProgressReporter, IProgressReporter } from "./reporter-progress"
import { ErrorMeta, CreateLogAction, ILogIntent } from "./types"
import {
ErrorMeta,
CreateLogAction,
ILogIntent,
IRenderPageArgs,
} from "./types"

const errorFormatter = getErrorFormatter()
const tracer = globalTracer()
Expand Down Expand Up @@ -342,6 +347,10 @@ class Reporter {
}
})
}

_renderPageTree(args: IRenderPageArgs): void {
reporterActions.renderPageTree(args)
}
}
export type { Reporter }
export const reporter = new Reporter()
21 changes: 21 additions & 0 deletions packages/gatsby-cli/src/reporter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ export interface ILogIntent {
}
}

type PageMode = "SSG" | "DSG" | "SSR"

interface IGatsbyPageComponent {
componentPath: string
pages: Set<string>
}
interface IGatsbyPage {
mode: PageMode
}
interface IGatsbyFunction {
functionRoute: string
originalAbsoluteFilePath: string
}

export interface IRenderPageArgs {
pages: Map<string, IGatsbyPage>
components: Map<string, IGatsbyPageComponent>
functions: Array<IGatsbyFunction>
root: string
}

export type ReporterMessagesFromChild = ILogIntent

export { ActionCreators }
9 changes: 9 additions & 0 deletions packages/gatsby/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { createGraphqlEngineBundle } from "../schema/graphql-engine/bundle-webpa
import { createPageSSRBundle } from "../utils/page-ssr-module/bundle-webpack"
import { shouldGenerateEngines } from "../utils/engines-helpers"
import uuidv4 from "uuid/v4"
import reporter from "gatsby-cli/lib/reporter"

module.exports = async function build(program: IBuildArgs): Promise<void> {
// global gatsby object to use without store
Expand Down Expand Up @@ -315,6 +316,14 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {

await Promise.all([waitForCompilerClose, waitForCompilerCloseBuildHtml])

const state = store.getState()
reporter._renderPageTree({
components: state.components,
functions: state.functions,
pages: state.pages,
root: state.program.directory,
})

report.info(`Done building in ${process.uptime()} sec`)

buildSpan.finish()
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/reducers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { nodesReducer } from "./nodes"
import { reducer as logReducer } from "gatsby-cli/lib/reporter/redux/reducer"
import { reducer as logReducer } from "gatsby-cli/lib/reporter/redux/reducers/logs"
import { pagesReducer } from "./pages"
import { redirectsReducer } from "./redirects"
import { schemaReducer } from "./schema"
Expand Down

0 comments on commit d515044

Please sign in to comment.