From db039b471a3b7538a9f5b8dd3bf423eacf27a9bc Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Thu, 5 May 2022 17:40:02 -0400 Subject: [PATCH 1/2] Switching hs account use arg to an option. Show warning for moved config command --- packages/cli-lib/lang/en.lyaml | 3 +- packages/cli/bin/cli.js | 77 ++++++++++++++++----------- packages/cli/commands/accounts/use.js | 13 +++-- packages/cli/commands/config/set.js | 5 +- 4 files changed, 60 insertions(+), 38 deletions(-) diff --git a/packages/cli-lib/lang/en.lyaml b/packages/cli-lib/lang/en.lyaml index 14e54f7f2..0d0782958 100644 --- a/packages/cli-lib/lang/en.lyaml +++ b/packages/cli-lib/lang/en.lyaml @@ -3,6 +3,7 @@ en: commands: generalErrors: srcIsProject: "\"{{ src }}\" is in a project folder. Did you mean \"hs project {{command}}\"?" + setDefaultAccountMoved: "This command has moved. Try `hs accounts use` instead" accounts: describe: "Commands for working with accounts." subcommands: @@ -32,7 +33,7 @@ en: default: "Select account to use as the default from a list" idBased: "Set the default account to the account in the config with accountId equal to \"1234567\"" nameBased: "Set the default account to the account in the config with name equal to \"MyAccount\"" - positionals: + options: account: describe: "Account name or id to use as the default" promptMessage: "Select an account to use as the default" diff --git a/packages/cli/bin/cli.js b/packages/cli/bin/cli.js index a23b1da35..b2b644c3b 100755 --- a/packages/cli/bin/cli.js +++ b/packages/cli/bin/cli.js @@ -58,24 +58,55 @@ const getTerminalWidth = () => { return width; }; +const handleFailure = (msg, err, yargs) => { + if (msg) { + logger.error(msg); + } else if (err) { + logErrorInstance(err); + } + + if (msg === null) { + yargs.showHelp(); + process.exit(EXIT_CODES.SUCCESS); + } else { + process.exit(EXIT_CODES.ERROR); + } +}; + +const performChecks = argv => { + // "hs config set default-account" has moved to "hs accounts use" + if ( + argv._[0] === 'config' && + argv._[1] === 'set' && + argv._[2] === 'default-account' + ) { + logger.error(i18n(`${i18nKey}.setDefaultAccountMoved`)); + process.exit(EXIT_CODES.ERROR); + } + + // Require "project" command when running upload/watch inside of a project + if (argv._.length === 1 && ['upload', 'watch'].includes(argv._[0])) { + if (getIsInProject(argv.src)) { + logger.error( + i18n(`${i18nKey}.srcIsProject`, { + src: argv.src || './', + command: argv._.join(' '), + }) + ); + process.exit(EXIT_CODES.ERROR); + } else { + return true; + } + } else { + return true; + } +}; + const argv = yargs .usage('Tools for working with HubSpot') .middleware([setLogLevel]) .exitProcess(false) - .fail((msg, err, yargs) => { - if (msg) { - logger.error(msg); - } else if (err) { - logErrorInstance(err); - } - - if (msg === null) { - yargs.showHelp(); - process.exit(EXIT_CODES.SUCCESS); - } else { - process.exit(EXIT_CODES.ERROR); - } - }) + .fail(handleFailure) .option('debug', { alias: 'd', default: false, @@ -94,23 +125,7 @@ const argv = yargs hidden: true, type: 'boolean', }) - .check(argv => { - if (argv._.length === 1 && ['upload', 'watch'].includes(argv._[0])) { - if (getIsInProject(argv.src)) { - logger.error( - i18n(`${i18nKey}.srcIsProject`, { - src: argv.src, - command: argv._.join(' '), - }) - ); - process.exit(EXIT_CODES.ERROR); - } else { - return true; - } - } else { - return true; - } - }) + .check(performChecks) .command(authCommand) .command(initCommand) .command(logsCommand) diff --git a/packages/cli/commands/accounts/use.js b/packages/cli/commands/accounts/use.js index e6ebfed2a..9db800be3 100644 --- a/packages/cli/commands/accounts/use.js +++ b/packages/cli/commands/accounts/use.js @@ -33,7 +33,7 @@ const selectAccountFromConfig = async config => { return selectedDefault; }; -exports.command = 'use [account]'; +exports.command = 'use [--account]'; exports.describe = i18n(`${i18nKey}.describe`); exports.handler = async options => { @@ -68,14 +68,17 @@ exports.handler = async options => { }; exports.builder = yargs => { - yargs.positional('account', { - describe: i18n(`${i18nKey}.positionals.account.describe`), + yargs.option('account', { + describe: i18n(`${i18nKey}.options.account.describe`), type: 'string', }); yargs.example([ ['$0 accounts use', i18n(`${i18nKey}.examples.default`)], - ['$0 accounts use MyAccount', i18n(`${i18nKey}.examples.nameBased`)], - ['$0 accounts use 1234567', i18n(`${i18nKey}.examples.idBased`)], + [ + '$0 accounts use --account=MyAccount', + i18n(`${i18nKey}.examples.nameBased`), + ], + ['$0 accounts use --account=1234567', i18n(`${i18nKey}.examples.idBased`)], ]); return yargs; diff --git a/packages/cli/commands/config/set.js b/packages/cli/commands/config/set.js index 2aa9005d5..712b93a68 100644 --- a/packages/cli/commands/config/set.js +++ b/packages/cli/commands/config/set.js @@ -33,7 +33,7 @@ const selectOptions = async () => { }; const handleConfigUpdate = async (accountId, options) => { - const { defaultMode, httpTimeout, allowUsageTracking } = options; + const { allowUsageTracking, defaultMode, httpTimeout } = options; if (typeof defaultMode !== 'undefined') { await setDefaultMode({ defaultMode, accountId }); @@ -89,5 +89,8 @@ exports.builder = yargs => { yargs.example([['$0 config set', i18n(`${i18nKey}.examples.default`)]]); + //TODO remove this + yargs.strict(false); + return yargs; }; From 5776033921f6324c114cac375f1d84f7fb6f9ff1 Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Thu, 5 May 2022 17:44:38 -0400 Subject: [PATCH 2/2] update comment --- packages/cli/commands/config/set.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/commands/config/set.js b/packages/cli/commands/config/set.js index 712b93a68..a0ab8c222 100644 --- a/packages/cli/commands/config/set.js +++ b/packages/cli/commands/config/set.js @@ -89,7 +89,7 @@ exports.builder = yargs => { yargs.example([['$0 config set', i18n(`${i18nKey}.examples.default`)]]); - //TODO remove this + //TODO remove this when "hs accounts use" is fully rolled out yargs.strict(false); return yargs;