Skip to content

Commit

Permalink
Merge pull request #683 from HubSpot/updates-to-accounts-use
Browse files Browse the repository at this point in the history
Switching `hs account use` arg to an option + deprecation message for `hs config set default-account`
  • Loading branch information
brandenrodgers authored May 6, 2022
2 parents 930e7ca + 5776033 commit 58d3699
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 38 deletions.
3 changes: 2 additions & 1 deletion packages/cli-lib/lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down
77 changes: 46 additions & 31 deletions packages/cli/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions packages/cli/commands/accounts/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/commands/config/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -89,5 +89,8 @@ exports.builder = yargs => {

yargs.example([['$0 config set', i18n(`${i18nKey}.examples.default`)]]);

//TODO remove this when "hs accounts use" is fully rolled out
yargs.strict(false);

return yargs;
};

0 comments on commit 58d3699

Please sign in to comment.