Skip to content

Commit

Permalink
Add support for create-override command
Browse files Browse the repository at this point in the history
  • Loading branch information
kemmerle committed Jan 22, 2025
1 parent 47e238b commit 038a074
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions commands/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const use = require('./account/use');
const info = require('./account/info');
const remove = require('./account/remove');
const clean = require('./account/clean');
const createOverride = require('./account/createOverride');

const i18nKey = 'commands.account';

Expand All @@ -23,6 +24,7 @@ exports.builder = yargs => {
.command(info)
.command(remove)
.command(clean)
.command(createOverride)
.demandCommand(1, '');

return yargs;
Expand Down
58 changes: 58 additions & 0 deletions commands/account/createOverride.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// @ts-nocheck
const fs = require('fs-extra');
const path = require('path');
const { getCwd } = require('@hubspot/local-dev-lib/path');
const { logger } = require('@hubspot/local-dev-lib/logger');
const {
getConfigPath,
getAccountId,
} = require('@hubspot/local-dev-lib/config');
const { addConfigOptions } = require('../../lib/commonOpts');
const { i18n } = require('../../lib/lang');
import { EXIT_CODES } from '../../lib/enums/exitCodes';
const { selectAccountFromConfig } = require('../../lib/prompts/accountsPrompt');

const i18nKey = 'commands.account.subcommands.createOverride';
exports.describe = null; // i18n(`${i18nKey}.describe`);

exports.command = 'create-override [account]';

exports.handler = async options => {
let overrideDefaultAccount = options.account;

if (!overrideDefaultAccount) {
overrideDefaultAccount = await selectAccountFromConfig();
} else if (!getAccountId(overrideDefaultAccount)) {
logger.error(
i18n(`${i18nKey}.errors.accountNotFound`, {
specifiedAccount: overrideDefaultAccount,
configPath: getConfigPath(),
})
);
overrideDefaultAccount = await selectAccountFromConfig();
}
const accountId = getAccountId(overrideDefaultAccount);

try {
const overrideFilePath = path.join(getCwd(), '.hs-account');
await fs.writeFile(overrideFilePath, accountId.toString(), 'utf8');
logger.success(i18n(`${i18nKey}.success`, { overrideFilePath }));
} catch (e) {
logger.error(i18n(`${i18nKey}.errors.writeFile`, { error: e.message }));
process.exit(EXIT_CODES.ERROR);
}
};

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

yargs.example([
['$0 accounts create-override', i18n(`${i18nKey}.examples.default`)],
[
'$0 accounts create-override 12345678',
i18n(`${i18nKey}.examples.withAccountId`),
],
]);

return yargs;
};
5 changes: 5 additions & 0 deletions commands/account/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
getConfigPath,
getConfigDefaultAccount,
getConfigAccounts,
getDefaultAccountOverrideFilePath,
} = require('@hubspot/local-dev-lib/config');
const {
getAccountIdentifier,
Expand Down Expand Up @@ -85,6 +86,7 @@ exports.handler = async options => {
trackCommandUsage('accounts-list', null, derivedAccountId);

const configPath = getConfigPath();
const overrideFilePath = getDefaultAccountOverrideFilePath();
const accountsList = getConfigAccounts();
const mappedPortalData = sortAndMapPortals(accountsList);
const portalData = getPortalData(mappedPortalData);
Expand All @@ -97,6 +99,9 @@ exports.handler = async options => {
);

logger.log(i18n(`${i18nKey}.configPath`, { configPath }));
if (overrideFilePath) {
logger.log(i18n(`${i18nKey}.overrideFilePath`, { overrideFilePath }));
}
logger.log(
i18n(`${i18nKey}.defaultAccount`, {
account: getConfigDefaultAccount(),
Expand Down
10 changes: 10 additions & 0 deletions lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ en:
account:
describe: "Commands for managing configured accounts."
subcommands:
createOverride:
describe: "Create a new default account override file (.hs-account) in the current working directory."
success: "Default account override file created at {{ overrideFilePath }}"
errors:
writeFile: "Unable to create the default account override file: {{ error }}"
accountNotFound: "The account \"{{ specifiedAccount }}\" could not be found in {{ configPath }}"
examples:
default: "Create a new default account override file (.hs-account) in the current working directory"
withAccountId: "Create a new default account override file (.hs-account) in the current working directory, using the account with accountId \"1234567\""
list:
accounts: "{{#bold}}Accounts{{/bold}}:"
defaultAccount: "{{#bold}}Default account{{/bold}}: {{ account }}"
describe: "List names of accounts defined in config."
configPath: "{{#bold}}Config path{{/bold}}: {{ configPath }}"
overrideFilePath: "{{#bold}}Default account override file path{{/bold}}: {{ overrideFilePath }}"
labels:
accountId: "Account ID"
authType: "Auth Type"
Expand Down

0 comments on commit 038a074

Please sign in to comment.