Skip to content

Commit 0ac577c

Browse files
andrewnesterpieh
authored andcommitted
chore: use panicOnBuild instead of process.exit (#9031)
* Use panicOnBuild instead process.exit * Added test for bad exports handling
1 parent c222e94 commit 0ac577c

File tree

6 files changed

+23
-47
lines changed

6 files changed

+23
-47
lines changed

packages/gatsby/src/bootstrap/get-config-file.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,20 @@ module.exports = async function getConfigFile(
3232
})
3333
)
3434
if (!testRequireError(configPath, err)) {
35-
report.error(
35+
report.panic(
3636
`We encountered an error while trying to load your site's ${configName}. Please fix the error and try again.`,
3737
err
3838
)
39-
process.exit(1)
4039
} else if (nearMatch) {
41-
console.log(``)
42-
report.error(
40+
report.panic(
4341
`It looks like you were trying to add the config file? Please rename "${chalk.bold(
4442
nearMatch
4543
)}" to "${chalk.bold(configName)}"`
4644
)
47-
console.log(``)
48-
process.exit(1)
4945
} else if (existsSync(path.join(rootDir, `src`, configName))) {
50-
console.log(``)
51-
report.error(
46+
report.panic(
5247
`Your ${configName} file is in the wrong place. You've placed it in the src/ directory. It must instead be at the root of your site next to your package.json file.`
5348
)
54-
console.log(``)
55-
process.exit(1)
5649
}
5750
}
5851

packages/gatsby/src/bootstrap/load-plugins/__tests__/validate.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
jest.mock(`gatsby-cli/lib/reporter`, () => {
2+
return {
3+
panicOnBuild: jest.fn(),
4+
warn: jest.fn(),
5+
}
6+
})
17
jest.mock(`../../resolve-module-exports`)
28

9+
const reporter = require(`gatsby-cli/lib/reporter`)
310
const {
411
collatePluginAPIs,
512
handleBadExports,
@@ -81,7 +88,7 @@ describe(`collatePluginAPIs`, () => {
8188

8289
describe(`handleBadExports`, () => {
8390
it(`Does nothing when there are no bad exports`, async () => {
84-
const result = handleBadExports({
91+
handleBadExports({
8592
apis: {
8693
node: [`these`, `can`, `be`],
8794
browser: [`anything`, `as there`],
@@ -93,12 +100,10 @@ describe(`handleBadExports`, () => {
93100
ssr: [],
94101
},
95102
})
96-
97-
expect(result).toEqual(false)
98103
})
99104

100-
it(`Returns true and logs a message when bad exports are detected`, async () => {
101-
const result = handleBadExports({
105+
it(`Calls reporter.panicOnBuild when bad exports are detected`, async () => {
106+
handleBadExports({
102107
apis: {
103108
node: [``],
104109
browser: [``],
@@ -115,8 +120,8 @@ describe(`handleBadExports`, () => {
115120
],
116121
},
117122
})
118-
// TODO: snapshot console.log()'s from handleBadExports?
119-
expect(result).toEqual(true)
123+
124+
expect(reporter.panicOnBuild.mock.calls.length).toBe(1)
120125
})
121126
})
122127

packages/gatsby/src/bootstrap/load-plugins/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ module.exports = async (config = {}) => {
5252
const badExports = x.badExports
5353

5454
// Show errors for any non-Gatsby APIs exported from plugins
55-
const isBad = handleBadExports({ apis, badExports })
56-
if (isBad && process.env.NODE_ENV === `production`) process.exit(1) // TODO: change to panicOnBuild
55+
handleBadExports({ apis, badExports })
5756

5857
// Show errors when ReplaceRenderer has been implemented multiple times
5958
flattenedPlugins = handleMultipleReplaceRenderers({

packages/gatsby/src/bootstrap/load-plugins/validate.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,12 @@ const getBadExportsMessage = (badExports, exportType, apis) => {
8989

9090
const handleBadExports = ({ apis, badExports }) => {
9191
// Output error messages for all bad exports
92-
let isBad = false
9392
_.toPairs(badExports).forEach(badItem => {
9493
const [exportType, entries] = badItem
9594
if (entries.length > 0) {
96-
isBad = true
97-
console.log(getBadExportsMessage(entries, exportType, apis[exportType]))
95+
reporter.panicOnBuild(getBadExportsMessage(entries, exportType, apis[exportType]))
9896
}
9997
})
100-
return isBad
10198
}
10299

103100
/**

packages/gatsby/src/internal-plugins/query-runner/query-runner.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,10 @@ module.exports = async (queryJob: QueryJob, component: Any) => {
5555
errorDetails.set(`Plugin`, queryJob.pluginCreatorId || `none`)
5656
errorDetails.set(`Query`, queryJob.query)
5757

58-
report.log(`
58+
report.panicOnBuild(`
5959
The GraphQL query from ${queryJob.componentPath} failed.
6060
6161
${formatErrorDetails(errorDetails)}`)
62-
63-
// Perhaps this isn't the best way to see if we're building?
64-
if (program._[0] === `build`) {
65-
process.exit(1)
66-
}
6762
}
6863

6964
// Add the page context onto the results.

packages/gatsby/src/redux/actions.js

+5-18
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,9 @@ ${reservedFields.map(f => ` * "${f}"`).join(`\n`)}
213213
}
214214

215215
if (noPageOrComponent) {
216-
console.log(``)
217-
console.log(
216+
report.panic(
218217
`See the documentation for createPage https://www.gatsbyjs.org/docs/bound-action-creators/#createPage`
219218
)
220-
console.log(``)
221-
process.exit(1)
222219
}
223220

224221
let jsonName
@@ -280,25 +277,16 @@ ${reservedFields.map(f => ` * "${f}"`).join(`\n`)}
280277
)
281278

282279
if (!notEmpty) {
283-
console.log(``)
284-
console.log(
280+
report.panicOnBuild(
285281
`You have an empty file in the "src/pages" directory at "${relativePath}". Please remove it or make it a valid component`
286282
)
287-
console.log(``)
288-
// TODO actually do die during builds.
289-
// process.exit(1)
290283
}
291284

292285
if (!includesDefaultExport) {
293-
console.log(``)
294-
console.log(
286+
report.panicOnBuild(
295287
`[${fileName}] The page component must export a React component for it to be valid`
296288
)
297-
console.log(``)
298289
}
299-
300-
// TODO actually do die during builds.
301-
// process.exit(1)
302290
}
303291

304292
fileOkCache[internalPage.component] = true
@@ -494,13 +482,12 @@ actions.createNode = (
494482

495483
// Tell user not to set the owner name themself.
496484
if (node.internal.owner) {
497-
console.log(JSON.stringify(node, null, 4))
498-
console.log(
485+
report.error(JSON.stringify(node, null, 4))
486+
report.panic(
499487
chalk.bold.red(
500488
`The node internal.owner field is set automatically by Gatsby and not by plugins`
501489
)
502490
)
503-
process.exit(1)
504491
}
505492

506493
// Add the plugin name to the internal object.

0 commit comments

Comments
 (0)