diff --git a/commands/sandbox/create.ts b/commands/sandbox/create.ts index d277adacd..33dbb1314 100644 --- a/commands/sandbox/create.ts +++ b/commands/sandbox/create.ts @@ -141,12 +141,7 @@ exports.handler = async options => { const sandboxAccountConfig = getAccountConfig(result.sandbox.sandboxHubId); // For v1 sandboxes, keep sync here. Once we migrate to v2, this will be handled by BE automatically const handleSyncSandbox = async syncTasks => { - await syncSandbox({ - accountConfig: sandboxAccountConfig, - parentAccountConfig: accountConfig, - env, - syncTasks, - }); + await syncSandbox(sandboxAccountConfig, accountConfig, env, syncTasks); }; try { let availableSyncTasks = await getAvailableSyncTypes( diff --git a/lib/localDev.ts b/lib/localDev.ts index 0f9d7ebd2..20d5b02b9 100644 --- a/lib/localDev.ts +++ b/lib/localDev.ts @@ -223,13 +223,13 @@ const createSandboxForLocalDev = async (accountId, accountConfig, env) => { sandboxAccountConfig ); // For v1 sandboxes, keep sync here. Once we migrate to v2, this will be handled by BE automatically - await syncSandbox({ - accountConfig: sandboxAccountConfig, - parentAccountConfig: accountConfig, + await syncSandbox( + sandboxAccountConfig, + accountConfig, env, syncTasks, - slimInfoMessage: true, - }); + true + ); return targetAccountId; } catch (err) { logError(err); diff --git a/lib/sandboxSync.ts b/lib/sandboxSync.ts index 0b6587d3b..2fc927f5e 100644 --- a/lib/sandboxSync.ts +++ b/lib/sandboxSync.ts @@ -1,50 +1,50 @@ -// @ts-nocheck -const SpinniesManager = require('./ui/SpinniesManager'); -const { getHubSpotWebsiteOrigin } = require('@hubspot/local-dev-lib/urls'); -const { logger } = require('@hubspot/local-dev-lib/logger'); -const { i18n } = require('./lang'); -const { getAvailableSyncTypes } = require('./sandboxes'); -const { initiateSync } = require('@hubspot/local-dev-lib/api/sandboxSync'); -const { - debugError, - logError, - ApiErrorContext, -} = require('./errorHandlers/index'); -const { isSpecifiedError } = require('@hubspot/local-dev-lib/errors/index'); -const { getSandboxTypeAsString } = require('./sandboxes'); -const { getAccountId } = require('@hubspot/local-dev-lib/config'); -const { - getAccountIdentifier, -} = require('@hubspot/local-dev-lib/config/getAccountIdentifier'); -const { +import SpinniesManager from './ui/SpinniesManager'; +import { getHubSpotWebsiteOrigin } from '@hubspot/local-dev-lib/urls'; +import { logger } from '@hubspot/local-dev-lib/logger'; +import { initiateSync } from '@hubspot/local-dev-lib/api/sandboxSync'; +import { isSpecifiedError } from '@hubspot/local-dev-lib/errors/index'; +import { getAccountId } from '@hubspot/local-dev-lib/config'; +import { getAccountIdentifier } from '@hubspot/local-dev-lib/config/getAccountIdentifier'; +import { CLIAccount } from '@hubspot/local-dev-lib/types/Accounts'; +import { Environment } from '@hubspot/local-dev-lib/types/Config'; + +import { i18n } from './lang'; +import { getAvailableSyncTypes } from './sandboxes'; +import { debugError, logError, ApiErrorContext } from './errorHandlers/index'; +import { getSandboxTypeAsString } from './sandboxes'; +import { uiAccountDescription, uiLine, uiLink, uiCommandDisabledBanner, -} = require('./ui'); -const { isDevelopmentSandbox } = require('./accountTypes'); +} from './ui'; +import { isDevelopmentSandbox } from './accountTypes'; +import { SandboxSyncTask } from '../types/Sandboxes'; const i18nKey = 'lib.sandbox.sync'; -/** - * @param {Object} accountConfig - Account config of sandbox portal - * @param {Object} parentAccountConfig - Account config of parent portal - * @param {String} env - Environment (QA/Prod) - * @param {Array} syncTasks - Array of available sync tasks - * @returns - */ -const syncSandbox = async ({ - accountConfig, - parentAccountConfig, - env, - syncTasks, - slimInfoMessage = false, -}) => { +export async function syncSandbox( + accountConfig: CLIAccount, + parentAccountConfig: CLIAccount, + env: Environment, + syncTasks: Array, + slimInfoMessage = false +) { const id = getAccountIdentifier(accountConfig); const accountId = getAccountId(id); const parentId = getAccountIdentifier(parentAccountConfig); const parentAccountId = getAccountId(parentId); const isDevSandbox = isDevelopmentSandbox(accountConfig); + + if (!accountId || !parentAccountId) { + throw new Error( + i18n(`${i18nKey}.failure.invalidUser`, { + accountName: uiAccountDescription(accountId), + parentAccountName: uiAccountDescription(parentAccountId), + }) + ); + } + SpinniesManager.init({ succeedColor: 'white', }); @@ -190,8 +190,4 @@ const syncSandbox = async ({ uiLine(); logger.log(); } -}; - -module.exports = { - syncSandbox, -}; +} diff --git a/lib/sandboxes.ts b/lib/sandboxes.ts index c05d97bfb..229362fd2 100644 --- a/lib/sandboxes.ts +++ b/lib/sandboxes.ts @@ -15,11 +15,12 @@ import { } from '@hubspot/local-dev-lib/errors/index'; import { getValidEnv } from '@hubspot/local-dev-lib/environment'; import { AccountType, CLIAccount } from '@hubspot/local-dev-lib/types/Accounts'; +import { Environment } from '@hubspot/local-dev-lib/types/Config'; import { i18n } from './lang'; import { uiAccountDescription } from './ui'; import { logError } from './errorHandlers/index'; -import { Environment } from '@hubspot/local-dev-lib/types/Config'; +import { SandboxSyncTask } from '../types/Sandboxes'; const i18nKey = 'lib.sandbox'; @@ -39,7 +40,7 @@ export const SANDBOX_API_TYPE_MAP = { [HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX]: 2, } as const; -export function getSandboxTypeAsString(accountType: AccountType): string { +export function getSandboxTypeAsString(accountType?: AccountType): string { if (accountType === HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX) { return 'development'; // Only place we're using this specific name } @@ -89,7 +90,7 @@ function getSandboxLimit(error: unknown): number { export async function getAvailableSyncTypes( parentAccountConfig: CLIAccount, config: CLIAccount -): Promise> { +): Promise> { const parentId = getAccountIdentifier(parentAccountConfig); const parentPortalId = getAccountId(parentId); const id = getAccountIdentifier(config); diff --git a/types/Sandboxes.ts b/types/Sandboxes.ts new file mode 100644 index 000000000..3b58ecfc5 --- /dev/null +++ b/types/Sandboxes.ts @@ -0,0 +1,3 @@ +export type SandboxSyncTask = { + type: string; +};