Skip to content

Commit

Permalink
feat!: Restructure account commands to meet the new design guidelines (
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-yeager authored Nov 22, 2024
1 parent 3d882c9 commit 5933219
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 61 deletions.
15 changes: 2 additions & 13 deletions commands/__tests__/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import use from '../accounts/use';
import info from '../accounts/info';
import remove from '../accounts/remove';
import clean from '../accounts/clean';
import { addAccountOptions, addConfigOptions } from '../../lib/commonOpts';

jest.mock('yargs');
jest.mock('../accounts/list');
Expand All @@ -25,7 +24,7 @@ import accountsCommand from '../accounts';
describe('commands/accounts', () => {
describe('command', () => {
it('should have the correct command structure', () => {
expect(accountsCommand.command).toEqual('accounts');
expect(accountsCommand.command).toEqual(['account', 'accounts']);
});
});

Expand All @@ -37,7 +36,7 @@ describe('commands/accounts', () => {

describe('builder', () => {
const subcommands = [
['list', { ...list, aliases: 'ls' }],
['list', list],
['rename', rename],
['use', use],
['info', info],
Expand All @@ -52,16 +51,6 @@ describe('commands/accounts', () => {
expect(yargs.demandCommand).toHaveBeenCalledWith(1, '');
});

it('should support the correct options', () => {
accountsCommand.builder(yargs);

expect(addConfigOptions).toHaveBeenCalledTimes(1);
expect(addConfigOptions).toHaveBeenCalledWith(yargs);

expect(addAccountOptions).toHaveBeenCalledTimes(1);
expect(addAccountOptions).toHaveBeenCalledWith(yargs);
});

it('should add the correct number of sub commands', () => {
accountsCommand.builder(yargs);
expect(yargs.command).toHaveBeenCalledTimes(subcommands.length);
Expand Down
11 changes: 2 additions & 9 deletions commands/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-nocheck
const { addConfigOptions, addAccountOptions } = require('../lib/commonOpts');
const { i18n } = require('../lib/lang');
const list = require('./accounts/list');
const rename = require('./accounts/rename');
Expand All @@ -10,18 +9,12 @@ const clean = require('./accounts/clean');

const i18nKey = 'commands.accounts';

exports.command = 'accounts';
exports.command = ['account', 'accounts'];
exports.describe = i18n(`${i18nKey}.describe`);

exports.builder = yargs => {
addConfigOptions(yargs);
addAccountOptions(yargs);

yargs
.command({
...list,
aliases: 'ls',
})
.command(list)
.command(rename)
.command(use)
.command(info)
Expand Down
9 changes: 1 addition & 8 deletions commands/accounts/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ const { trackCommandUsage } = require('../../lib/usageTracking');
const { i18n } = require('../../lib/lang');
const { loadAndValidateOptions } = require('../../lib/validation');
const { EXIT_CODES } = require('../../lib/enums/exitCodes');
const {
addConfigOptions,
addAccountOptions,
addUseEnvironmentOptions,
addTestingOptions,
} = require('../../lib/commonOpts');
const { addTestingOptions, addConfigOptions } = require('../../lib/commonOpts');
const { promptUser } = require('../../lib/prompts/promptUtils');
const { getTableContents } = require('../../lib/ui/table');
const SpinniesManager = require('../../lib/ui/SpinniesManager');
Expand Down Expand Up @@ -137,8 +132,6 @@ exports.handler = async options => {

exports.builder = yargs => {
addConfigOptions(yargs);
addAccountOptions(yargs);
addUseEnvironmentOptions(yargs);
addTestingOptions(yargs);

yargs.example([['$0 accounts clean']]);
Expand Down
12 changes: 4 additions & 8 deletions commands/accounts/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
const { logger } = require('@hubspot/local-dev-lib/logger');
const { getAccountConfig } = require('@hubspot/local-dev-lib/config');
const { getAccessToken } = require('@hubspot/local-dev-lib/personalAccessKey');
const { addAccountOptions, addConfigOptions } = require('../../lib/commonOpts');
const { addConfigOptions } = require('../../lib/commonOpts');
const { loadAndValidateOptions } = require('../../lib/validation');
const { i18n } = require('../../lib/lang');
const { getTableContents } = require('../../lib/ui/table');

const i18nKey = 'commands.accounts.subcommands.info';
exports.describe = i18n(`${i18nKey}.describe`);

exports.command = 'info [--account]';
exports.command = 'info [account]';

exports.handler = async options => {
await loadAndValidateOptions(options);
Expand Down Expand Up @@ -40,15 +40,11 @@ exports.handler = async options => {

exports.builder = yargs => {
addConfigOptions(yargs);
addAccountOptions(yargs);

yargs.example([
['$0 accounts info', i18n(`${i18nKey}.examples.default`)],
[
'$0 accounts info --account=MyAccount',
i18n(`${i18nKey}.examples.nameBased`),
],
['$0 accounts info --account=1234567', i18n(`${i18nKey}.examples.idBased`)],
['$0 accounts info MyAccount', i18n(`${i18nKey}.examples.nameBased`)],
['$0 accounts info 1234567', i18n(`${i18nKey}.examples.idBased`)],
]);

return yargs;
Expand Down
7 changes: 2 additions & 5 deletions commands/accounts/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const {
const {
getAccountIdentifier,
} = require('@hubspot/local-dev-lib/config/getAccountIdentifier');
const { addConfigOptions } = require('../../lib/commonOpts');
const { getTableContents, getTableHeader } = require('../../lib/ui/table');

const { addConfigOptions, addAccountOptions } = require('../../lib/commonOpts');
const { trackCommandUsage } = require('../../lib/usageTracking');
const { loadAndValidateOptions } = require('../../lib/validation');
const { isSandbox, isDeveloperTestAccount } = require('../../lib/accountTypes');
Expand All @@ -24,7 +24,7 @@ const {

const i18nKey = 'commands.accounts.subcommands.list';

exports.command = 'list';
exports.command = ['list', 'ls'];
exports.describe = i18n(`${i18nKey}.describe`);

const sortAndMapPortals = portals => {
Expand Down Expand Up @@ -113,9 +113,6 @@ exports.handler = async options => {

exports.builder = yargs => {
addConfigOptions(yargs);
addAccountOptions(yargs);

yargs.example([['$0 accounts list']]);

return yargs;
};
15 changes: 7 additions & 8 deletions commands/accounts/remove.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-nocheck
const { addConfigOptions } = require('../../lib/commonOpts');
const { logger } = require('@hubspot/local-dev-lib/logger');
const {
getConfig,
Expand All @@ -16,16 +17,16 @@ const { loadAndValidateOptions } = require('../../lib/validation');

const i18nKey = 'commands.accounts.subcommands.remove';

exports.command = 'remove [--account]';
exports.command = 'remove [account]';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
await loadAndValidateOptions(options, false);
const { account } = options;
let accountToRemove = account;

let config = getConfig();

let accountToRemove = options.providedAccountId;

if (accountToRemove && !getAccountIdFromConfig(accountToRemove)) {
logger.error(
i18n(`${i18nKey}.errors.accountNotFound`, {
Expand Down Expand Up @@ -69,16 +70,14 @@ exports.handler = async options => {
};

exports.builder = yargs => {
yargs.option('account', {
addConfigOptions(yargs);
yargs.positional('account', {
describe: i18n(`${i18nKey}.options.account.describe`),
type: 'string',
});
yargs.example([
['$0 accounts remove', i18n(`${i18nKey}.examples.default`)],
[
'$0 accounts remove --account=MyAccount',
i18n(`${i18nKey}.examples.byName`),
],
['$0 accounts remove MyAccount', i18n(`${i18nKey}.examples.byName`)],
]);

return yargs;
Expand Down
11 changes: 4 additions & 7 deletions commands/accounts/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { loadAndValidateOptions } = require('../../lib/validation');

const i18nKey = 'commands.accounts.subcommands.use';

exports.command = 'use [--account]';
exports.command = 'use [account]';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
Expand Down Expand Up @@ -52,17 +52,14 @@ exports.handler = async options => {
};

exports.builder = yargs => {
yargs.option('account', {
yargs.positional('account', {
describe: i18n(`${i18nKey}.options.account.describe`),
type: 'string',
});
yargs.example([
['$0 accounts use', i18n(`${i18nKey}.examples.default`)],
[
'$0 accounts use --account=MyAccount',
i18n(`${i18nKey}.examples.nameBased`),
],
['$0 accounts use --account=1234567', i18n(`${i18nKey}.examples.idBased`)],
['$0 accounts use MyAccount', i18n(`${i18nKey}.examples.nameBased`)],
['$0 accounts use 1234567', i18n(`${i18nKey}.examples.idBased`)],
]);

return yargs;
Expand Down
6 changes: 3 additions & 3 deletions lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ en:
loadConfigMiddleware:
configFileExists: "A configuration file already exists at {{ configPath }}. To specify a new configuration file, delete the existing one and try again."
accounts:
describe: "Commands for working with accounts."
describe: "Commands for managing configured accounts."
subcommands:
list:
accounts: "{{#bold}}Accounts{{/bold}}:"
Expand All @@ -22,7 +22,7 @@ en:
authType: "Auth Type"
name: "Name"
rename:
describe: "Rename account in config."
describe: "Rename an account in the config."
positionals:
accountName:
describe: "Name of account to be renamed."
Expand Down Expand Up @@ -63,7 +63,7 @@ en:
accountRemoved: "Account \"{{ accountName }}\" removed from the config"
info:
accountId: "{{#bold}}Account ID{{/bold}}: {{ accountId }}"
describe: "Print information about the default account, or about the account specified with the \"--account\" option."
describe: "Print information about the default account, or about the account specified with the \"account\" option."
errors:
notUsingPersonalAccessKey: "This command currently only supports fetching scopes for the personal access key auth type."
examples:
Expand Down

0 comments on commit 5933219

Please sign in to comment.