From 4cbedf6bf82ea253b03cef173201144f91363745 Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Fri, 22 Nov 2024 15:25:36 -0500 Subject: [PATCH 1/2] restructure hs completion command --- bin/cli.js | 3 ++- commands/completion.ts | 26 ++++++++++++++++++++++++++ lang/en.lyaml | 4 ++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 commands/completion.ts diff --git a/bin/cli.js b/bin/cli.js index d0d245ff6..b64de26de 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -54,6 +54,7 @@ const sandboxesCommand = require('../commands/sandbox'); const cmsCommand = require('../commands/cms'); const feedbackCommand = require('../commands/feedback'); const doctorCommand = require('../commands/doctor'); +const completionCommand = require('../commands/completion'); const notifier = updateNotifier({ pkg: { ...pkg, name: '@hubspot/cli' }, @@ -226,10 +227,10 @@ const argv = yargs .command(sandboxesCommand) .command(feedbackCommand) .command(doctorCommand) + .command(completionCommand) .help() .recommendCommands() .demandCommand(1, '') - .completion() .wrap(getTerminalWidth()) .strict().argv; diff --git a/commands/completion.ts b/commands/completion.ts new file mode 100644 index 000000000..37a54d690 --- /dev/null +++ b/commands/completion.ts @@ -0,0 +1,26 @@ +// @ts-nocheck +const yargsParser = require('yargs-parser'); +const { i18n } = require('../lib/lang'); +const { trackCommandUsage } = require('../lib/usageTracking'); + +const i18nKey = 'commands.completion'; + +exports.command = 'completion'; +exports.describe = i18n(`${i18nKey}.describe`); + +exports.handler = async () => { + await trackCommandUsage('completion'); +}; + +exports.builder = yargs => { + const { help } = yargsParser(process.argv.slice(2)); + + if (!help) { + yargs.completion(); + } + + yargs.example([ + ['$0 completion >> ~/.zshrc', i18n(`${i18nKey}.examples.default`)], + ]); + return yargs; +}; diff --git a/lang/en.lyaml b/lang/en.lyaml index b8c49b38f..0b7f75eab 100644 --- a/lang/en.lyaml +++ b/lang/en.lyaml @@ -9,6 +9,10 @@ en: setDefaultAccountMoved: "This command has moved. Try `hs accounts use` instead" loadConfigMiddleware: configFileExists: "A configuration file already exists at {{ configPath }}. To specify a new configuration file, delete the existing one and try again." + completion: + describe: "Enable bash completion shortcuts for commands. Concat the generated script to your .bashrc, .bash_profile, or .zshrc file." + examples: + default: "Generate shell completion scripts for the zsh shell" accounts: describe: "Commands for working with accounts." subcommands: From dcee22168ef68ec8ccaa3a9a4934e3059ff03fe5 Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Fri, 22 Nov 2024 15:41:37 -0500 Subject: [PATCH 2/2] Add yargs parser dep --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 09eb8e0c0..7127a2ffd 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "table": "^6.6.0", "tmp": "^0.2.1", "update-notifier": "^5.1.0", - "yargs": "17.7.2" + "yargs": "17.7.2", + "yargs-parser": "^21.1.1" }, "devDependencies": { "@types/fs-extra": "^11.0.4",