From bb1a25004dc60457344e9b729c07c652304b42d6 Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Wed, 8 Jan 2020 16:55:12 -0500 Subject: [PATCH] Fix suggestions for CLI At some point the `scripts` object changes to be a map instead of an array, at which point the suggestions functionality broke. This commit includes the fix and a test to make sure it doesn't break again. --- __tests__/fixtures/index/run-suggestion/package.json | 8 ++++++++ __tests__/index.js | 4 ++++ src/cli/commands/run.js | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 __tests__/fixtures/index/run-suggestion/package.json diff --git a/__tests__/fixtures/index/run-suggestion/package.json b/__tests__/fixtures/index/run-suggestion/package.json new file mode 100644 index 0000000000..5757bb3b8c --- /dev/null +++ b/__tests__/fixtures/index/run-suggestion/package.json @@ -0,0 +1,8 @@ +{ + "name": "test_suggestion", + "version": "1.0.0", + "license": "UNLICENSED", + "scripts": { + "foobar": "foobar" + } +} diff --git a/__tests__/index.js b/__tests__/index.js index 7e16b08048..218d7285d1 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -300,6 +300,10 @@ test.concurrent('should throws missing command for unknown command', async () => await expectAnErrorMessage(execCommand('unknown', [], 'run-add', true), 'Command "unknown" not found'); }); +test.concurrent('should suggest options for other commands when missing a command', async () => { + await expectAnErrorMessage(execCommand('foobarx', [], 'run-suggestion', true), 'Did you mean "foobar"?'); +}); + test.concurrent('should not display documentation link for unknown command', async () => { await expectAnInfoMessageAfterError(execCommand('unknown', [], 'run-add', true), ''); }); diff --git a/src/cli/commands/run.js b/src/cli/commands/run.js index 33c90d5999..f0dfbf455d 100644 --- a/src/cli/commands/run.js +++ b/src/cli/commands/run.js @@ -149,7 +149,7 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg } else { let suggestion; - for (const commandName in scripts) { + for (const commandName of scripts.keys()) { const steps = leven(commandName, action); if (steps < 2) { suggestion = commandName;