Skip to content

Commit 9c2a5ad

Browse files
DSchauKyleAMathews
authored andcommitted
fix: add info message if gatsby-config.js could have been typo'd (#4017)
* fix: add info message if gatsby-config.js could have been typo'd * chore: move preferDefault back to where it was * refactor: tweak error logic a bit * chore: tweak versions for yarn.lock compat * refactor: address PR comments * fix: fail with error * format * Fix lint errors
1 parent c8d3c63 commit 9c2a5ad

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

packages/gatsby/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"express": "^4.14.0",
4343
"express-graphql": "^0.6.6",
4444
"extract-text-webpack-plugin": "^1.0.1",
45+
"fast-levenshtein": "~2.0.4",
4546
"file-loader": "^0.9.0",
4647
"flat": "^2.0.1",
4748
"friendly-errors-webpack-plugin": "^1.6.1",
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* @flow */
2+
const levenshtein = require(`fast-levenshtein`)
3+
const fs = require(`fs-extra`)
4+
const testRequireError = require(`../utils/test-require-error`)
5+
const report = require(`gatsby-cli/lib/reporter`)
6+
const chalk = require(`chalk`)
7+
8+
function isNearMatch(
9+
fileName: string,
10+
configName: string,
11+
distance: number
12+
): boolean {
13+
return levenshtein.get(fileName, configName) <= distance
14+
}
15+
16+
module.exports = async function getConfigFile(
17+
rootDir: string,
18+
configName: string,
19+
distance: number = 3
20+
) {
21+
const configPath = `${rootDir}/${configName}`
22+
let configModule
23+
try {
24+
configModule = require(configPath)
25+
} catch (err) {
26+
const nearMatch = await fs.readdir(rootDir).then(files =>
27+
files.find(file => {
28+
const fileName = file.split(rootDir).pop()
29+
return isNearMatch(fileName, configName, distance)
30+
})
31+
)
32+
if (!testRequireError(configPath, err)) {
33+
report.error(`Could not load ${configName}`, err)
34+
process.exit(1)
35+
} else if (nearMatch) {
36+
console.log(``)
37+
report.error(
38+
`It looks like you were trying to add the config file? Please rename "${chalk.bold(
39+
nearMatch
40+
)}" to "${chalk.bold(configName)}"`
41+
)
42+
console.log(``)
43+
process.exit(1)
44+
}
45+
}
46+
47+
return configModule
48+
}

packages/gatsby/src/bootstrap/index.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ const crypto = require(`crypto`)
1010
const del = require(`del`)
1111

1212
const apiRunnerNode = require(`../utils/api-runner-node`)
13-
const testRequireError = require(`../utils/test-require-error`)
1413
const { graphql } = require(`graphql`)
1514
const { store, emitter } = require(`../redux`)
1615
const loadPlugins = require(`./load-plugins`)
1716
const { initCache } = require(`../utils/cache`)
1817
const report = require(`gatsby-cli/lib/reporter`)
18+
const getConfigFile = require(`./get-config-file`)
1919

2020
// Show stack trace on unhandled promises.
2121
process.on(`unhandledRejection`, (reason, p) => {
@@ -72,16 +72,9 @@ module.exports = async (args: BootstrapArgs) => {
7272
// Try opening the site's gatsby-config.js file.
7373
activity = report.activityTimer(`open and validate gatsby-config.js`)
7474
activity.start()
75-
let config
76-
try {
77-
// $FlowFixMe
78-
config = preferDefault(require(`${program.directory}/gatsby-config`))
79-
} catch (err) {
80-
if (!testRequireError(`${program.directory}/gatsby-config`, err)) {
81-
report.error(`Could not load gatsby-config`, err)
82-
process.exit(1)
83-
}
84-
}
75+
const config = await preferDefault(
76+
getConfigFile(program.directory, `gatsby-config.js`)
77+
)
8578

8679
store.dispatch({
8780
type: `SET_SITE_CONFIG`,

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ const getBadExportsMessage = (badExports, exportType, apis) => {
6060
See https://www.gatsbyjs.org/docs/${exportType}-apis/ for the list of Gatsby ${capitalized} APIs`
6161

6262
badExports.forEach(bady => {
63-
const similarities = stringSimiliarity.findBestMatch(
64-
bady.exportName,
65-
apis
66-
)
63+
const similarities = stringSimiliarity.findBestMatch(bady.exportName, apis)
6764
message += `\n — `
6865
if (bady.pluginName == `default-site-plugin`) {
6966
message += `Your site's gatsby-${exportType}.js is exporting a variable named "${
@@ -72,9 +69,7 @@ const getBadExportsMessage = (badExports, exportType, apis) => {
7269
} else {
7370
message += `The plugin "${bady.pluginName}@${
7471
bady.pluginVersion
75-
}" is exporting a variable named "${
76-
bady.exportName
77-
}" which isn't an API.`
72+
}" is exporting a variable named "${bady.exportName}" which isn't an API.`
7873
}
7974
if (similarities.bestMatch.rating > 0.5) {
8075
message += ` Perhaps you meant to export "${

packages/gatsby/src/utils/webpack.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,10 @@ module.exports = async (
290290
]
291291
const isFramework = some(
292292
vendorModuleList.map(vendor => {
293-
const regex = new RegExp(`[\\\\/]node_modules[\\\\/]${vendor}[\\\\/].*`, `i`)
293+
const regex = new RegExp(
294+
`[\\\\/]node_modules[\\\\/]${vendor}[\\\\/].*`,
295+
`i`
296+
)
294297
return regex.test(module.resource)
295298
})
296299
)

0 commit comments

Comments
 (0)