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 (POC): Add default account overrides #227

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
26 changes: 19 additions & 7 deletions config/CLIConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
deleteConfigFile,
} from './configFile';
import { commaSeparatedValues } from '../lib/text';
import { getCwd } from '../lib/path';
import { ENVIRONMENTS } from '../constants/environments';
import { API_KEY_AUTH_METHOD } from '../constants/auth';
import { HUBSPOT_ACCOUNT_TYPES, MIN_HTTP_TIMEOUT } from '../constants/config';
Expand Down Expand Up @@ -228,9 +229,11 @@ class _CLIConfiguration {
}

getDefaultAccount(): string | number | null {
return this.config && this.config.defaultAccount
? this.config.defaultAccount
: null;
return (
this.getResolvedDefaultAccountForCWD() ||
this.config?.defaultAccount ||
null
);
}

// TODO a util that returns the account to use, respecting the values set in
Expand All @@ -241,10 +244,19 @@ class _CLIConfiguration {
// "/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(): string | number | null {
const cwd = getCwd();
const overrides = this.config?.defaultAccountOverrides;

if (overrides) {
for (const [path, account] of Object.entries(overrides)) {
if (cwd.startsWith(path)) {
return account;
}
}
}

return null;
}

getAccountIndex(accountId: number): number {
Expand Down
1 change: 1 addition & 0 deletions types/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface CLIConfig_NEW {
accounts: Array<CLIAccount_NEW>;
allowUsageTracking?: boolean;
defaultAccount?: string | number;
defaultAccountOverrides?: { [key: string]: string | number };
defaultMode?: CmsPublishMode; // Deprecated - left in to handle existing configs with this field
defaultCmsPublishMode?: CmsPublishMode;
httpTimeout?: number;
Expand Down
Loading