Skip to content

Commit 2136520

Browse files
Refactor Nightwatch flags suggestion when wrong flags is been detected
Signed-off-by: Samson Olawoyin <[email protected]>
1 parent 1bdd438 commit 2136520

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@nightwatch/setup-tools": "^3.3.1",
4242
"ansi-colors": "^4.1.3",
4343
"axios": "^0.27.2",
44+
"didyoumean3": "^1.2.5",
4445
"inquirer": "^8.2.4",
4546
"minimist": "^1.2.6"
4647
},

src/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {NightwatchInitiator} from '@nightwatch/setup-tools';
77
import {NIGHTWATCH_TITLE, AVAILABLE_CONFIG_FLAGS} from './constants';
88
import Logger from './logger';
99
import minimist from 'minimist';
10-
import suggestSimilarOption from './utils/suggestSimilar';
10+
import suggestSimilarOption2 from './utils/suggestSimilar2';
1111
import {isNodeProject} from './utils';
1212
import {CURRENT_VERSION} from './utils/version';
1313
import axios, {AxiosResponse} from 'axios';
@@ -34,12 +34,11 @@ export const run = async () => {
3434
const wrongUserFlags = Object.keys(options).filter((word) => !AVAILABLE_CONFIG_FLAGS.includes(word));
3535

3636
if (wrongUserFlags.length > 0) {
37-
const findAndSuggestSimilarOption = suggestSimilarOption(wrongUserFlags[0], AVAILABLE_CONFIG_FLAGS);
38-
if (findAndSuggestSimilarOption !== '') {
39-
Logger.error(`error: unknown option '${wrongUserFlags[0]}'${findAndSuggestSimilarOption}`);
37+
const findAndSuggestSimilarOption = suggestSimilarOption2(wrongUserFlags[0], AVAILABLE_CONFIG_FLAGS);
38+
Logger.error(findAndSuggestSimilarOption);
4039

41-
return;
42-
}
40+
// Exit Nightwatch setup process
41+
process.exit(0);
4342
}
4443

4544
let rootDir = path.resolve(process.cwd(), args[0] || '');

src/utils/suggestSimilar2.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import didYouMean from 'didyoumean3';
2+
import colors from 'ansi-colors';
3+
4+
export default function suggestSimilarOption(word: string, default_flags: string[]) {
5+
const checkFlags = didYouMean(word, default_flags);
6+
const rt = `Flag ${colors.red('--' +word)} is not a valid flag, did you mean ${colors.green('--' + checkFlags['winner'])}`;
7+
8+
return rt;
9+
}

0 commit comments

Comments
 (0)