Skip to content

Commit

Permalink
Convert sandbox sync to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
camden11 committed Jan 10, 2025
1 parent 680d155 commit 1281aa7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 54 deletions.
7 changes: 1 addition & 6 deletions commands/sandbox/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions lib/localDev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
76 changes: 36 additions & 40 deletions lib/sandboxSync.ts
Original file line number Diff line number Diff line change
@@ -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<SandboxSyncTask>,
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',
});
Expand Down Expand Up @@ -190,8 +190,4 @@ const syncSandbox = async ({
uiLine();
logger.log();
}
};

module.exports = {
syncSandbox,
};
}
7 changes: 4 additions & 3 deletions lib/sandboxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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
}
Expand Down Expand Up @@ -89,7 +90,7 @@ function getSandboxLimit(error: unknown): number {
export async function getAvailableSyncTypes(
parentAccountConfig: CLIAccount,
config: CLIAccount
): Promise<Array<{ type: string }>> {
): Promise<Array<SandboxSyncTask>> {
const parentId = getAccountIdentifier(parentAccountConfig);
const parentPortalId = getAccountId(parentId);
const id = getAccountIdentifier(config);
Expand Down
3 changes: 3 additions & 0 deletions types/Sandboxes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type SandboxSyncTask = {
type: string;
};

0 comments on commit 1281aa7

Please sign in to comment.