Skip to content
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 9 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"express": "^4.14.0",
"express-graphql": "^0.6.6",
"extract-text-webpack-plugin": "^1.0.1",
"fast-levenshtein": "~2.0.4",
"file-loader": "^0.9.0",
"flat": "^2.0.1",
"friendly-errors-webpack-plugin": "^1.6.1",
Expand Down
35 changes: 35 additions & 0 deletions packages/gatsby/src/bootstrap/get-config-file.js
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);
}
}
};
12 changes: 2 additions & 10 deletions packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)) {
Copy link
Contributor Author

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 returns true in the example when gatsby-config.js can't be found or is misnamed.

Copy link
Contributor

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 if gatsby-config.js exists and has a JS error of some sort. Not having a config file is fine.

report.error(`Could not load gatsby-config`, err)
process.exit(1)
}
}
const config = await preferDefault(getConfigFile(program.directory, 'gatsby-config.js'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues with awaiting here right? The "happy path" shouldn't see a performance hit I don't think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, this should be fine.


store.dispatch({
type: `SET_SITE_CONFIG`,
Expand Down