Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add default account overrides with .hsaccount file #228

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 55 additions & 16 deletions config/CLIConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'fs';
import findup from 'findup-sync';
import { getCwd } from '../lib/path';
import { logger } from '../lib/logger';
import { loadConfigFromEnvironment } from './environment';
import { getValidEnv } from '../lib/environment';
Expand All @@ -11,7 +14,13 @@ import {
import { commaSeparatedValues } from '../lib/text';
import { ENVIRONMENTS } from '../constants/environments';
import { API_KEY_AUTH_METHOD } from '../constants/auth';
import { HUBSPOT_ACCOUNT_TYPES, MIN_HTTP_TIMEOUT } from '../constants/config';
import {
HUBSPOT_ACCOUNT_TYPES,
MIN_HTTP_TIMEOUT,
DEFAULT_ACCOUNT_OVERRIDE_FILE_NAME,
DEFAULT_ACCOUNT_OVERRIDE_ERROR_INVALID_ID,
DEFAULT_ACCOUNT_OVERRIDE_ERROR_ACCOUNT_NOT_FOUND,
} from '../constants/config';
import { CMS_PUBLISH_MODE } from '../constants/files';
import { CLIConfig_NEW, Environment } from '../types/Config';
import {
Expand Down Expand Up @@ -228,23 +237,53 @@ class _CLIConfiguration {
}

getDefaultAccount(): string | number | null {
return this.config && this.config.defaultAccount
? this.config.defaultAccount
: null;
return (
this.getResolvedDefaultAccountForCWD() ||
kemmerle marked this conversation as resolved.
Show resolved Hide resolved
this.config?.defaultAccount ||
null
);
}

getDefaultAccountOverrideFilePath(): string | null {
return findup([DEFAULT_ACCOUNT_OVERRIDE_FILE_NAME], {
cwd: getCwd(),
});
joe-yeager marked this conversation as resolved.
Show resolved Hide resolved
}

// TODO a util that returns the account to use, respecting the values set in
// "defaultAccountOverrides"
// Example "defaultAccountOverrides":
// - /src/brodgers/customer-project-1: customer-account1
// - /src/brodgers/customer-project-2: customer-account2
// "/src/brodgers/customer-project-1" is the path to the project dir
// "customer-account1" is the name of the account to use as the default for the specified dir
// These defaults take precedence over the standard default account specified in the config
getResolvedDefaultAccountForCWD(
nameOrId: string | number
): CLIAccount_NEW | null {
return this.getAccount(nameOrId);
getResolvedDefaultAccountForCWD(): number | null {
kemmerle marked this conversation as resolved.
Show resolved Hide resolved
const defaultOverrideFile = this.getDefaultAccountOverrideFilePath();
if (!defaultOverrideFile) {
return null;
}
const source = fs.readFileSync(defaultOverrideFile, 'utf8');
kemmerle marked this conversation as resolved.
Show resolved Hide resolved
const accountId = Number(source);
kemmerle marked this conversation as resolved.
Show resolved Hide resolved

if (isNaN(accountId)) {
throw new Error(
i18n(`${i18nKey}.getResolvedDefaultAccountForCWD.errorHeader`, {
hsAccountFile: defaultOverrideFile,
}),
{
cause: DEFAULT_ACCOUNT_OVERRIDE_ERROR_INVALID_ID,
}
);
}

const account = this.config?.accounts?.find(
account => account.accountId === accountId
);
if (!account) {
throw new Error(
i18n(`${i18nKey}.getResolvedDefaultAccountForCWD.errorHeader`, {
hsAccountFile: defaultOverrideFile,
}),
{
cause: DEFAULT_ACCOUNT_OVERRIDE_ERROR_ACCOUNT_NOT_FOUND,
}
);
}

return accountId;
}

getAccountIndex(accountId: number): number {
Expand Down
12 changes: 12 additions & 0 deletions config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ export function updateDefaultCmsPublishMode(
return config_DEPRECATED.updateDefaultCmsPublishMode(cmsPublishMode);
}

export function getResolvedDefaultAccountForCWD() {
if (CLIConfiguration.isActive()) {
return CLIConfiguration.getResolvedDefaultAccountForCWD();
}
}

export function getDefaultAccountOverrideFilePath() {
if (CLIConfiguration.isActive()) {
return CLIConfiguration.getDefaultAccountOverrideFilePath();
}
}

// These functions are not supported with the new config setup
export const getConfigAccountId = config_DEPRECATED.getConfigAccountId;
export const getOrderedAccount = config_DEPRECATED.getOrderedAccount;
Expand Down
6 changes: 6 additions & 0 deletions constants/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export const DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME = 'hubspot.config.yml';
export const HUBSPOT_CONFIGURATION_FOLDER = '.hubspot-cli';
export const HUBSPOT_CONFIGURATION_FILE = 'config.yml';

export const DEFAULT_ACCOUNT_OVERRIDE_FILE_NAME = '.hs-account';
export const DEFAULT_ACCOUNT_OVERRIDE_ERROR_INVALID_ID =
'DEFAULT_ACCOUNT_OVERRIDE_ERROR_INVALID_ID';
export const DEFAULT_ACCOUNT_OVERRIDE_ERROR_ACCOUNT_NOT_FOUND =
'DEFAULT_ACCOUNT_OVERRIDE_ERROR_ACCOUNT_NOT_FOUND';

export const MIN_HTTP_TIMEOUT = 3000;

export const HUBSPOT_ACCOUNT_TYPES = {
Expand Down
3 changes: 3 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@
"invalidInput": "A 'defaultAccount' with value of number or string is required to update the config."
}
},
"getResolvedDefaultAccountForCWD": {
"errorHeader": "Error in {{ hsAccountFile }}"
},
"renameAccount": {
"errors": {
"invalidName": "Cannot find account with identifier {{ currentName }}"
Expand Down
Loading