-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
c8d3c63
commit 9c2a5ad
Showing
5 changed files
with
59 additions
and
19 deletions.
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,48 @@ | ||
/* @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}` | ||
let configModule | ||
try { | ||
configModule = require(configPath) | ||
} catch (err) { | ||
const nearMatch = await fs.readdir(rootDir).then(files => | ||
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) | ||
} | ||
} | ||
|
||
return configModule | ||
} |
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
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