Skip to content

Commit d592481

Browse files
committed
Add a 'Did you mean ...' error message for unrecognized API methods
1 parent e3fbf67 commit d592481

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const validator = require('./lib/config/validator');
1515
const PrettyError = require('pretty-error');
1616
const logger = require('./lib/logger');
1717
const parseRuntime = require('./lib/config/parse-runtime');
18+
const chalk = require('chalk');
19+
const levenshtein = require('fast-levenshtein');
1820

1921
let webpackConfig = null;
2022
let runtimeConfig = require('./lib/context').runtimeConfig;
@@ -631,6 +633,21 @@ const publicApiProxy = new Proxy(publicApi, {
631633
process.exit(1); // eslint-disable-line
632634
}
633635
};
636+
} else if (typeof target[prop] === 'undefined') {
637+
// Find the property with the closest Levenshtein distance
638+
let similarProperty;
639+
let minDistance = Number.MAX_VALUE;
640+
for (const apiProperty in target) {
641+
const distance = levenshtein.get(apiProperty, prop);
642+
if (distance <= minDistance) {
643+
similarProperty = apiProperty;
644+
minDistance = distance;
645+
}
646+
}
647+
648+
const error = new Error(`${chalk.red(`Encore.${prop}`)} is not a recognized property, did you mean ${chalk.green(`Encore.${similarProperty}`)}?`);
649+
console.log(new PrettyError().render(error));
650+
process.exit(1); // eslint-disable-line
634651
}
635652

636653
return target[prop];

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"clean-webpack-plugin": "^0.1.16",
3434
"css-loader": "^0.26.2",
3535
"extract-text-webpack-plugin": "^3.0.0",
36+
"fast-levenshtein": "^2.0.6",
3637
"file-loader": "^0.10.1",
3738
"friendly-errors-webpack-plugin": "^1.6.1",
3839
"fs-extra": "^2.0.0",

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ fast-deep-equal@^1.0.0:
21562156
version "1.0.0"
21572157
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz#96256a3bc975595eb36d82e9929d060d893439ff"
21582158

2159-
fast-levenshtein@~2.0.4:
2159+
fast-levenshtein@^2.0.6, fast-levenshtein@~2.0.4:
21602160
version "2.0.6"
21612161
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
21622162

0 commit comments

Comments
 (0)