Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Convert simple prompts to TypeScript #1273

Merged
merged 9 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions commands/account/list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @ts-nocheck
const { logger } = require('@hubspot/local-dev-lib/logger');
const {
getConfig,
getConfigPath,
getConfigDefaultAccount,
getConfigAccounts,
Expand Down Expand Up @@ -88,7 +87,6 @@ exports.handler = async options => {

trackCommandUsage('accounts-list', null, derivedAccountId);

const config = getConfig();
const configPath = getConfigPath();
const accountsList = getConfigAccounts();
const mappedPortalData = sortAndMapPortals(accountsList);
Expand All @@ -104,7 +102,7 @@ exports.handler = async options => {
logger.log(i18n(`${i18nKey}.configPath`, { configPath }));
logger.log(
i18n(`${i18nKey}.defaultAccount`, {
account: getConfigDefaultAccount(config),
account: getConfigDefaultAccount(),
})
);
logger.log(i18n(`${i18nKey}.accounts`));
Expand Down
9 changes: 3 additions & 6 deletions commands/account/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const { addConfigOptions } = require('../../lib/commonOpts');
const { logger } = require('@hubspot/local-dev-lib/logger');
const {
getConfig,
loadConfig,
getConfigPath,
deleteAccount,
getConfigDefaultAccount,
Expand All @@ -25,8 +25,6 @@ exports.handler = async options => {
const { account } = options;
let accountToRemove = account;

let config = getConfig();

if (accountToRemove && !getAccountIdFromConfig(accountToRemove)) {
logger.error(
i18n(`${i18nKey}.errors.accountNotFound`, {
Expand All @@ -38,7 +36,6 @@ exports.handler = async options => {

if (!accountToRemove || !getAccountIdFromConfig(accountToRemove)) {
accountToRemove = await selectAccountFromConfig(
config,
i18n(`${i18nKey}.prompts.selectAccountToRemove`)
);
}
Expand All @@ -59,12 +56,12 @@ exports.handler = async options => {
);

// Get updated version of the config
config = getConfig();
loadConfig(getConfigPath(), options);

if (accountToRemove === currentDefaultAccount) {
logger.log();
logger.log(i18n(`${i18nKey}.logs.replaceDefaultAccount`));
const newDefaultAccount = await selectAccountFromConfig(config);
const newDefaultAccount = await selectAccountFromConfig();
updateDefaultAccount(newDefaultAccount);
}
};
Expand Down
7 changes: 2 additions & 5 deletions commands/account/use.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @ts-nocheck
const { logger } = require('@hubspot/local-dev-lib/logger');
const {
getConfig,
getConfigPath,
updateDefaultAccount,
getAccountId: getAccountIdFromConfig,
Expand All @@ -20,20 +19,18 @@ exports.describe = i18n(`${i18nKey}.describe`);
exports.handler = async options => {
await loadAndValidateOptions(options, false);

const config = getConfig();

let newDefaultAccount = options.account;

if (!newDefaultAccount) {
newDefaultAccount = await selectAccountFromConfig(config);
newDefaultAccount = await selectAccountFromConfig();
} else if (!getAccountIdFromConfig(newDefaultAccount)) {
logger.error(
i18n(`${i18nKey}.errors.accountNotFound`, {
specifiedAccount: newDefaultAccount,
configPath: getConfigPath(),
})
);
newDefaultAccount = await selectAccountFromConfig(config);
newDefaultAccount = await selectAccountFromConfig();
}

trackCommandUsage(
Expand Down
4 changes: 1 addition & 3 deletions commands/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const {
const {
updateAccountConfig,
writeConfig,
getConfig,
getConfigPath,
loadConfig,
getConfigDefaultAccount,
Expand Down Expand Up @@ -180,10 +179,9 @@ exports.handler = async options => {
})
);
} else {
const config = getConfig();
logger.info(
i18n(`lib.prompts.setAsDefaultAccountPrompt.keepingCurrentDefault`, {
accountName: getConfigDefaultAccount(config),
accountName: getConfigDefaultAccount(),
})
);
}
Expand Down
12 changes: 5 additions & 7 deletions commands/sandbox/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const { deleteSandbox } = require('@hubspot/local-dev-lib/api/sandboxHubs');
const { i18n } = require('../../lib/lang');
const { deleteSandboxPrompt } = require('../../lib/prompts/sandboxesPrompt');
const {
getConfig,
getEnv,
removeSandboxAccountFromConfig,
updateDefaultAccount,
Expand Down Expand Up @@ -45,14 +44,13 @@ exports.handler = async options => {
await loadAndValidateOptions(options, false);

const { providedAccountId, force } = options;
const config = getConfig();

trackCommandUsage('sandbox-delete', null);

let accountPrompt;
if (!providedAccountId) {
if (!force) {
accountPrompt = await deleteSandboxPrompt(config);
accountPrompt = await deleteSandboxPrompt();
} else {
// Account is required, throw error if force flag is present and no account is specified
logger.log('');
Expand All @@ -70,7 +68,7 @@ exports.handler = async options => {
account: providedAccountId || accountPrompt.account,
});
const isDefaultAccount =
sandboxAccountId === getAccountId(getConfigDefaultAccount(config));
sandboxAccountId === getAccountId(getConfigDefaultAccount());

const baseUrl = getHubSpotWebsiteOrigin(
getValidEnv(getEnv(sandboxAccountId))
Expand All @@ -83,7 +81,7 @@ exports.handler = async options => {
if (portal.parentAccountId) {
parentAccountId = portal.parentAccountId;
} else if (!force) {
const parentAccountPrompt = await deleteSandboxPrompt(config, true);
const parentAccountPrompt = await deleteSandboxPrompt(true);
parentAccountId = getAccountId({
account: parentAccountPrompt.account,
});
Expand Down Expand Up @@ -161,7 +159,7 @@ exports.handler = async options => {
sandboxAccountId
);
if (promptDefaultAccount && !force) {
const newDefaultAccount = await selectAccountFromConfig(getConfig());
const newDefaultAccount = await selectAccountFromConfig();
updateDefaultAccount(newDefaultAccount);
} else {
// If force is specified, skip prompt and set the parent account id as the default account
Expand Down Expand Up @@ -214,7 +212,7 @@ exports.handler = async options => {
sandboxAccountId
);
if (promptDefaultAccount && !force) {
const newDefaultAccount = await selectAccountFromConfig(getConfig());
const newDefaultAccount = await selectAccountFromConfig();
updateDefaultAccount(newDefaultAccount);
} else {
// If force is specified, skip prompt and set the parent account id as the default account
Expand Down
10 changes: 8 additions & 2 deletions commands/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const {
getCmsPublishMode,
} = require('../lib/commonOpts');
const { uploadPrompt } = require('../lib/prompts/uploadPrompt');
const { cleanUploadPrompt } = require('../lib/prompts/cleanUploadPrompt');
const { confirmPrompt } = require('../lib/prompts/promptUtils');
const {
validateCmsPublishMode,
loadAndValidateOptions,
Expand Down Expand Up @@ -218,7 +218,13 @@ exports.handler = async options => {
// If clean is true, will first delete the dest folder and then upload src. Cleans up files that only exist on HS.
let cleanUpload = options.force;
if (!options.force) {
cleanUpload = await cleanUploadPrompt(derivedAccountId, dest);
cleanUpload = await confirmPrompt(
i18n(`${i18nKey}.confirmCleanUpload`, {
accountId: derivedAccountId,
path: dest,
}),
{ defaultAnswer: false }
);
}
if (cleanUpload) {
try {
Expand Down
3 changes: 1 addition & 2 deletions lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ en:
uploading: "Uploading files from \"{{ src }}\" to \"{{ dest }}\" in the Design Manager of account {{ accountId }}"
notUploaded: "There was an error processing \"{{ src }}\". The file has not been uploaded."
cleaning: "Removing \"{{ filePath }}\" from account {{ accountId }} and uploading local..."
confirmCleanUpload: "You are about to remove any remote files in \"{{ filePath }}\" on HubSpot account {{ accountId }} that don't exist locally. Are you sure you want to do this?"
watch:
describe: "Watch a directory on your computer for changes and upload the changed files to the HubSpot CMS."
errors:
Expand Down Expand Up @@ -1355,8 +1356,6 @@ en:
errors:
srcRequired: "You must specify a source directory."
destRequired: "You must specify a destination directory."
cleanUploadPrompt:
message: "You are about to remove any remote files in \"{{ filePath }}\" on HubSpot account {{ accountId }} that don't exist locally. Are you sure you want to do this?"
installPublicAppPrompt:
explanation: "Local development requires this app to be installed in the target test account"
reinstallExplanation: "This app's required scopes have been updated since it was last installed on the target test account. To avoid issues with local development, we recommend reinstalling the app with the updated scopes."
Expand Down
89 changes: 50 additions & 39 deletions lib/prompts/accountNamePrompt.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
// @ts-nocheck
const { accountNameExistsInConfig } = require('@hubspot/local-dev-lib/config');
const { promptUser } = require('./promptUtils');
const { i18n } = require('../lang');
const {
HUBSPOT_ACCOUNT_TYPES,
} = require('@hubspot/local-dev-lib/constants/config');
import { accountNameExistsInConfig } from '@hubspot/local-dev-lib/config';
import { promptUser } from './promptUtils';
import { i18n } from '../lang';
import { PromptConfig } from '../../types/prompts';
import { HUBSPOT_ACCOUNT_TYPES } from '@hubspot/local-dev-lib/constants/config';
import { AccountType } from '@hubspot/local-dev-lib/types/Accounts';

const i18nKey = 'lib.prompts.accountNamePrompt';

const getCliAccountNamePromptConfig = defaultName => ({
name: 'name',
message: i18n(`${i18nKey}.enterAccountName`),
default: defaultName,
validate(val) {
if (typeof val !== 'string') {
return i18n(`${i18nKey}.errors.invalidName`);
} else if (!val.length) {
return i18n(`${i18nKey}.errors.nameRequired`);
} else if (val.indexOf(' ') >= 0) {
return i18n(`${i18nKey}.errors.spacesInName`);
}
return accountNameExistsInConfig(val)
? i18n(`${i18nKey}.errors.accountNameExists`, { name: val })
: true;
},
});

const cliAccountNamePrompt = defaultName => {
return promptUser(getCliAccountNamePromptConfig(defaultName));
type AccountNamePromptResponse = {
name: string;
};

const hubspotAccountNamePrompt = ({ accountType, currentPortalCount = 0 }) => {
export function getCliAccountNamePromptConfig(
defaultName: string
): PromptConfig<AccountNamePromptResponse> {
return {
name: 'name',
message: i18n(`${i18nKey}.enterAccountName`),
default: defaultName,
validate(val?: string) {
if (typeof val !== 'string') {
return i18n(`${i18nKey}.errors.invalidName`);
} else if (!val.length) {
return i18n(`${i18nKey}.errors.nameRequired`);
} else if (val.indexOf(' ') >= 0) {
return i18n(`${i18nKey}.errors.spacesInName`);
}
return accountNameExistsInConfig(val)
? i18n(`${i18nKey}.errors.accountNameExists`, { name: val })
: true;
},
};
}

export function cliAccountNamePrompt(
defaultName: string
): Promise<AccountNamePromptResponse> {
return promptUser<AccountNamePromptResponse>(
getCliAccountNamePromptConfig(defaultName)
);
}

export function hubspotAccountNamePrompt({
accountType,
currentPortalCount = 0,
}: {
accountType: AccountType;
currentPortalCount?: number;
}): Promise<AccountNamePromptResponse> {
const isDevelopmentSandbox =
accountType === HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX;
const isSandbox =
Expand All @@ -39,8 +56,8 @@ const hubspotAccountNamePrompt = ({ accountType, currentPortalCount = 0 }) => {
const isDeveloperTestAccount =
accountType === HUBSPOT_ACCOUNT_TYPES.DEVELOPER_TEST;

let promptMessageString;
let defaultName;
let promptMessageString: string | undefined;
let defaultName: string | undefined;
if (isSandbox) {
promptMessageString = isDevelopmentSandbox
? i18n(`${i18nKey}.enterDevelopmentSandboxName`)
Expand All @@ -52,11 +69,11 @@ const hubspotAccountNamePrompt = ({ accountType, currentPortalCount = 0 }) => {
});
}

return promptUser([
return promptUser<AccountNamePromptResponse>([
{
name: 'name',
message: promptMessageString,
validate(val) {
validate(val?: string) {
if (typeof val !== 'string') {
return i18n(`${i18nKey}.errors.invalidName`);
} else if (!val.trim().length) {
Expand All @@ -69,10 +86,4 @@ const hubspotAccountNamePrompt = ({ accountType, currentPortalCount = 0 }) => {
default: defaultName,
},
]);
};

module.exports = {
getCliAccountNamePromptConfig,
cliAccountNamePrompt,
hubspotAccountNamePrompt,
};
}
4 changes: 2 additions & 2 deletions lib/prompts/accountsPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const mapAccountChoices = portals =>

const i18nKey = 'commands.account.subcommands.use';

const selectAccountFromConfig = async (config, prompt) => {
const selectAccountFromConfig = async (prompt = '') => {
const accountsList = getConfigAccounts();
const defaultAccount = getConfigDefaultAccount(config);
const defaultAccount = getConfigDefaultAccount();

const { default: selectedDefault } = await promptUser([
{
Expand Down
21 changes: 0 additions & 21 deletions lib/prompts/cleanUploadPrompt.ts

This file was deleted.

Loading
Loading