Skip to content

Commit

Permalink
fix(gatsby): Pass pluginName to reporter.activityTimer & `reporte…
Browse files Browse the repository at this point in the history
…r.createProgress` (#37710)
  • Loading branch information
LekoArts committed Mar 1, 2023
1 parent e58c180 commit 21e798b
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 16 deletions.
14 changes: 8 additions & 6 deletions packages/gatsby-cli/src/reporter/reporter-progress.ts
Expand Up @@ -13,17 +13,18 @@ interface ICreateProgressReporterArguments {
span: Span
reporter: typeof gatsbyReporter
reporterActions: typeof reporterActionsForTypes
pluginName?: string
}

export interface IProgressReporter {
start(): void
setStatus(statusText: string): void
tick(increment?: number): void
panicOnBuild(
arg: any,
...otherArgs: Array<any>
errorMeta: ErrorMeta,
error?: Error | Array<Error>
): IStructuredError | Array<IStructuredError>
panic(arg: any, ...otherArgs: Array<any>): void
panic(errorMeta: ErrorMeta, error?: Error | Array<Error>): never
end(): void
done(): void
total: number
Expand All @@ -38,6 +39,7 @@ export const createProgressReporter = ({
span,
reporter,
reporterActions,
pluginName,
}: ICreateProgressReporterArguments): IProgressReporter => {
let lastUpdateTime = 0
let unflushedProgress = 0
Expand Down Expand Up @@ -92,18 +94,18 @@ export const createProgressReporter = ({
id,
})

return reporter.panicOnBuild(errorMeta, error)
return reporter.panicOnBuild(errorMeta, error, pluginName)
},

panic(errorMeta: ErrorMeta, error?: Error | Array<Error>): void {
panic(errorMeta: ErrorMeta, error?: Error | Array<Error>): never {
span.finish()

reporterActions.endActivity({
id,
status: ActivityStatuses.Failed,
})

return reporter.panic(errorMeta, error)
return reporter.panic(errorMeta, error, pluginName)
},

end(): void {
Expand Down
14 changes: 8 additions & 6 deletions packages/gatsby-cli/src/reporter/reporter-timer.ts
Expand Up @@ -15,16 +15,17 @@ interface ICreateTimerReporterArguments {
span: Span
reporter: typeof gatsbyReporter
reporterActions: typeof reporterActionsForTypes
pluginName?: string
}

export interface ITimerReporter {
start(): void
setStatus(statusText: string): void
panicOnBuild(
arg: any,
...otherArgs: Array<any>
errorMeta: ErrorMeta,
error?: Error | Array<Error>
): IStructuredError | Array<IStructuredError>
panic(arg: any, ...otherArgs: Array<any>): void
panic(errorMeta: ErrorMeta, error?: Error | Array<Error>): never
end(): void
span: Span
}
Expand All @@ -35,6 +36,7 @@ export const createTimerReporter = ({
span,
reporter,
reporterActions,
pluginName,
}: ICreateTimerReporterArguments): ITimerReporter => {
return {
start(): void {
Expand Down Expand Up @@ -62,18 +64,18 @@ export const createTimerReporter = ({
id,
})

return reporter.panicOnBuild(errorMeta, error)
return reporter.panicOnBuild(errorMeta, error, pluginName)
},

panic(errorMeta: ErrorMeta, error?: Error | Array<Error>): void {
panic(errorMeta: ErrorMeta, error?: Error | Array<Error>): never {
span.finish()

reporterActions.endActivity({
id,
status: ActivityStatuses.Failed,
})

return reporter.panic(errorMeta, error)
return reporter.panic(errorMeta, error, pluginName)
},

end(): void {
Expand Down
8 changes: 6 additions & 2 deletions packages/gatsby-cli/src/reporter/reporter.ts
Expand Up @@ -244,7 +244,8 @@ class Reporter {
*/
activityTimer = (
text: string,
activityArgs: IActivityArgs = {}
activityArgs: IActivityArgs = {},
pluginName?: string
): ITimerReporter => {
let { parentSpan, id, tags } = activityArgs
const spanArgs = parentSpan ? { childOf: parentSpan, tags } : { tags }
Expand All @@ -260,6 +261,7 @@ class Reporter {
span,
reporter: this,
reporterActions,
pluginName,
})
}

Expand Down Expand Up @@ -295,7 +297,8 @@ class Reporter {
text: string,
total = 0,
start = 0,
activityArgs: IActivityArgs = {}
activityArgs: IActivityArgs = {},
pluginName?: string
): IProgressReporter => {
let { parentSpan, id, tags } = activityArgs
const spanArgs = parentSpan ? { childOf: parentSpan, tags } : { tags }
Expand All @@ -312,6 +315,7 @@ class Reporter {
span,
reporter: this,
reporterActions,
pluginName,
})
}

Expand Down
60 changes: 60 additions & 0 deletions packages/gatsby/src/utils/__tests__/api-runner-node.js
Expand Up @@ -21,10 +21,12 @@ jest.mock(`../get-cache`, () => {

const start = jest.fn()
const end = jest.fn()
const panicOnBuild = jest.fn()

const mockActivity = {
start,
end,
panicOnBuild,
done: end,
}

Expand Down Expand Up @@ -205,4 +207,62 @@ describe(`api-runner-node`, () => {
},
})
})

it(`setErrorMap works with activityTimer`, async () => {
store.getState.mockImplementation(() => {
return {
program: {},
config: {},
flattenedPlugins: [
{
name: `test-plugin-activity-map`,
resolve: path.join(fixtureDir, `test-plugin-activity-map`),
nodeAPIs: [`onPreInit`],
},
],
}
})
await apiRunnerNode(`onPreInit`)
expect(reporter.setErrorMap).toBeCalledTimes(1)
expect(panicOnBuild).toBeCalledTimes(1)
expect(reporter.activityTimer.mock.calls[3]).toEqual([
`Test Activity`,
{},
`test-plugin-activity-map`,
])
expect(panicOnBuild.mock.calls[0][0]).toEqual({
id: `1337`,
context: { someProp: `Naruto` },
})
})

it(`setErrorMap works with createProgress`, async () => {
store.getState.mockImplementation(() => {
return {
program: {},
config: {},
flattenedPlugins: [
{
name: `test-plugin-progress-map`,
resolve: path.join(fixtureDir, `test-plugin-progress-map`),
nodeAPIs: [`onPreInit`],
},
],
}
})
await apiRunnerNode(`onPreInit`)
expect(reporter.setErrorMap).toBeCalledTimes(1)
expect(reporter.createProgress).toBeCalledTimes(4)
expect(reporter.createProgress.mock.calls[3]).toEqual([
`Test Progress`,
0,
0,
{},
`test-plugin-progress-map`,
])
expect(panicOnBuild.mock.calls[0][0]).toEqual({
id: `1337`,
context: { someProp: `Naruto` },
})
})
})
@@ -0,0 +1,17 @@
exports.onPreInit = ({ reporter }) => {
reporter.setErrorMap({
1337: {
text: context => `Error text is ${context.someProp}`,
level: `ERROR`,
docsUrl: `https://www.gatsbyjs.com/docs/gatsby-cli/#new`,
},
})

const activity = reporter.activityTimer(`Test Activity`)
activity.start()

activity.panicOnBuild({
id: `1337`,
context: { someProp: `Naruto` },
})
}
@@ -0,0 +1,17 @@
exports.onPreInit = ({ reporter }) => {
reporter.setErrorMap({
1337: {
text: context => `Error text is ${context.someProp}`,
level: `ERROR`,
docsUrl: `https://www.gatsbyjs.com/docs/gatsby-cli/#new`,
},
})

const activity = reporter.createProgress(`Test Progress`)
activity.start()

activity.panicOnBuild({
id: `1337`,
context: { someProp: `Naruto` },
})
}
17 changes: 15 additions & 2 deletions packages/gatsby/src/utils/api-runner-node.js
Expand Up @@ -222,7 +222,14 @@ function extendLocalReporterToCatchPluginErrors({
error,
panic,
panicOnBuild,
activityTimer: (...args) => {
// If you change arguments here, update reporter.ts as well
activityTimer: (text, activityArgs = {}) => {
let args = [text, activityArgs]

if (pluginName && setErrorMap) {
args = [...args, pluginName]
}

// eslint-disable-next-line prefer-spread
const activity = reporter.activityTimer.apply(reporter, args)

Expand All @@ -241,8 +248,14 @@ function extendLocalReporterToCatchPluginErrors({

return activity
},
// If you change arguments here, update reporter.ts as well
createProgress: (text, total = 0, start = 0, activityArgs = {}) => {
let args = [text, total, start, activityArgs]

if (pluginName && setErrorMap) {
args = [...args, pluginName]
}

createProgress: (...args) => {
// eslint-disable-next-line prefer-spread
const activity = reporter.createProgress.apply(reporter, args)

Expand Down

0 comments on commit 21e798b

Please sign in to comment.