Skip to content

Commit

Permalink
Merge pull request #216 from HubSpot/rename-mode
Browse files Browse the repository at this point in the history
chore!: Rename Mode to CmsPublishMode
  • Loading branch information
camden11 authored Nov 25, 2024
2 parents 11f934a + 1c519db commit ab7337c
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 93 deletions.
57 changes: 39 additions & 18 deletions config/CLIConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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 { MODE } from '../constants/files';
import { CMS_PUBLISH_MODE } from '../constants/files';
import { CLIConfig_NEW, Environment } from '../types/Config';
import {
CLIAccount_NEW,
Expand All @@ -21,9 +21,8 @@ import {
AccountType,
} from '../types/Accounts';
import { CLIOptions } from '../types/CLIOptions';
import { ValueOf } from '../types/Utils';
import { i18n } from '../utils/lang';
import { Mode } from '../types/Files';
import { CmsPublishMode } from '../types/Files';

const i18nKey = 'config.cliConfiguration';

Expand Down Expand Up @@ -65,7 +64,7 @@ class _CLIConfiguration {
})
);
this.useEnvConfig = true;
this.config = configFromEnv;
this.config = this.handleLegacyCmsPublishMode(configFromEnv);
}
} else {
const configFromFile = loadConfigFromFile();
Expand All @@ -76,7 +75,7 @@ class _CLIConfiguration {
this.config = { accounts: [] };
}
this.useEnvConfig = false;
this.config = configFromFile;
this.config = this.handleLegacyCmsPublishMode(configFromFile);
}

return this.config;
Expand Down Expand Up @@ -342,7 +341,7 @@ class _CLIConfiguration {
authType,
clientId,
clientSecret,
defaultMode,
defaultCmsPublishMode,
env,
name,
parentAccountId,
Expand Down Expand Up @@ -397,8 +396,9 @@ class _CLIConfiguration {
const updatedEnv = getValidEnv(
env || (currentAccountConfig && currentAccountConfig.env)
);
const updatedDefaultMode: ValueOf<typeof MODE> | undefined =
defaultMode && (defaultMode.toLowerCase() as ValueOf<typeof MODE>);
const updatedDefaultCmsPublishMode: CmsPublishMode | undefined =
defaultCmsPublishMode &&
(defaultCmsPublishMode.toLowerCase() as CmsPublishMode);
const updatedAccountType =
accountType || (currentAccountConfig && currentAccountConfig.accountType);

Expand All @@ -410,9 +410,12 @@ class _CLIConfiguration {
if (nextAccountConfig.authType === API_KEY_AUTH_METHOD.value) {
safelyApplyUpdates('apiKey', apiKey);
}
if (typeof updatedDefaultMode !== 'undefined') {
if (typeof updatedDefaultCmsPublishMode !== 'undefined') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
safelyApplyUpdates('defaultMode', MODE[updatedDefaultMode]);
safelyApplyUpdates(
'defaultCmsPublishMode',
CMS_PUBLISH_MODE[updatedDefaultCmsPublishMode]
);
}
safelyApplyUpdates('personalAccessKey', personalAccessKey);

Expand Down Expand Up @@ -554,21 +557,29 @@ class _CLIConfiguration {
/**
* @throws {Error}
*/
updateDefaultMode(defaultMode: Mode): CLIConfig_NEW | null {
updateDefaultCmsPublishMode(
defaultCmsPublishMode: CmsPublishMode
): CLIConfig_NEW | null {
if (!this.config) {
throw new Error(i18n(`${i18nKey}.errors.noConfigLoaded`));
}
const ALL_MODES = Object.values(MODE);
if (!defaultMode || !ALL_MODES.find(m => m === defaultMode)) {
const ALL_CMS_PUBLISH_MODES = Object.values(CMS_PUBLISH_MODE);
if (
!defaultCmsPublishMode ||
!ALL_CMS_PUBLISH_MODES.find(m => m === defaultCmsPublishMode)
) {
throw new Error(
i18n(`${i18nKey}.updateDefaultMode.errors.invalidMode`, {
defaultMode,
validModes: commaSeparatedValues(ALL_MODES),
})
i18n(
`${i18nKey}.updateDefaultCmsPublishMode.errors.invalidCmsPublishMode`,
{
defaultCmsPublishMode,
validCmsPublishModes: commaSeparatedValues(ALL_CMS_PUBLISH_MODES),
}
)
);
}

this.config.defaultMode = defaultMode;
this.config.defaultCmsPublishMode = defaultCmsPublishMode;
return this.write();
}

Expand Down Expand Up @@ -618,6 +629,16 @@ class _CLIConfiguration {
}
return this.config.allowUsageTracking !== false;
}

handleLegacyCmsPublishMode(
config: CLIConfig_NEW | null
): CLIConfig_NEW | null {
if (config?.defaultMode) {
config.defaultCmsPublishMode = config.defaultMode;
delete config.defaultMode;
}
return config;
}
}

export const CLIConfiguration = new _CLIConfiguration();
4 changes: 2 additions & 2 deletions config/__tests__/CLIConfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ describe('config/CLIConfiguration', () => {
});
});

describe('updateDefaultMode()', () => {
describe('updateDefaultCmsPublishMode()', () => {
it('throws when no config is loaded', () => {
expect(() => {
config.updateDefaultMode('draft');
config.updateDefaultCmsPublishMode('draft');
}).toThrow();
});
});
Expand Down
4 changes: 2 additions & 2 deletions config/configUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function getOrderedConfig(
): CLIConfig_NEW {
const {
defaultAccount,
defaultMode,
defaultCmsPublishMode,
httpTimeout,
allowUsageTracking,
accounts,
Expand All @@ -47,7 +47,7 @@ export function getOrderedConfig(

return {
...(defaultAccount && { defaultAccount }),
defaultMode,
defaultCmsPublishMode,
httpTimeout,
allowUsageTracking,
...rest,
Expand Down
50 changes: 34 additions & 16 deletions config/config_DEPRECATED.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
PERSONAL_ACCESS_KEY_AUTH_METHOD,
OAUTH_SCOPES,
} from '../constants/auth';
import { MODE } from '../constants/files';
import { CMS_PUBLISH_MODE } from '../constants/files';
import { getValidEnv } from '../lib/environment';
import { logger } from '../lib/logger';
import { isConfigPathInGitRepo } from '../utils/git';
Expand All @@ -32,10 +32,10 @@ import {
UpdateAccountConfigOptions,
} from '../types/Accounts';
import { BaseError } from '../types/Error';
import { Mode } from '../types/Files';
import { CmsPublishMode } from '../types/Files';
import { CLIOptions, WriteConfigOptions } from '../types/CLIOptions';

const ALL_MODES = Object.values(MODE);
const ALL_CMS_PUBLISH_MODES = Object.values(CMS_PUBLISH_MODE);
let _config: CLIConfig_DEPRECATED | null;
let _configPath: string | null;
let environmentVariableConfigLoaded = false;
Expand Down Expand Up @@ -182,7 +182,7 @@ export function getOrderedAccount(
export function getOrderedConfig(unorderedConfig: CLIConfig_DEPRECATED) {
const {
defaultPortal,
defaultMode,
defaultCmsPublishMode,
httpTimeout,
allowUsageTracking,
portals,
Expand All @@ -191,7 +191,7 @@ export function getOrderedConfig(unorderedConfig: CLIConfig_DEPRECATED) {

return {
...(defaultPortal && { defaultPortal }),
defaultMode,
defaultCmsPublishMode,
httpTimeout,
allowUsageTracking,
...rest,
Expand Down Expand Up @@ -288,7 +288,7 @@ function loadConfigFromFile(path?: string, options: CLIOptions = {}) {
if (sourceError) return;
const { parsed, error: parseError } = parseConfig(source);
if (parseError) return;
setConfig(parsed);
setConfig(handleLegacyCmsPublishMode(parsed));

if (!getConfig()) {
logger.debug('The config file was empty config');
Expand Down Expand Up @@ -491,7 +491,7 @@ export function updateAccountConfig(
authType,
clientId,
clientSecret,
defaultMode,
defaultCmsPublishMode,
environment,
name,
parentAccountId,
Expand Down Expand Up @@ -526,8 +526,8 @@ export function updateAccountConfig(
(configOptions && configOptions.env) ||
(accountConfig && accountConfig.env)
);
const mode: Mode | undefined =
defaultMode && (defaultMode.toLowerCase() as Mode);
const cmsPublishMode: CmsPublishMode | undefined =
defaultCmsPublishMode?.toLowerCase() as CmsPublishMode;
const nextAccountConfig: FlatAccountFields_DEPRECATED = {
...accountConfig,
name: name || (accountConfig && accountConfig.name),
Expand All @@ -537,7 +537,10 @@ export function updateAccountConfig(
auth,
accountType: getAccountType(accountType, sandboxAccountType),
apiKey,
defaultMode: mode && Object.hasOwn(MODE, mode) ? mode : undefined,
defaultCmsPublishMode:
cmsPublishMode && Object.hasOwn(CMS_PUBLISH_MODE, cmsPublishMode)
? cmsPublishMode
: undefined,
personalAccessKey,
sandboxAccountType,
parentAccountId,
Expand Down Expand Up @@ -583,17 +586,22 @@ export function updateDefaultAccount(defaultAccount: string | number): void {
/**
* @throws {Error}
*/
export function updateDefaultMode(defaultMode: Mode): void {
if (!defaultMode || !ALL_MODES.find(m => m === defaultMode)) {
export function updateDefaultCmsPublishMode(
defaultCmsPublishMode: CmsPublishMode
): void {
if (
!defaultCmsPublishMode ||
!ALL_CMS_PUBLISH_MODES.find(m => m === defaultCmsPublishMode)
) {
throw new Error(
`The mode ${defaultMode} is invalid. Valid values are ${commaSeparatedValues(
ALL_MODES
`The mode ${defaultCmsPublishMode} is invalid. Valid values are ${commaSeparatedValues(
ALL_CMS_PUBLISH_MODES
)}.`
);
}

const config = getAndLoadConfigIfNeeded();
config.defaultMode = defaultMode;
config.defaultCmsPublishMode = defaultCmsPublishMode;

setDefaultConfigPathIfUnset();
writeConfig();
Expand Down Expand Up @@ -878,7 +886,7 @@ function loadEnvironmentVariableConfig(options: {
`Loaded config from environment variables for account ${portalId}`
);

return setConfig(envConfig);
return setConfig(handleLegacyCmsPublishMode(envConfig));
}

export function isConfigFlagEnabled(flag: keyof CLIConfig_DEPRECATED): boolean {
Expand All @@ -890,3 +898,13 @@ export function isConfigFlagEnabled(flag: keyof CLIConfig_DEPRECATED): boolean {

return Boolean(config[flag] || false);
}

function handleLegacyCmsPublishMode(
config: CLIConfig_DEPRECATED | undefined
): CLIConfig_DEPRECATED | undefined {
if (config?.defaultMode) {
config.defaultCmsPublishMode = config.defaultMode;
delete config.defaultMode;
}
return config;
}
10 changes: 6 additions & 4 deletions config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
FlatAccountFields,
} from '../types/Accounts';
import { getAccountIdentifier } from './getAccountIdentifier';
import { Mode } from '../types/Files';
import { CmsPublishMode } from '../types/Files';

// Use new config if it exists
export function loadConfig(
Expand Down Expand Up @@ -250,11 +250,13 @@ export function getConfigAccounts():
return config_DEPRECATED.getConfigAccounts();
}

export function updateDefaultMode(mode: Mode): void | CLIConfig_NEW | null {
export function updateDefaultCmsPublishMode(
cmsPublishMode: CmsPublishMode
): void | CLIConfig_NEW | null {
if (CLIConfiguration.isActive()) {
return CLIConfiguration.updateDefaultMode(mode);
return CLIConfiguration.updateDefaultCmsPublishMode(cmsPublishMode);
}
return config_DEPRECATED.updateDefaultMode(mode);
return config_DEPRECATED.updateDefaultCmsPublishMode(cmsPublishMode);
}

// These functions are not supported with the new config setup
Expand Down
4 changes: 2 additions & 2 deletions constants/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export const STAT_TYPES = {
DIRECTORY: 'dir',
} as const;

export const MODE = {
export const CMS_PUBLISH_MODE = {
draft: 'draft',
publish: 'publish',
} as const;

export const DEFAULT_MODE = MODE.publish;
export const DEFAULT_CMS_PUBLISH_MODE = CMS_PUBLISH_MODE.publish;

export const FILE_UPLOAD_RESULT_TYPES = {
SUCCESS: 'SUCCESS',
Expand Down
4 changes: 2 additions & 2 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@
"invalidId": "Unable to find account for {{ nameOrId }}."
}
},
"updateDefaultMode": {
"updateDefaultCmsPublishMode": {
"errors": {
"invalidMode": "The mode {{ defaultMode }} is invalid. Valid values are {{ validModes }}."
"invalidCmsPublishMode": "The CMS publish mode {{ defaultCmsPublishMode }} is invalid. Valid values are {{ validCmsPublishModes }}."
}
},
"updateHttpTimeout": {
Expand Down
Loading

0 comments on commit ab7337c

Please sign in to comment.