Skip to content

Commit

Permalink
Project dev supports dev test accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Wang committed Feb 13, 2024
1 parent 396a417 commit 4adea5e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 37 deletions.
60 changes: 38 additions & 22 deletions packages/cli/commands/accounts/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ const {
} = require('../../lib/commonOpts');
const { trackCommandUsage } = require('../../lib/usageTracking');
const { loadAndValidateOptions } = require('../../lib/validation');
const { getSandboxTypeAsString } = require('../../lib/sandboxes');
const { getSandboxTypeAsString, isSandbox } = require('../../lib/sandboxes');
const {
isDeveloperTestAccount,
DEV_TEST_ACCOUNT_STRING,
} = require('../../lib/developerTestAccounts');
const { i18n } = require('../../lib/lang');
const {
HUBSPOT_ACCOUNT_TYPES,
} = require('@hubspot/local-dev-lib/constants/config');

const i18nKey = 'cli.commands.accounts.subcommands.list';

Expand All @@ -22,29 +29,32 @@ exports.describe = i18n(`${i18nKey}.describe`);

const sortAndMapPortals = portals => {
const mappedPortalData = {};
// Standard and app developer portals
portals
.sort((a, b) => {
if (a.sandboxAccountType === null && b.sandboxAccountType !== null) {
return -1;
}
if (a.sandboxAccountType !== null && b.sandboxAccountType === null) {
return 1;
}
return 0;
})
.filter(p =>
p.accountType
? p.accountType === HUBSPOT_ACCOUNT_TYPES.STANDARD ||
p.accountType === HUBSPOT_ACCOUNT_TYPES.APP_DEVELOPER
: p.sandboxAccountType === null
)
.forEach(portal => {
if (
portal.sandboxAccountType !== undefined &&
portal.sandboxAccountType === null
) {
mappedPortalData[portal.portalId] = [portal];
} else if (portal.sandboxAccountType && portal.parentAccountId) {
mappedPortalData[portal.parentAccountId] = [
...(mappedPortalData[portal.parentAccountId] || []),
portal,
mappedPortalData[portal.portalId] = [portal];
});
// Non-standard portals (sandbox, developer test account)
portals
.filter(p =>
p.accountType
? isSandbox(p) || isDeveloperTestAccount(p)
: p.sandboxAccountType !== null
)
.forEach(p => {
if (p.parentAccountId) {
mappedPortalData[p.parentAccountId] = [
...(mappedPortalData[p.parentAccountId] || []),
p,
];
} else {
mappedPortalData[portal.portalId] = [portal];
mappedPortalData[p.portalId] = [p];
}
});
return mappedPortalData;
Expand All @@ -56,14 +66,20 @@ const getPortalData = mappedPortalData => {
set.forEach((portal, i) => {
if (i === 0) {
portalData.push([portal.name, portal.portalId, portal.authType]);
} else {
} else if (isSandbox(portal)) {
portalData.push([
`↳ ${portal.name} [${getSandboxTypeAsString(
portal.sandboxAccountType
portal.accountType || portal.sandboxAccountType
)} sandbox]`,
portal.portalId,
portal.authType,
]);
} else if (isDeveloperTestAccount(portal)) {
portalData.push([
`↳ ${portal.name} [${DEV_TEST_ACCOUNT_STRING}]`,
portal.portalId,
portal.authType,
]);
}
});
});
Expand Down
26 changes: 19 additions & 7 deletions packages/cli/commands/project/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ const {
const { confirmPrompt } = require('../../lib/prompts/promptUtils');
const {
selectTargetAccountPrompt,
confirmDefaultSandboxAccountPrompt,
confirmDefaultAccountPrompt,
} = require('../../lib/prompts/projectDevTargetAccountPrompt');
const SpinniesManager = require('../../lib/SpinniesManager');
const LocalDevManager = require('../../lib/LocalDevManager');
const { isSandbox } = require('../../lib/sandboxes');
const { isSandbox, getSandboxTypeAsString } = require('../../lib/sandboxes');
const { sandboxNamePrompt } = require('../../lib/prompts/sandboxesPrompt');
const {
validateSandboxUsageLimits,
Expand All @@ -67,6 +67,10 @@ const {
isMissingScopeError,
} = require('@hubspot/local-dev-lib/errors/apiErrors');
const { logErrorInstance } = require('../../lib/errorHandlers/standardErrors');
const {
isDeveloperTestAccount,
DEV_TEST_ACCOUNT_STRING,
} = require('../../lib/developerTestAccounts');

const i18nKey = 'cli.commands.project.subcommands.dev';

Expand Down Expand Up @@ -96,19 +100,27 @@ exports.handler = async options => {
let targetAccountId = options.account ? accountId : null;
let createNewSandbox = false;
const defaultAccountIsSandbox = isSandbox(accountConfig);
const defaultAccountIsDeveloperTestAccount = isDeveloperTestAccount(
accountConfig
);

if (!targetAccountId && defaultAccountIsSandbox) {
if (
!targetAccountId &&
(defaultAccountIsSandbox || defaultAccountIsDeveloperTestAccount)
) {
logger.log();
const useDefaultSandboxAccount = await confirmDefaultSandboxAccountPrompt(
const useDefaultAccount = await confirmDefaultAccountPrompt(
accountConfig.name,
accountConfig.sandboxAccountType
defaultAccountIsSandbox
? `${getSandboxTypeAsString(accountConfig.accountType)} sandbox`
: DEV_TEST_ACCOUNT_STRING
);

if (useDefaultSandboxAccount) {
if (useDefaultAccount) {
targetAccountId = accountId;
} else {
logger.log(
i18n(`${i18nKey}.logs.declineDefaultSandboxExplanation`, {
i18n(`${i18nKey}.logs.declineDefaultAccountExplanation`, {
useCommand: uiCommandReference('hs accounts use'),
devCommand: uiCommandReference('hs project dev'),
})
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ en:
projectMustExistExplanation: "The project {{ projectName }} does not exist in the target account {{ accountIdentifier}}. This command requires the project to exist in the target account."
choseNotToCreateProject: "Exiting because this command requires the project to exist in the target account."
initialUploadMessage: "HubSpot Local Dev Server Startup"
declineDefaultSandboxExplanation: "To develop on a different account, run {{ useCommand }} to change your default account, then re-run {{ devCommand }}."
declineDefaultAccountExplanation: "To develop on a different account, run {{ useCommand }} to change your default account, then re-run {{ devCommand }}."
status:
creatingProject: "Creating project {{ projectName }} in {{ accountIdentifier }}"
createdProject: "Created project {{ projectName }} in {{ accountIdentifier }}"
Expand Down Expand Up @@ -1028,7 +1028,7 @@ en:
promptMessage: "[--account] Choose a sandbox under {{ accountIdentifier }} to test with:"
sandboxLimit: "Your account reached the limit of {{ limit }} development sandboxes"
sandboxLimitWithSuggestion: "Your account reached the limit of {{ limit }} development sandboxes. Run {{ authCommand }} to add an existing one to the config."
confirmDefaultSandboxAccount: "Continue testing on {{#bold}}{{ accountName }} ({{ accountType }}){{/bold}}? (Y/n)"
confirmDefaultAccount: "Continue testing on {{#bold}}{{ accountName }} ({{ accountType }}){{/bold}}? (Y/n)"
projectLogsPrompt:
projectName:
message: "[--project] Enter the project name:"
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/lib/developerTestAccounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const {
HUBSPOT_ACCOUNT_TYPES,
} = require('@hubspot/local-dev-lib/constants/config');

const DEV_TEST_ACCOUNT_STRING = 'dev test account';

const isDeveloperTestAccount = config =>
config.accountType &&
config.accountType === HUBSPOT_ACCOUNT_TYPES.DEVELOPER_TEST;

module.exports = {
DEV_TEST_ACCOUNT_STRING,
isDeveloperTestAccount,
};
6 changes: 3 additions & 3 deletions packages/cli/lib/prompts/projectDevTargetAccountPrompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ const selectTargetAccountPrompt = async (accounts, defaultAccountConfig) => {
return targetAccountInfo;
};

const confirmDefaultSandboxAccountPrompt = async (accountName, accountType) => {
const confirmDefaultAccountPrompt = async (accountName, accountType) => {
const { useDefaultAccount } = await promptUser([
{
name: 'useDefaultAccount',
type: 'confirm',
message: i18n(`${i18nKey}.confirmDefaultSandboxAccount`, {
message: i18n(`${i18nKey}.confirmDefaultAccount`, {
accountName,
accountType,
}),
Expand All @@ -101,5 +101,5 @@ const confirmDefaultSandboxAccountPrompt = async (accountName, accountType) => {

module.exports = {
selectTargetAccountPrompt,
confirmDefaultSandboxAccountPrompt,
confirmDefaultAccountPrompt,
};
23 changes: 20 additions & 3 deletions packages/cli/lib/sandboxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const {
personalAccessKeyPrompt,
} = require('./prompts/personalAccessKeyPrompt');
const { logErrorInstance } = require('./errorHandlers/standardErrors');
const {
HUBSPOT_ACCOUNT_TYPES,
} = require('@hubspot/local-dev-lib/constants/config');

const STANDARD_SANDBOX = 'standard';
const DEVELOPER_SANDBOX = 'developer';
Expand All @@ -51,11 +54,24 @@ const sandboxApiTypeMap = {
developer: 2,
};

const getSandboxTypeAsString = type =>
type === 'DEVELOPER' ? 'development' : 'standard';
const getSandboxTypeAsString = accountType => {
if (
accountType === HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX ||
accountType === 'DEVELOPER'
) {
return 'development';
}
return 'standard';
};

const isSandbox_OLD = config =>
config.sandboxAccountType && config.sandboxAccountType !== null;

const isSandbox = config =>
config.sandboxAccountType && config.sandboxAccountType !== null;
config.accountType
? config.accountType === HUBSPOT_ACCOUNT_TYPES.STANDARD_SANDBOX ||
config.accountType === HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX
: isSandbox_OLD(config);

function getAccountName(config, bold = true) {
const sandboxName = `[${getSandboxTypeAsString(
Expand Down Expand Up @@ -445,6 +461,7 @@ module.exports = {
sandboxApiTypeMap,
syncTypes,
isSandbox,
isSandbox_OLD,
getSandboxTypeAsString,
getAccountName,
saveSandboxToConfig,
Expand Down

0 comments on commit 4adea5e

Please sign in to comment.