diff --git a/commands/sandbox/create.ts b/commands/sandbox/create.ts index 18e304a68..c44eb45fd 100644 --- a/commands/sandbox/create.ts +++ b/commands/sandbox/create.ts @@ -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'); @@ -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 diff --git a/lib/buildAccount.ts b/lib/buildAccount.ts index 580249d93..91f421bea 100644 --- a/lib/buildAccount.ts +++ b/lib/buildAccount.ts @@ -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, @@ -83,111 +85,139 @@ 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 { + 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 { + 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) { @@ -195,8 +225,5 @@ export async function buildNewAccount({ throw err; } - return { - configAccountName, - result, - }; + return sandbox; } diff --git a/lib/localDev.ts b/lib/localDev.ts index c4c2d348e..886e185ca 100644 --- a/lib/localDev.ts +++ b/lib/localDev.ts @@ -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'; @@ -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; @@ -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) { diff --git a/lib/sandboxes.ts b/lib/sandboxes.ts index 458e1adbc..d7712201a 100644 --- a/lib/sandboxes.ts +++ b/lib/sandboxes.ts @@ -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`, {