Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: solved asyncapi help #843

Merged
merged 22 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
14cbd0c
handle the case if the file is not present.
AyushNautiyalDeveloper Sep 19, 2023
03c0146
Merge pull request #1 from asyncapi/master
ayushnau Sep 21, 2023
0c7dc9a
Merge branch 'master' into filehandle
ayushnau Sep 21, 2023
972cef0
remove the lint error
AyushNautiyalDeveloper Sep 26, 2023
013599d
Merge branch 'master' of github.com-ayushnau:ayushnau/cli into fileha…
AyushNautiyalDeveloper Sep 26, 2023
0bb6b5d
made the required changes.
AyushNautiyalDeveloper Sep 30, 2023
002952f
Merge branch 'master' into filehandle
Souvikns Oct 4, 2023
c673263
Merge pull request #2 from asyncapi/master
ayushnau Oct 5, 2023
3750452
Added the latest version of asyncapi in the for generator template of…
AyushNautiyalDeveloper Oct 5, 2023
6665cd9
Merge branch 'master' into master
Souvikns Oct 9, 2023
16d845c
Merge branch 'master' into master
Souvikns Oct 9, 2023
84162fc
Merge branch 'master' into filehandle
Souvikns Oct 9, 2023
29b2df3
added the hook .
AyushNautiyalDeveloper Oct 13, 2023
9b42245
fixed bug
AyushNautiyalDeveloper Oct 13, 2023
6a53b61
added the semicolon
AyushNautiyalDeveloper Oct 13, 2023
a1afe8b
ran lint command
AyushNautiyalDeveloper Oct 13, 2023
6a82b6c
Added the test
AyushNautiyalDeveloper Oct 14, 2023
2a28d11
Merge branch 'master' of github.com-ayushnau:ayushnau/cli into fileha…
AyushNautiyalDeveloper Oct 18, 2023
51d1a6f
fixed the bug of help command.
AyushNautiyalDeveloper Oct 18, 2023
fa80b7c
Merge branch 'master' into filehandle
Souvikns Oct 19, 2023
f8cdf92
made small change.
AyushNautiyalDeveloper Oct 19, 2023
aabac41
Merge branch 'filehandle' of github.com-ayushnau:ayushnau/cli into fi…
AyushNautiyalDeveloper Oct 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ oclif.run()
.then(require('@oclif/core/flush'))
.catch((err) => {
const oclifHandler = require('@oclif/core/handle');
console.error(err.message);
return oclifHandler(err.message);
});

11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"ajv": "^8.12.0",
"chalk": "^4.1.0",
"chokidar": "^3.5.2",
"fast-levenshtein": "^3.0.0",
"fs-extra": "^11.1.0",
"indent-string": "^4.0.0",
"inquirer": "^8.2.0",
Expand All @@ -50,6 +51,7 @@
"@oclif/test": "^2",
"@swc/core": "^1.3.2",
"@types/chai": "^4.3.6",
"@types/fast-levenshtein": "^0.0.2",
"@types/fs-extra": "^11.0.1",
"@types/inquirer": "^8.1.3",
"@types/js-yaml": "^4.0.5",
Expand Down Expand Up @@ -103,9 +105,12 @@
"oclif": {
"commands": "./lib/commands",
"bin": "asyncapi",
"plugins": [
"@oclif/plugin-not-found"
],
"plugins": [],
"hooks": {
"command_not_found": [
"./lib/hooks/command_not_found/myhook"
]
},
"macos": {
"identifier": "com.asyncapi.cli"
},
Expand Down Expand Up @@ -151,7 +156,9 @@
"release": "semantic-release",
"test": "npm run test:unit",
"test:unit": "cross-env NODE_ENV=development TEST=1 CUSTOM_CONTEXT_FILENAME=\"test.asyncapi-cli\" CUSTOM_CONTEXT_FILE_LOCATION=\"\" nyc --extension .ts mocha --require ts-node/register --require test/helpers/init.js --reporter spec --timeout 100000 \"test/**/*.test.ts\"",
"get-version": "echo $npm_package_version"
"get-version": "echo $npm_package_version",
"createhook": "oclif generate hook myhook --event=command_not_found",
"createhookinit": "oclif generate hook inithook --event=init"
},
"types": "lib/index.d.ts"
}
75 changes: 75 additions & 0 deletions src/hooks/command_not_found/myhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {Hook, toConfiguredId, CliUx} from '@oclif/core';
import chalk from 'chalk';
import {default as levenshtein} from 'fast-levenshtein';
import { Help } from '@oclif/core';

export const closest = (target: string, possibilities: string[]): string =>
possibilities
.map((id) => ({distance: levenshtein.get(target, id, {useCollator: true}), id}))
.sort((a, b) => a.distance - b.distance)[0]?.id ?? '';

const hook: Hook.CommandNotFound = async function (opts) {
if (opts.id === '--help') {
const help = new Help(this.config);
help.showHelp(['--help']);
return;
}
const hiddenCommandIds = new Set(opts.config.commands.filter((c) => c.hidden).map((c) => c.id));
const commandIDs = [...opts.config.commandIDs, ...opts.config.commands.flatMap((c) => c.aliases)].filter(
(c) => !hiddenCommandIds.has(c),
);

if (commandIDs.length === 0) {return;}

// now we we return if the command id are not there.

let binHelp = `${opts.config.bin} help`;

const idSplit = opts.id.split(':');
if (opts.config.findTopic(idSplit[0])) {
// if valid topic, update binHelp with topic
binHelp = `${binHelp} ${idSplit[0]}`;
}

//if there is a topic in the opts we just upgrade the our commnad like

// alter the suggestion in the help scenario so that help is the first command
// otherwise the user will be presented 'did you mean 'help'?' instead of 'did you mean "help <command>"?'
let suggestion = (/:?help:?/).test(opts.id)
? ['help', ...opts.id.split(':').filter((cmd) => cmd !== 'help')].join(':')
: closest(opts.id, commandIDs);

let readableSuggestion = toConfiguredId(suggestion, this.config);
const originalCmd = toConfiguredId(opts.id, this.config);
this.warn(`${chalk.yellow(originalCmd)} is not a ${opts.config.bin} command.`);

let response = '';
try {
if (opts.id === 'help') {readableSuggestion = '--help';}
response = await CliUx.ux.prompt(`Did you mean ${chalk.blueBright(readableSuggestion)}? [y/n]`, {timeout: 10_000});
} catch (error) {
this.log('');
this.debug(error);
}

if (response === 'y') {
// this will split the original command from the suggested replacement, and gather the remaining args as varargs to help with situations like:
// confit set foo-bar -> confit:set:foo-bar -> config:set:foo-bar -> config:set foo-bar
let argv = opts.argv?.length ? opts.argv : opts.id.split(':').slice(suggestion.split(':').length);
if (suggestion.startsWith('help:')) {
// the args are the command/partial command you need help for (package:version)
// we created the suggestion variable to start with "help" so slice the first entry
argv = suggestion.split(':').slice(1);
// the command is just the word "help"
suggestion = 'help';
}
if (opts.id === 'help') {
return this.config.runCommand('--help');
}
return this.config.runCommand(suggestion, argv);
}

this.error(`Run ${chalk.bold.cyan(binHelp)} for a list of available commands.`, {exit: 127});
};

export default hook;
10 changes: 10 additions & 0 deletions test/hooks/command_not_found/myhook.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {expect, test} from '@oclif/test';

describe('hooks', () => {
test
.stdout()
.hook('command_not_found', {id: 'help'})
.do(output => expect(output.stdout).to.contain('help command not found.'))
.it('shows a message');
});

6 changes: 1 addition & 5 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
"compilerOptions": {
"noEmit": true,
},
"references": [
{
"path": "../"
}
]

}