-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add info message if gatsby-config.js could have been typo'd #4017
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
93895e7
fix: add info message if gatsby-config.js could have been typo'd
DSchau 527a637
chore: move preferDefault back to where it was
DSchau 1343745
refactor: tweak error logic a bit
DSchau 360d638
chore: tweak versions for yarn.lock compat
DSchau 6e82150
refactor: address PR comments
DSchau 11b46e4
fix: fail with error
DSchau d92876b
format
KyleAMathews 3bf4e58
Merge branch 'master' into config-validation
KyleAMathews 0614b4b
Fix lint errors
KyleAMathews File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* @flow */ | ||
const levenshtein = require('fast-levenshtein'); | ||
const fs = require('fs-extra'); | ||
const testRequireError = require(`../utils/test-require-error`); | ||
const report = require(`gatsby-cli/lib/reporter`); | ||
const chalk = require('chalk'); | ||
|
||
function isNearMatch(fileName: string, configName: string, distance: number): boolean { | ||
return levenshtein.get(fileName, configName) <= distance; | ||
} | ||
|
||
module.exports = async function getConfigFile(rootDir: string, configName: string, distance: number = 3) { | ||
const configPath = `${rootDir}/${configName}`; | ||
try { | ||
return require(configPath); | ||
} catch (err) { | ||
const nearMatch = await fs.readdir(rootDir) | ||
.then(files => { | ||
return files | ||
.find(file => { | ||
const fileName = file.split(rootDir).pop(); | ||
return isNearMatch(fileName, configName, distance); | ||
}); | ||
}); | ||
if (!testRequireError(configPath, err)) { | ||
report.error(`Could not load ${configName}`, err); | ||
process.exit(1); | ||
} else if (nearMatch) { | ||
console.log(''); | ||
report.error(`It looks like you were trying to add the config file? Please rename "${chalk.bold(nearMatch)}" to "${chalk.bold(configName)}"`); | ||
console.log(''); | ||
process.exit(1); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ const { store, emitter } = require(`../redux`) | |
const loadPlugins = require(`./load-plugins`) | ||
const { initCache } = require(`../utils/cache`) | ||
const report = require(`gatsby-cli/lib/reporter`) | ||
const getConfigFile = require('./get-config-file'); | ||
|
||
// Show stack trace on unhandled promises. | ||
process.on(`unhandledRejection`, (reason, p) => { | ||
|
@@ -72,16 +73,7 @@ module.exports = async (args: BootstrapArgs) => { | |
// Try opening the site's gatsby-config.js file. | ||
activity = report.activityTimer(`open and validate gatsby-config.js`) | ||
activity.start() | ||
let config | ||
try { | ||
// $FlowFixMe | ||
config = preferDefault(require(`${program.directory}/gatsby-config`)) | ||
} catch (err) { | ||
if (!testRequireError(`${program.directory}/gatsby-config`, err)) { | ||
report.error(`Could not load gatsby-config`, err) | ||
process.exit(1) | ||
} | ||
} | ||
const config = await preferDefault(getConfigFile(program.directory, 'gatsby-config.js')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No issues with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, this should be fine. |
||
|
||
store.dispatch({ | ||
type: `SET_SITE_CONFIG`, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm a crazy person, I think this was a bug actually.
testRequireError
returnstrue
in the example when gatsby-config.js can't be found or is misnamed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it's correct.
testRequireError
only returns an error ifgatsby-config.js
exists and has a JS error of some sort. Not having a config file is fine.