Skip to content

Commit d515044

Browse files
authored
fix(gatsby-cli): fix pagetree global cli (#33200)
1 parent df36732 commit d515044

File tree

16 files changed

+122
-44
lines changed

16 files changed

+122
-44
lines changed

packages/gatsby-cli/src/reporter/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export enum Actions {
33
SetStatus = `SET_STATUS`,
44
Log = `LOG`,
55
SetLogs = `SET_LOGS`,
6+
RenderPageTree = `RENDER_PAGE_TREE`,
67

78
StartActivity = `ACTIVITY_START`,
89
EndActivity = `ACTIVITY_END`,

packages/gatsby-cli/src/reporter/loggers/ink/components/develop.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ const ConnectedDevelop: React.FC = () => {
8383

8484
return (
8585
<Develop
86+
// @ts-ignore - program exists on state but we should refactor this
8687
pagesCount={state.pages?.size || 0}
88+
// @ts-ignore - program exists on state but we should refactor this
8789
appName={state.program?.sitePackageJson.name || ``}
8890
status={state.logs?.status || ``}
8991
/>

packages/gatsby-cli/src/reporter/loggers/ink/components/pageTree.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
12
import React, { ReactElement, useContext } from "react"
23
import { Box, Text, BoxProps, Spacer } from "ink"
34
import path from "path"
@@ -114,16 +115,17 @@ const ConnectedPageTree: React.FC = function ConnectedPageTree() {
114115
const state = useContext(StoreStateContext)
115116

116117
const componentWithPages = new Map<string, IComponentWithPageModes>()
117-
for (const { componentPath, pages } of state.components.values()) {
118+
119+
for (const { componentPath, pages } of state.pageTree!.components.values()) {
118120
const pagesByMode = {
119121
SSG: new Set<string>(),
120122
DSG: new Set<string>(),
121123
SSR: new Set<string>(),
122124
FN: new Set<string>(),
123125
}
124126
pages.forEach(pagePath => {
125-
const gatsbyPage = state.pages.get(pagePath)
126-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
127+
const gatsbyPage = state.pageTree!.pages.get(pagePath)
128+
127129
pagesByMode[gatsbyPage!.mode].add(pagePath)
128130
})
129131

@@ -133,7 +135,7 @@ const ConnectedPageTree: React.FC = function ConnectedPageTree() {
133135
for (const {
134136
originalAbsoluteFilePath,
135137
functionRoute,
136-
} of state.functions.values()) {
138+
} of state.pageTree!.functions.values()) {
137139
componentWithPages.set(originalAbsoluteFilePath, {
138140
SSG: new Set<string>(),
139141
DSG: new Set<string>(),
@@ -143,7 +145,7 @@ const ConnectedPageTree: React.FC = function ConnectedPageTree() {
143145
}
144146

145147
return (
146-
<PageTree components={componentWithPages} root={state.program.directory} />
148+
<PageTree components={componentWithPages} root={state.pageTree!.root} />
147149
)
148150
}
149151

packages/gatsby-cli/src/reporter/loggers/ink/context.tsx

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
import React, { useState, useLayoutEffect, createContext } from "react"
22
import { getStore, onLogAction } from "../../redux"
3-
// TODO remove and copy types
4-
import { IGatsbyState } from "gatsby/src/redux/types"
3+
import { IGatsbyCLIState } from "../../redux/types"
4+
import { IRenderPageArgs } from "../../types"
55

6-
// 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
7-
// but then quickly swaps it out with the store from the installed gatsby. This would benefit from a refactor later on
8-
// to not use it's own store temporarily.
9-
// By the time this is actually running, it will become an `IGatsbyState`
10-
const StoreStateContext = createContext<IGatsbyState>(
11-
getStore().getState() as any as IGatsbyState
12-
)
6+
const StoreStateContext = createContext<{
7+
logs: IGatsbyCLIState
8+
pageTree: IRenderPageArgs | null
9+
}>(getStore().getState())
1310

1411
export const StoreStateProvider: React.FC = ({
1512
children,
1613
}): React.ReactElement => {
17-
const [state, setState] = useState(
18-
getStore().getState() as any as IGatsbyState
19-
)
14+
const [state, setState] = useState(getStore().getState())
2015

2116
useLayoutEffect(
2217
() =>
2318
onLogAction(() => {
24-
setState(getStore().getState() as any as IGatsbyState)
19+
setState(getStore().getState())
2520
}),
2621
[]
2722
)

packages/gatsby-cli/src/reporter/loggers/ink/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import CLI from "./cli"
66
const ConnectedCLI: React.FC = (): React.ReactElement => {
77
const state = useContext(StoreStateContext)
88
const showStatusBar =
9+
// @ts-ignore - program exists on state but we should refactor this
910
state.program?._?.[0] === `develop` &&
11+
// @ts-ignore - program exists on state but we should refactor this
1012
state.program?.status === `BOOTSTRAP_FINISHED`
11-
const showPageTree =
12-
state.program?._?.[0] === `build` &&
13-
state.logs.messages.find(message => message?.text?.includes(`onPostBuild`))
13+
const showPageTree = !!state.pageTree
1414

1515
return (
1616
<CLI

packages/gatsby-cli/src/reporter/loggers/yurnalist/index.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path"
2-
import { getStore, onLogAction } from "../../redux"
2+
import { onLogAction } from "../../redux"
33
import {
44
Actions,
55
LogLevels,
@@ -16,8 +16,7 @@ import {
1616
generatePageTree,
1717
IComponentWithPageModes,
1818
} from "../../../util/generate-page-tree"
19-
// TODO remove and copy types
20-
import { IGatsbyState } from "gatsby/src/redux/types"
19+
import { IRenderPageArgs } from "../../types"
2120

2221
interface IYurnalistActivities {
2322
[activityId: string]: {
@@ -28,11 +27,11 @@ interface IYurnalistActivities {
2827
}
2928
}
3029

31-
function generatePageTreeToConsole(yurnalist: any): void {
32-
const state = getStore().getState() as IGatsbyState
33-
34-
// TODO use program
35-
const root = state.program.directory
30+
function generatePageTreeToConsole(
31+
yurnalist: any,
32+
state: IRenderPageArgs
33+
): void {
34+
const root = state.root
3635
const componentWithPages = new Map<string, IComponentWithPageModes>()
3736
for (const { componentPath, pages } of state.components.values()) {
3837
const pagesByMode = {
@@ -159,10 +158,6 @@ export function initializeYurnalistLogger(): void {
159158
yurnalistMethod(message)
160159
}
161160

162-
if (action.payload.text?.includes(`onPostBuild`)) {
163-
generatePageTreeToConsole(yurnalist)
164-
}
165-
166161
break
167162
}
168163
case Actions.StartActivity: {
@@ -252,6 +247,12 @@ export function initializeYurnalistLogger(): void {
252247
activity.end()
253248
delete activities[action.payload.id]
254249
}
250+
break
251+
}
252+
253+
case Actions.RenderPageTree: {
254+
generatePageTreeToConsole(yurnalist, action.payload)
255+
break
255256
}
256257
}
257258
})

packages/gatsby-cli/src/reporter/redux/actions.ts

+2
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ export const setActivityTotal =
2727
boundActions.setActivityTotal as typeof actions.setActivityTotal
2828
export const activityTick =
2929
boundActions.activityTick as typeof actions.activityTick
30+
export const renderPageTree =
31+
boundActions.renderPageTree as typeof actions.renderPageTree

packages/gatsby-cli/src/reporter/redux/index.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import { createStore, combineReducers, Store } from "redux"
2-
import { reducer } from "./reducer"
2+
import { reducer as logsReducer } from "./reducers/logs"
3+
import { reducer as pageTreeReducer } from "./reducers/page-tree"
34
import { ActionsUnion, ISetLogs, IGatsbyCLIState } from "./types"
45
import { isInternalAction } from "./utils"
56
import { createStructuredLoggingDiagnosticsMiddleware } from "./diagnostics"
67
import { Actions } from "../constants"
8+
import { IRenderPageArgs } from "../../reporter/types"
79

8-
let store: Store<{ logs: IGatsbyCLIState }> = createStore(
9-
combineReducers({
10-
logs: reducer,
11-
}),
12-
{}
13-
)
10+
let store: Store<{ logs: IGatsbyCLIState; pageTree: IRenderPageArgs }> =
11+
createStore(
12+
combineReducers({
13+
logs: logsReducer,
14+
pageTree: pageTreeReducer,
15+
}),
16+
{}
17+
)
1418

1519
const diagnosticsMiddleware =
1620
createStructuredLoggingDiagnosticsMiddleware(store)

packages/gatsby-cli/src/reporter/redux/internal-actions.ts

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
IActivityErrored,
2222
IGatsbyCLIState,
2323
ISetLogs,
24+
IRenderPageTree,
2425
} from "./types"
2526
import {
2627
delayedCall,
@@ -30,6 +31,7 @@ import {
3031
} from "./utils"
3132
import { IStructuredError } from "../../structured-errors/types"
3233
import { ErrorCategory } from "../../structured-errors/error-map"
34+
import { IRenderPageArgs } from "../types"
3335

3436
const ActivityStatusToLogLevel = {
3537
[ActivityStatuses.Interrupted]: ActivityLogLevels.Interrupted,
@@ -379,3 +381,10 @@ export const setLogs = (logs: IGatsbyCLIState): ISetLogs => {
379381
payload: logs,
380382
}
381383
}
384+
385+
export const renderPageTree = (payload: IRenderPageArgs): IRenderPageTree => {
386+
return {
387+
type: Actions.RenderPageTree,
388+
payload,
389+
}
390+
}

packages/gatsby-cli/src/reporter/redux/reducer.ts renamed to packages/gatsby-cli/src/reporter/redux/reducers/logs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ActionsUnion, IGatsbyCLIState, ISetLogs } from "./types"
2-
import { Actions } from "../constants"
1+
import { ActionsUnion, IGatsbyCLIState, ISetLogs } from "../types"
2+
import { Actions } from "../../constants"
33

44
export const reducer = (
55
state: IGatsbyCLIState = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ActionsUnion } from "../types"
2+
import { IRenderPageArgs } from "../../types"
3+
import { Actions } from "../../constants"
4+
5+
export const reducer = (
6+
state: IRenderPageArgs | null = null,
7+
action: ActionsUnion
8+
): IRenderPageArgs | null => {
9+
switch (action.type) {
10+
case Actions.RenderPageTree: {
11+
return action.payload
12+
}
13+
}
14+
15+
return state
16+
}

packages/gatsby-cli/src/reporter/redux/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Actions, ActivityStatuses, ActivityTypes } from "../constants"
22
import { IStructuredError } from "../../structured-errors/types"
33
import { ErrorCategory } from "../../structured-errors/error-map"
4+
import { IRenderPageArgs } from "../types"
45

56
export interface IGatsbyCLIState {
67
messages: Array<ILog>
@@ -20,6 +21,7 @@ export type ActionsUnion =
2021
| IUpdateActivity
2122
| IActivityErrored
2223
| ISetLogs
24+
| IRenderPageTree
2325

2426
export interface IActivity {
2527
startTime?: [number, number]
@@ -125,3 +127,8 @@ export interface ISetLogs {
125127
type: Actions.SetLogs
126128
payload: IGatsbyCLIState
127129
}
130+
131+
export interface IRenderPageTree {
132+
type: Actions.RenderPageTree
133+
payload: IRenderPageArgs
134+
}

packages/gatsby-cli/src/reporter/reporter.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import { IConstructError, IStructuredError } from "../structured-errors/types"
1313
import { createTimerReporter, ITimerReporter } from "./reporter-timer"
1414
import { createPhantomReporter, IPhantomReporter } from "./reporter-phantom"
1515
import { createProgressReporter, IProgressReporter } from "./reporter-progress"
16-
import { ErrorMeta, CreateLogAction, ILogIntent } from "./types"
16+
import {
17+
ErrorMeta,
18+
CreateLogAction,
19+
ILogIntent,
20+
IRenderPageArgs,
21+
} from "./types"
1722

1823
const errorFormatter = getErrorFormatter()
1924
const tracer = globalTracer()
@@ -342,6 +347,10 @@ class Reporter {
342347
}
343348
})
344349
}
350+
351+
_renderPageTree(args: IRenderPageArgs): void {
352+
reporterActions.renderPageTree(args)
353+
}
345354
}
346355
export type { Reporter }
347356
export const reporter = new Reporter()

packages/gatsby-cli/src/reporter/types.ts

+21
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ export interface ILogIntent {
5858
}
5959
}
6060

61+
type PageMode = "SSG" | "DSG" | "SSR"
62+
63+
interface IGatsbyPageComponent {
64+
componentPath: string
65+
pages: Set<string>
66+
}
67+
interface IGatsbyPage {
68+
mode: PageMode
69+
}
70+
interface IGatsbyFunction {
71+
functionRoute: string
72+
originalAbsoluteFilePath: string
73+
}
74+
75+
export interface IRenderPageArgs {
76+
pages: Map<string, IGatsbyPage>
77+
components: Map<string, IGatsbyPageComponent>
78+
functions: Array<IGatsbyFunction>
79+
root: string
80+
}
81+
6182
export type ReporterMessagesFromChild = ILogIntent
6283

6384
export { ActionCreators }

packages/gatsby/src/commands/build.ts

+9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { createGraphqlEngineBundle } from "../schema/graphql-engine/bundle-webpa
5151
import { createPageSSRBundle } from "../utils/page-ssr-module/bundle-webpack"
5252
import { shouldGenerateEngines } from "../utils/engines-helpers"
5353
import uuidv4 from "uuid/v4"
54+
import reporter from "gatsby-cli/lib/reporter"
5455

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

316317
await Promise.all([waitForCompilerClose, waitForCompilerCloseBuildHtml])
317318

319+
const state = store.getState()
320+
reporter._renderPageTree({
321+
components: state.components,
322+
functions: state.functions,
323+
pages: state.pages,
324+
root: state.program.directory,
325+
})
326+
318327
report.info(`Done building in ${process.uptime()} sec`)
319328

320329
buildSpan.finish()

packages/gatsby/src/redux/reducers/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { nodesReducer } from "./nodes"
2-
import { reducer as logReducer } from "gatsby-cli/lib/reporter/redux/reducer"
2+
import { reducer as logReducer } from "gatsby-cli/lib/reporter/redux/reducers/logs"
33
import { pagesReducer } from "./pages"
44
import { redirectsReducer } from "./redirects"
55
import { schemaReducer } from "./schema"

0 commit comments

Comments
 (0)