From 21365209570856e88fa78da0bd40fd0b9f87ff20 Mon Sep 17 00:00:00 2001 From: Samson Olawoyin Date: Mon, 11 Mar 2024 19:05:11 +0100 Subject: [PATCH 1/2] Refactor Nightwatch flags suggestion when wrong flags is been detected Signed-off-by: Samson Olawoyin --- package-lock.json | 11 +++++++++++ package.json | 1 + src/index.ts | 11 +++++------ src/utils/suggestSimilar2.ts | 9 +++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 src/utils/suggestSimilar2.ts diff --git a/package-lock.json b/package-lock.json index cb8a037..1019e4d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@nightwatch/setup-tools": "^3.3.1", "ansi-colors": "^4.1.3", "axios": "^0.27.2", + "didyoumean3": "^1.2.5", "inquirer": "^8.2.4", "minimist": "^1.2.6" }, @@ -2926,6 +2927,11 @@ "integrity": "sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==", "dev": true }, + "node_modules/didyoumean3": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/didyoumean3/-/didyoumean3-1.2.5.tgz", + "integrity": "sha512-ROol2k9IQ04NjrCiB98knunQ1cOmoXdYaGFjMfDIcXl9PuaxNVHxAZrG163aWkrRq5jIL+LatmqCgPasOvRwSQ==" + }, "node_modules/diff": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", @@ -10412,6 +10418,11 @@ "integrity": "sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==", "dev": true }, + "didyoumean3": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/didyoumean3/-/didyoumean3-1.2.5.tgz", + "integrity": "sha512-ROol2k9IQ04NjrCiB98knunQ1cOmoXdYaGFjMfDIcXl9PuaxNVHxAZrG163aWkrRq5jIL+LatmqCgPasOvRwSQ==" + }, "diff": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", diff --git a/package.json b/package.json index 3147ca5..dfeeb14 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "@nightwatch/setup-tools": "^3.3.1", "ansi-colors": "^4.1.3", "axios": "^0.27.2", + "didyoumean3": "^1.2.5", "inquirer": "^8.2.4", "minimist": "^1.2.6" }, diff --git a/src/index.ts b/src/index.ts index 00b0806..c7deb6f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import {NightwatchInitiator} from '@nightwatch/setup-tools'; import {NIGHTWATCH_TITLE, AVAILABLE_CONFIG_FLAGS} from './constants'; import Logger from './logger'; import minimist from 'minimist'; -import suggestSimilarOption from './utils/suggestSimilar'; +import suggestSimilarOption2 from './utils/suggestSimilar2'; import {isNodeProject} from './utils'; import {CURRENT_VERSION} from './utils/version'; import axios, {AxiosResponse} from 'axios'; @@ -34,12 +34,11 @@ export const run = async () => { const wrongUserFlags = Object.keys(options).filter((word) => !AVAILABLE_CONFIG_FLAGS.includes(word)); if (wrongUserFlags.length > 0) { - const findAndSuggestSimilarOption = suggestSimilarOption(wrongUserFlags[0], AVAILABLE_CONFIG_FLAGS); - if (findAndSuggestSimilarOption !== '') { - Logger.error(`error: unknown option '${wrongUserFlags[0]}'${findAndSuggestSimilarOption}`); + const findAndSuggestSimilarOption = suggestSimilarOption2(wrongUserFlags[0], AVAILABLE_CONFIG_FLAGS); + Logger.error(findAndSuggestSimilarOption); - return; - } + // Exit Nightwatch setup process + process.exit(0); } let rootDir = path.resolve(process.cwd(), args[0] || ''); diff --git a/src/utils/suggestSimilar2.ts b/src/utils/suggestSimilar2.ts new file mode 100644 index 0000000..ecfea73 --- /dev/null +++ b/src/utils/suggestSimilar2.ts @@ -0,0 +1,9 @@ +import didYouMean from 'didyoumean3'; +import colors from 'ansi-colors'; + +export default function suggestSimilarOption(word: string, default_flags: string[]) { + const checkFlags = didYouMean(word, default_flags); + const rt = `Flag ${colors.red('--' +word)} is not a valid flag, did you mean ${colors.green('--' + checkFlags['winner'])}`; + + return rt; +} From ee139e7d48aa4ac116c48a8a34194f0d7f3f59dd Mon Sep 17 00:00:00 2001 From: Samson Olawoyin Date: Wed, 13 Mar 2024 11:39:44 +0100 Subject: [PATCH 2/2] Update flags suggestion when installing Nightwatch Signed-off-by: Samson Olawoyin --- package-lock.json | 48 ++++++++++++++++++++++++++++++++++++ package.json | 2 +- src/index.ts | 7 +++--- src/utils/suggestSimilar2.ts | 9 ------- 4 files changed, 53 insertions(+), 13 deletions(-) delete mode 100644 src/utils/suggestSimilar2.ts diff --git a/package-lock.json b/package-lock.json index 1019e4d..1af1716 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@nightwatch/setup-tools": "^3.3.1", "ansi-colors": "^4.1.3", "axios": "^0.27.2", + "didyoumean2": "^6.0.1", "didyoumean3": "^1.2.5", "inquirer": "^8.2.4", "minimist": "^1.2.6" @@ -2927,6 +2928,20 @@ "integrity": "sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==", "dev": true }, + "node_modules/didyoumean2": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/didyoumean2/-/didyoumean2-6.0.1.tgz", + "integrity": "sha512-PSy0zQwMg5O+LjT5Mz7vnKC8I7DfWLPF6M7oepqW7WP5mn2CY3hz46xZOa1GJY+KVfyXhdmz6+tdgXwrHlZc5g==", + "hasInstallScript": true, + "dependencies": { + "@babel/runtime": "^7.19.0", + "fastest-levenshtein": "^1.0.12", + "lodash.deburr": "^4.1.0" + }, + "engines": { + "node": "^16.14.0 || >=18.12.0" + } + }, "node_modules/didyoumean3": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/didyoumean3/-/didyoumean3-1.2.5.tgz", @@ -3517,6 +3532,14 @@ "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==", "dev": true }, + "node_modules/fastest-levenshtein": { + "version": "1.0.16", + "resolved": "https://registry.npmjs.org/fastest-levenshtein/-/fastest-levenshtein-1.0.16.tgz", + "integrity": "sha512-eRnCtTTtGZFpQCwhJiUOuxPQWRXVKYDn0b2PeHfXL6/Zi53SLAzAHfVhVWK2AryC/WH05kGfxhFIPvTF0SXQzg==", + "engines": { + "node": ">= 4.9.1" + } + }, "node_modules/fastq": { "version": "1.13.0", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.13.0.tgz", @@ -5203,6 +5226,11 @@ "lodash._isiterateecall": "^3.0.0" } }, + "node_modules/lodash.deburr": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/lodash.deburr/-/lodash.deburr-4.1.0.tgz", + "integrity": "sha512-m/M1U1f3ddMCs6Hq2tAsYThTBDaAKFDX3dwDo97GEYzamXi9SqUpjWi/Rrj/gf3X2n8ktwgZrlP1z6E3v/IExQ==" + }, "node_modules/lodash.defaults": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz", @@ -10418,6 +10446,16 @@ "integrity": "sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==", "dev": true }, + "didyoumean2": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/didyoumean2/-/didyoumean2-6.0.1.tgz", + "integrity": "sha512-PSy0zQwMg5O+LjT5Mz7vnKC8I7DfWLPF6M7oepqW7WP5mn2CY3hz46xZOa1GJY+KVfyXhdmz6+tdgXwrHlZc5g==", + "requires": { + "@babel/runtime": "^7.19.0", + "fastest-levenshtein": "^1.0.12", + "lodash.deburr": "^4.1.0" + } + }, "didyoumean3": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/didyoumean3/-/didyoumean3-1.2.5.tgz", @@ -10867,6 +10905,11 @@ "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==", "dev": true }, + "fastest-levenshtein": { + "version": "1.0.16", + "resolved": "https://registry.npmjs.org/fastest-levenshtein/-/fastest-levenshtein-1.0.16.tgz", + "integrity": "sha512-eRnCtTTtGZFpQCwhJiUOuxPQWRXVKYDn0b2PeHfXL6/Zi53SLAzAHfVhVWK2AryC/WH05kGfxhFIPvTF0SXQzg==" + }, "fastq": { "version": "1.13.0", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.13.0.tgz", @@ -12155,6 +12198,11 @@ "lodash._isiterateecall": "^3.0.0" } }, + "lodash.deburr": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/lodash.deburr/-/lodash.deburr-4.1.0.tgz", + "integrity": "sha512-m/M1U1f3ddMCs6Hq2tAsYThTBDaAKFDX3dwDo97GEYzamXi9SqUpjWi/Rrj/gf3X2n8ktwgZrlP1z6E3v/IExQ==" + }, "lodash.defaults": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz", diff --git a/package.json b/package.json index dfeeb14..b35e237 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "@nightwatch/setup-tools": "^3.3.1", "ansi-colors": "^4.1.3", "axios": "^0.27.2", - "didyoumean3": "^1.2.5", + "didyoumean2": "^6.0.1", "inquirer": "^8.2.4", "minimist": "^1.2.6" }, diff --git a/src/index.ts b/src/index.ts index c7deb6f..7c2cb31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import {NightwatchInitiator} from '@nightwatch/setup-tools'; import {NIGHTWATCH_TITLE, AVAILABLE_CONFIG_FLAGS} from './constants'; import Logger from './logger'; import minimist from 'minimist'; -import suggestSimilarOption2 from './utils/suggestSimilar2'; +import DidYouMean from 'didyoumean2'; import {isNodeProject} from './utils'; import {CURRENT_VERSION} from './utils/version'; import axios, {AxiosResponse} from 'axios'; @@ -34,8 +34,9 @@ export const run = async () => { const wrongUserFlags = Object.keys(options).filter((word) => !AVAILABLE_CONFIG_FLAGS.includes(word)); if (wrongUserFlags.length > 0) { - const findAndSuggestSimilarOption = suggestSimilarOption2(wrongUserFlags[0], AVAILABLE_CONFIG_FLAGS); - Logger.error(findAndSuggestSimilarOption); + const findAndSuggestSimilarOption = DidYouMean(wrongUserFlags[0], AVAILABLE_CONFIG_FLAGS); + const result = `Flag ${colors.red('--'+wrongUserFlags[0])} is not valid, did you mean ${colors.green('--'+findAndSuggestSimilarOption)}`; + Logger.error(result); // Exit Nightwatch setup process process.exit(0); diff --git a/src/utils/suggestSimilar2.ts b/src/utils/suggestSimilar2.ts deleted file mode 100644 index ecfea73..0000000 --- a/src/utils/suggestSimilar2.ts +++ /dev/null @@ -1,9 +0,0 @@ -import didYouMean from 'didyoumean3'; -import colors from 'ansi-colors'; - -export default function suggestSimilarOption(word: string, default_flags: string[]) { - const checkFlags = didYouMean(word, default_flags); - const rt = `Flag ${colors.red('--' +word)} is not a valid flag, did you mean ${colors.green('--' + checkFlags['winner'])}`; - - return rt; -}