From 93895e769de7e34d90875320cb8ac5e0a2856b18 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 13 Feb 2018 10:31:12 -0800 Subject: [PATCH 1/8] fix: add info message if gatsby-config.js could have been typo'd --- packages/gatsby/package.json | 1 + .../gatsby/src/bootstrap/get-config-file.js | 32 +++++++++++++++++++ packages/gatsby/src/bootstrap/index.js | 16 +++------- yarn.lock | 4 +-- 4 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 packages/gatsby/src/bootstrap/get-config-file.js diff --git a/packages/gatsby/package.json b/packages/gatsby/package.json index dc3466c3eacc4..e77df7e23332e 100644 --- a/packages/gatsby/package.json +++ b/packages/gatsby/package.json @@ -42,6 +42,7 @@ "express": "^4.14.0", "express-graphql": "^0.6.6", "extract-text-webpack-plugin": "^1.0.1", + "fast-levenshtein": "~2.0.6", "file-loader": "^0.9.0", "flat": "^2.0.1", "friendly-errors-webpack-plugin": "^1.6.1", diff --git a/packages/gatsby/src/bootstrap/get-config-file.js b/packages/gatsby/src/bootstrap/get-config-file.js new file mode 100644 index 0000000000000..2e8cfb86150a7 --- /dev/null +++ b/packages/gatsby/src/bootstrap/get-config-file.js @@ -0,0 +1,32 @@ +/* @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`); + +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)) { + if (nearMatch) { + report.info(`The file ${nearMatch} looks like ${configName}, please rename.`); + } + report.error(`Could not load ${configName}`, err); + process.exit(1); + } + } +}; diff --git a/packages/gatsby/src/bootstrap/index.js b/packages/gatsby/src/bootstrap/index.js index a5e16b401a349..1cbe27a018b0a 100644 --- a/packages/gatsby/src/bootstrap/index.js +++ b/packages/gatsby/src/bootstrap/index.js @@ -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) => { @@ -33,13 +34,13 @@ const { writeRedirects, } = require(`../internal-plugins/query-runner/redirects-writer`) +const preferDefault = m => (m && m.default) || m + // Override console.log to add the source file + line number. // Useful for debugging if you lose a console.log somewhere. // Otherwise leave commented out. // require(`./log-line-function`) -const preferDefault = m => (m && m.default) || m - type BootstrapArgs = { directory: string, prefixPaths?: boolean, @@ -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')); store.dispatch({ type: `SET_SITE_CONFIG`, diff --git a/yarn.lock b/yarn.lock index 86044855e8398..324635151c753 100644 --- a/yarn.lock +++ b/yarn.lock @@ -549,7 +549,7 @@ axios@^0.17.1: follow-redirects "^1.2.5" is-buffer "^1.1.5" -"axios@github:contentful/axios#fix/https-via-http-proxy": +axios@contentful/axios#fix/https-via-http-proxy: version "0.17.1" resolved "https://codeload.github.com/contentful/axios/tar.gz/4b06f4a63db3ac16c99f7c61b584ef0e6d11f1af" dependencies: @@ -4598,7 +4598,7 @@ fast-json-stable-stringify@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2" -fast-levenshtein@~2.0.4: +fast-levenshtein@~2.0.4, fast-levenshtein@~2.0.6: version "2.0.6" resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917" From 527a63782fc964c538cb8c7ea2a6057178650816 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 13 Feb 2018 10:33:41 -0800 Subject: [PATCH 2/8] chore: move preferDefault back to where it was --- packages/gatsby/src/bootstrap/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/bootstrap/index.js b/packages/gatsby/src/bootstrap/index.js index 1cbe27a018b0a..42f02c1dda0e5 100644 --- a/packages/gatsby/src/bootstrap/index.js +++ b/packages/gatsby/src/bootstrap/index.js @@ -34,13 +34,13 @@ const { writeRedirects, } = require(`../internal-plugins/query-runner/redirects-writer`) -const preferDefault = m => (m && m.default) || m - // Override console.log to add the source file + line number. // Useful for debugging if you lose a console.log somewhere. // Otherwise leave commented out. // require(`./log-line-function`) +const preferDefault = m => (m && m.default) || m + type BootstrapArgs = { directory: string, prefixPaths?: boolean, From 1343745ed1a3b712cafc3386cd22d63c33625b4f Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 13 Feb 2018 11:16:55 -0800 Subject: [PATCH 3/8] refactor: tweak error logic a bit --- packages/gatsby/src/bootstrap/get-config-file.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/gatsby/src/bootstrap/get-config-file.js b/packages/gatsby/src/bootstrap/get-config-file.js index 2e8cfb86150a7..c2bece915e4f3 100644 --- a/packages/gatsby/src/bootstrap/get-config-file.js +++ b/packages/gatsby/src/bootstrap/get-config-file.js @@ -3,6 +3,7 @@ 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; @@ -21,12 +22,13 @@ module.exports = async function getConfigFile(rootDir: string, configName: strin return isNearMatch(fileName, configName, distance); }); }); - if (testRequireError(configPath, err)) { - if (nearMatch) { - report.info(`The file ${nearMatch} looks like ${configName}, please rename.`); - } - report.error(`Could not load ${configName}`, err); - process.exit(1); + if (testRequireError(configPath, err) && nearMatch) { + console.log(''); + report.info(`The file ${chalk.bold(nearMatch)} looks like ${chalk.bold(configName)}, please rename.`); + console.log(''); } + + report.error(`Could not load ${configName}`, err); + process.exit(1); } }; From 360d6389aa48834dae8df05efe6fea5ca9491a2a Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 13 Feb 2018 11:26:40 -0800 Subject: [PATCH 4/8] chore: tweak versions for yarn.lock compat --- packages/gatsby/package.json | 2 +- yarn.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/gatsby/package.json b/packages/gatsby/package.json index e77df7e23332e..c2bcc505ea841 100644 --- a/packages/gatsby/package.json +++ b/packages/gatsby/package.json @@ -42,7 +42,7 @@ "express": "^4.14.0", "express-graphql": "^0.6.6", "extract-text-webpack-plugin": "^1.0.1", - "fast-levenshtein": "~2.0.6", + "fast-levenshtein": "~2.0.4", "file-loader": "^0.9.0", "flat": "^2.0.1", "friendly-errors-webpack-plugin": "^1.6.1", diff --git a/yarn.lock b/yarn.lock index 324635151c753..86044855e8398 100644 --- a/yarn.lock +++ b/yarn.lock @@ -549,7 +549,7 @@ axios@^0.17.1: follow-redirects "^1.2.5" is-buffer "^1.1.5" -axios@contentful/axios#fix/https-via-http-proxy: +"axios@github:contentful/axios#fix/https-via-http-proxy": version "0.17.1" resolved "https://codeload.github.com/contentful/axios/tar.gz/4b06f4a63db3ac16c99f7c61b584ef0e6d11f1af" dependencies: @@ -4598,7 +4598,7 @@ fast-json-stable-stringify@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2" -fast-levenshtein@~2.0.4, fast-levenshtein@~2.0.6: +fast-levenshtein@~2.0.4: version "2.0.6" resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917" From 6e82150cb6ee5de021004857ce011f28885b572c Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 13 Feb 2018 13:56:56 -0800 Subject: [PATCH 5/8] refactor: address PR comments --- packages/gatsby/src/bootstrap/get-config-file.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/gatsby/src/bootstrap/get-config-file.js b/packages/gatsby/src/bootstrap/get-config-file.js index c2bece915e4f3..b4e913b6b5525 100644 --- a/packages/gatsby/src/bootstrap/get-config-file.js +++ b/packages/gatsby/src/bootstrap/get-config-file.js @@ -9,6 +9,11 @@ function isNearMatch(fileName: string, configName: string, distance: number): bo return levenshtein.get(fileName, configName) <= distance; } +function failAndExit(configName, err) { + report.error(`Could not load ${configName}`, err); + process.exit(1); +} + module.exports = async function getConfigFile(rootDir: string, configName: string, distance: number = 3) { const configPath = `${rootDir}/${configName}`; try { @@ -22,13 +27,13 @@ module.exports = async function getConfigFile(rootDir: string, configName: strin return isNearMatch(fileName, configName, distance); }); }); - if (testRequireError(configPath, err) && nearMatch) { + if (!testRequireError(configPath, err)) { + failAndExit(configName, err); + } else if (nearMatch) { console.log(''); - report.info(`The file ${chalk.bold(nearMatch)} looks like ${chalk.bold(configName)}, please rename.`); + report.info(`It looks like you were trying to add the config file? Please rename "${chalk.bold(nearMatch)}" to "${chalk.bold(configName)}"`); console.log(''); + failAndExit(configName, err); } - - report.error(`Could not load ${configName}`, err); - process.exit(1); } }; From 11b46e47b9c26df315ae22c4b5b8a8b66f4df9ab Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 13 Feb 2018 13:58:30 -0800 Subject: [PATCH 6/8] fix: fail with error --- packages/gatsby/src/bootstrap/get-config-file.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/gatsby/src/bootstrap/get-config-file.js b/packages/gatsby/src/bootstrap/get-config-file.js index b4e913b6b5525..fd385763e0be5 100644 --- a/packages/gatsby/src/bootstrap/get-config-file.js +++ b/packages/gatsby/src/bootstrap/get-config-file.js @@ -9,11 +9,6 @@ function isNearMatch(fileName: string, configName: string, distance: number): bo return levenshtein.get(fileName, configName) <= distance; } -function failAndExit(configName, err) { - report.error(`Could not load ${configName}`, err); - process.exit(1); -} - module.exports = async function getConfigFile(rootDir: string, configName: string, distance: number = 3) { const configPath = `${rootDir}/${configName}`; try { @@ -28,12 +23,13 @@ module.exports = async function getConfigFile(rootDir: string, configName: strin }); }); if (!testRequireError(configPath, err)) { - failAndExit(configName, err); + report.error(`Could not load ${configName}`, err); + process.exit(1); } else if (nearMatch) { console.log(''); - report.info(`It looks like you were trying to add the config file? Please rename "${chalk.bold(nearMatch)}" to "${chalk.bold(configName)}"`); + report.error(`It looks like you were trying to add the config file? Please rename "${chalk.bold(nearMatch)}" to "${chalk.bold(configName)}"`); console.log(''); - failAndExit(configName, err); + process.exit(1); } } }; From d92876bcd02bcbe7601bca9e882a28acb90ad759 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Tue, 13 Feb 2018 15:55:28 -0800 Subject: [PATCH 7/8] format --- .../gatsby/src/bootstrap/get-config-file.js | 58 +++++++++++-------- packages/gatsby/src/bootstrap/index.js | 6 +- packages/gatsby/src/bootstrap/load-plugins.js | 14 ++--- packages/gatsby/src/utils/webpack.config.js | 5 +- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/packages/gatsby/src/bootstrap/get-config-file.js b/packages/gatsby/src/bootstrap/get-config-file.js index fd385763e0be5..aa5663c14dca3 100644 --- a/packages/gatsby/src/bootstrap/get-config-file.js +++ b/packages/gatsby/src/bootstrap/get-config-file.js @@ -1,35 +1,43 @@ /* @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'); +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; +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}`; +module.exports = async function getConfigFile( + rootDir: string, + configName: string, + distance: number = 3 +) { + const configPath = `${rootDir}/${configName}` try { - return require(configPath); + 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); - }); - }); + 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); + 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); + 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) } } -}; +} diff --git a/packages/gatsby/src/bootstrap/index.js b/packages/gatsby/src/bootstrap/index.js index 42f02c1dda0e5..18a5cba345693 100644 --- a/packages/gatsby/src/bootstrap/index.js +++ b/packages/gatsby/src/bootstrap/index.js @@ -16,7 +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'); +const getConfigFile = require(`./get-config-file`) // Show stack trace on unhandled promises. process.on(`unhandledRejection`, (reason, p) => { @@ -73,7 +73,9 @@ 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() - const config = await preferDefault(getConfigFile(program.directory, 'gatsby-config.js')); + const config = await preferDefault( + getConfigFile(program.directory, `gatsby-config.js`) + ) store.dispatch({ type: `SET_SITE_CONFIG`, diff --git a/packages/gatsby/src/bootstrap/load-plugins.js b/packages/gatsby/src/bootstrap/load-plugins.js index 0f44b55b9ac15..f7c637eb480a6 100644 --- a/packages/gatsby/src/bootstrap/load-plugins.js +++ b/packages/gatsby/src/bootstrap/load-plugins.js @@ -58,10 +58,7 @@ const getBadExportsMessage = (badExports, exportType, apis) => { See https://www.gatsbyjs.org/docs/${exportType}-apis/ for the list of Gatsby ${capitalized} APIs` badExports.forEach(bady => { - const similarities = stringSimiliarity.findBestMatch( - bady.exportName, - apis - ) + const similarities = stringSimiliarity.findBestMatch(bady.exportName, apis) message += `\n — ` if (bady.pluginName == `default-site-plugin`) { message += `Your site's gatsby-${exportType}.js is exporting a variable named "${ @@ -70,9 +67,7 @@ const getBadExportsMessage = (badExports, exportType, apis) => { } else { message += `The plugin "${bady.pluginName}@${ bady.pluginVersion - }" is exporting a variable named "${ - bady.exportName - }" which isn't an API.` + }" is exporting a variable named "${bady.exportName}" which isn't an API.` } if (similarities.bestMatch.rating > 0.5) { message += ` Perhaps you meant to export "${ @@ -276,7 +271,6 @@ module.exports = async (config = {}) => { return acc }, {}) - const badExports = { node: [], browser: [], @@ -305,7 +299,9 @@ module.exports = async (config = {}) => { if (gatsbyBrowser) { const gatsbyBrowserKeys = _.keys(gatsbyBrowser) plugin.browserAPIs = _.intersection(gatsbyBrowserKeys, apis) - plugin.browserAPIs.map(browserAPI => apiToPlugins[browserAPI].push(plugin.name)) + plugin.browserAPIs.map(browserAPI => + apiToPlugins[browserAPI].push(plugin.name) + ) badExports.browser = getBadExports(plugin, gatsbyBrowserKeys, apis) // Collate any bad exports } diff --git a/packages/gatsby/src/utils/webpack.config.js b/packages/gatsby/src/utils/webpack.config.js index 2a40d217c6701..ae968dbc19e81 100644 --- a/packages/gatsby/src/utils/webpack.config.js +++ b/packages/gatsby/src/utils/webpack.config.js @@ -290,7 +290,10 @@ module.exports = async ( ] const isFramework = some( vendorModuleList.map(vendor => { - const regex = new RegExp(`[\\\\/]node_modules[\\\\/]${vendor}[\\\\/].*`, `i`) + const regex = new RegExp( + `[\\\\/]node_modules[\\\\/]${vendor}[\\\\/].*`, + `i` + ) return regex.test(module.resource) }) ) From 0614b4bd137db4c78481cca3acb7033c18753314 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Tue, 13 Feb 2018 16:25:26 -0800 Subject: [PATCH 8/8] Fix lint errors --- packages/gatsby/src/bootstrap/get-config-file.js | 11 ++++++++--- packages/gatsby/src/bootstrap/index.js | 1 - 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/gatsby/src/bootstrap/get-config-file.js b/packages/gatsby/src/bootstrap/get-config-file.js index aa5663c14dca3..26c67e89433e6 100644 --- a/packages/gatsby/src/bootstrap/get-config-file.js +++ b/packages/gatsby/src/bootstrap/get-config-file.js @@ -19,13 +19,16 @@ module.exports = async function getConfigFile( distance: number = 3 ) { const configPath = `${rootDir}/${configName}` + let configModule try { - return require(configPath) + configModule = require(configPath) } catch (err) { - const nearMatch = await fs.readdir(rootDir).then(files => files.find(file => { + 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) @@ -40,4 +43,6 @@ module.exports = async function getConfigFile( process.exit(1) } } + + return configModule } diff --git a/packages/gatsby/src/bootstrap/index.js b/packages/gatsby/src/bootstrap/index.js index 18a5cba345693..0c1bec338799b 100644 --- a/packages/gatsby/src/bootstrap/index.js +++ b/packages/gatsby/src/bootstrap/index.js @@ -10,7 +10,6 @@ const crypto = require(`crypto`) const del = require(`del`) const apiRunnerNode = require(`../utils/api-runner-node`) -const testRequireError = require(`../utils/test-require-error`) const { graphql } = require(`graphql`) const { store, emitter } = require(`../redux`) const loadPlugins = require(`./load-plugins`)