Skip to content

Commit

Permalink
Convert lib/buildAccount to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
camden11 committed Jan 13, 2025
1 parent 8add9d7 commit b4001cb
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 95 deletions.
12 changes: 6 additions & 6 deletions commands/sandbox/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const {
HUBSPOT_ACCOUNT_TYPES,
HUBSPOT_ACCOUNT_TYPE_STRINGS,
} = require('@hubspot/local-dev-lib/constants/config');
const { buildNewAccount } = require('../../lib/buildAccount');
const { buildSandbox } = require('../../lib/buildAccount');
const {
hubspotAccountNamePrompt,
} = require('../../lib/prompts/accountNamePrompt');
Expand Down Expand Up @@ -131,13 +131,13 @@ exports.handler = async options => {
}

try {
const { result } = await buildNewAccount({
name: sandboxName,
accountType: sandboxType,
const result = await buildSandbox(
sandboxName,
accountConfig,
sandboxType,
env,
force,
});
force
);

const sandboxAccountConfig = getAccountConfig(result.sandbox.sandboxHubId);
// For v1 sandboxes, keep sync here. Once we migrate to v2, this will be handled by BE automatically
Expand Down
185 changes: 106 additions & 79 deletions lib/buildAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { debugError, logError } from './errorHandlers/index';
import { SANDBOX_API_TYPE_MAP, handleSandboxCreateError } from './sandboxes';
import { handleDeveloperTestAccountCreateError } from './developerTestAccounts';
import { CLIAccount } from '@hubspot/local-dev-lib/types/Accounts';
import { DeveloperTestAccount } from '@hubspot/local-dev-lib/types/developerTestAccounts';
import { SandboxResponse } from '@hubspot/local-dev-lib/types/Sandbox';

export async function saveAccountToConfig(
accountId: number | undefined,
Expand Down Expand Up @@ -83,120 +85,145 @@ export async function saveAccountToConfig(
return validName;
}

type ValidBuildAccountType =
| typeof HUBSPOT_ACCOUNT_TYPES.STANDARD_SANDBOX
| typeof HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX
| typeof HUBSPOT_ACCOUNT_TYPES.DEVELOPER_TEST;
export async function buildDeveloperTestAccount(
name: string,
accountConfig: CLIAccount,
env: Environment,
portalLimit: number
): Promise<DeveloperTestAccount> {
const i18nKey = 'lib.developerTestAccount.create.loading';

type BuildNewAccountOptions = {
name: string;
accountType: ValidBuildAccountType;
accountConfig: CLIAccount;
env: Environment;
portalLimit?: number;
force?: boolean;
};
const id = getAccountIdentifier(accountConfig);
const accountId = getAccountId(id);

if (!accountId) {
throw new Error(i18n(`${i18nKey}.fail`));
}

export async function buildNewAccount({
name,
accountType,
accountConfig,
env,
portalLimit, // Used only for developer test accounts
force = false,
}: BuildNewAccountOptions) {
SpinniesManager.init({
succeedColor: 'white',
});
const id = getAccountIdentifier(accountConfig);
const accountId = getAccountId(id);
const isSandbox =
accountType === HUBSPOT_ACCOUNT_TYPES.STANDARD_SANDBOX ||
accountType === HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX;
const isDeveloperTestAccount =
accountType === HUBSPOT_ACCOUNT_TYPES.DEVELOPER_TEST;

if ((!isSandbox && !isDeveloperTestAccount) || !accountId) {
return;

logger.log('');
SpinniesManager.add('buildDeveloperTestAccount', {
text: i18n(`${i18nKey}.add`, {
accountName: name,
}),
});

let developerTestAccount: DeveloperTestAccount;

try {
const { data } = await createDeveloperTestAccount(accountId, name);

developerTestAccount = data;

SpinniesManager.succeed('buildDeveloperTestAccount', {
text: i18n(`${i18nKey}.succeed`, {
accountName: name,
accountId: developerTestAccount.id,
}),
});
} catch (e) {
debugError(e);

SpinniesManager.fail('buildDeveloperTestAccount', {
text: i18n(`${i18nKey}.fail`, {
accountName: name,
}),
});

handleDeveloperTestAccountCreateError(e, accountId, env, portalLimit);
}

let result;
let spinniesI18nKey: string;
if (isSandbox) {
if (accountType === HUBSPOT_ACCOUNT_TYPES.STANDARD_SANDBOX) {
spinniesI18nKey = 'lib.sandbox.create.loading.standard';
}
if (accountType === HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX) {
spinniesI18nKey = 'lib.sandbox.create.loading.developer';
}
try {
await saveAccountToConfig(accountId, name, env);
} catch (err) {
logError(err);
throw err;
}

return developerTestAccount;
}

type SandboxType =
| typeof HUBSPOT_ACCOUNT_TYPES.STANDARD_SANDBOX
| typeof HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX;

type SandboxAccount = SandboxResponse & {
name: string;
};

export async function buildSandbox(
name: string,
accountConfig: CLIAccount,
sandboxType: SandboxType,
env: Environment,
force = false
): Promise<SandboxAccount> {
let i18nKey: string;
if (sandboxType === HUBSPOT_ACCOUNT_TYPES.STANDARD_SANDBOX) {
i18nKey = 'lib.sandbox.create.loading.standard';
} else {
spinniesI18nKey = 'lib.developerTestAccount.create.loading';
i18nKey = 'lib.sandbox.create.loading.developer';
}

const id = getAccountIdentifier(accountConfig);
const accountId = getAccountId(id);

if (!accountId) {
throw new Error(i18n(`${i18nKey}.fail`));
}

SpinniesManager.init({
succeedColor: 'white',
});

logger.log('');
SpinniesManager.add('buildNewAccount', {
text: i18n(`${spinniesI18nKey}.add`, {
SpinniesManager.add('buildSandbox', {
text: i18n(`${i18nKey}.add`, {
accountName: name,
}),
});

let resultAccountId;
let resultPersonalAccessKey;
let sandbox: SandboxAccount;

try {
if (isSandbox) {
const sandboxApiType = SANDBOX_API_TYPE_MAP[accountType]; // API expects sandbox type as 1 or 2.

const { data } = await createSandbox(accountId, name, sandboxApiType);
result = { name, ...data };
resultAccountId = result.sandbox.sandboxHubId;
resultPersonalAccessKey = result.personalAccessKey;
} else if (isDeveloperTestAccount) {
const { data } = await createDeveloperTestAccount(accountId, name);
result = data;
resultAccountId = result.id;
}
const sandboxApiType = SANDBOX_API_TYPE_MAP[sandboxType];

SpinniesManager.succeed('buildNewAccount', {
text: i18n(`${spinniesI18nKey}.succeed`, {
const { data } = await createSandbox(accountId, name, sandboxApiType);
sandbox = { name, ...data };

SpinniesManager.succeed('buildSandbox', {
text: i18n(`${i18nKey}.succeed`, {
accountName: name,
accountId: resultAccountId,
accountId: sandbox.sandbox.sandboxHubId,
}),
});
} catch (err) {
debugError(err);
} catch (e) {
debugError(e);

SpinniesManager.fail('buildNewAccount', {
text: i18n(`${spinniesI18nKey}.fail`, {
SpinniesManager.fail('buildSandbox', {
text: i18n(`${i18nKey}.fail`, {
accountName: name,
}),
});

if (isSandbox) {
handleSandboxCreateError(err, env, name, accountId);
}
if (isDeveloperTestAccount) {
handleDeveloperTestAccountCreateError(err, env, accountId, portalLimit);
}
handleSandboxCreateError(e, env, name, accountId);
}

let configAccountName: string;

try {
// Response contains PAK, save to config here
configAccountName = await saveAccountToConfig(
resultAccountId,
await saveAccountToConfig(
accountId,
name,
env,
resultPersonalAccessKey,
sandbox.personalAccessKey,
force
);
} catch (err) {
logError(err);
throw err;
}

return {
configAccountName,
result,
};
return sandbox;
}
21 changes: 12 additions & 9 deletions lib/localDev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ const { logError, ApiErrorContext } = require('./errorHandlers/index');
const {
PERSONAL_ACCESS_KEY_AUTH_METHOD,
} = require('@hubspot/local-dev-lib/constants/auth');
const { buildNewAccount, saveAccountToConfig } = require('./buildAccount');
const {
buildSandbox,
buildDeveloperTestAccount,
saveAccountToConfig,
} = require('./buildAccount');
const { hubspotAccountNamePrompt } = require('./prompts/accountNamePrompt');

const i18nKey = 'lib.localDev';
Expand Down Expand Up @@ -208,12 +212,12 @@ const createSandboxForLocalDev = async (accountId, accountConfig, env) => {
accountId
);

const { result } = await buildNewAccount({
const result = await buildSandbox(
name,
accountType: HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX,
accountConfig,
env,
});
HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX,
env
);

const targetAccountId = result.sandbox.sandboxHubId;

Expand Down Expand Up @@ -286,13 +290,12 @@ const createDeveloperTestAccountForLocalDev = async (
accountId
);

const { result } = await buildNewAccount({
const result = await buildDeveloperTestAccount(
name,
accountType: HUBSPOT_ACCOUNT_TYPES.DEVELOPER_TEST,
accountConfig,
env,
portalLimit: maxTestPortals,
});
maxTestPortals
);

return result.id;
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion lib/sandboxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export function handleSandboxCreateError(
env: Environment,
name: string,
accountId: number
) {
): never {
if (isMissingScopeError(err)) {
logger.error(
i18n(`${i18nKey}.create.failure.scopes.message`, {
Expand Down

0 comments on commit b4001cb

Please sign in to comment.