Skip to content

Commit

Permalink
Merge branch 'next' into br/lib-ts-port-2
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenrodgers committed Dec 20, 2024
2 parents fedda8a + a20161b commit 574f829
Show file tree
Hide file tree
Showing 56 changed files with 81 additions and 216 deletions.
76 changes: 68 additions & 8 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
getCommandName,
injectAccountIdMiddleware,
} = require('../lib/commonOpts');
const { validateAccount } = require('../lib/validation');
const {
trackHelpUsage,
trackConvertFieldsUsage,
Expand Down Expand Up @@ -147,18 +148,40 @@ const setRequestHeaders = () => {
addUserAgentHeader('HubSpot CLI', pkg.version);
};

const NO_CONFIG_VALIDATION = {
init: { skip: true },
auth: { skip: true },
const isTargetedCommand = (options, commandMap) => {
const checkCommand = (options, commandMap) => {
const currentCommand = options._[0];

if (!commandMap[currentCommand]) {
return false;
}

if (commandMap[currentCommand].target) {
return true;
}

const subCommands = commandMap[currentCommand].subCommands || {};
if (options._.length > 1) {
return checkCommand({ _: options._.slice(1) }, subCommands);
}

return false;
};

return checkCommand(options, commandMap);
};

const SKIP_CONFIG_VALIDATION = {
init: { target: true },
auth: { target: true },
};

const loadConfigMiddleware = async options => {
const maybeValidateConfig = () => {
const shouldValidate = options._.every(
command =>
!(NO_CONFIG_VALIDATION[command] && NO_CONFIG_VALIDATION[command].skip)
);
if (shouldValidate && !validateConfig()) {
if (
!isTargetedCommand(options, SKIP_CONFIG_VALIDATION) &&
!validateConfig()
) {
process.exit(EXIT_CODES.ERROR);
}
};
Expand All @@ -182,6 +205,42 @@ const checkAndWarnGitInclusionMiddleware = () => {
checkAndWarnGitInclusion(getConfigPath());
};

const accountsSubCommands = {
target: false,
subCommands: {
clean: { target: true },
list: { target: true },
ls: { target: true },
remove: { target: true },
},
};
const sandboxesSubCommands = {
target: false,
subCommands: {
delete: { target: true },
},
};

const SKIP_ACCOUNT_VALIDATION = {
init: { target: true },
auth: { target: true },
account: accountsSubCommands,
accounts: accountsSubCommands,
sandbox: sandboxesSubCommands,
sandboxes: sandboxesSubCommands,
};

const validateAccountOptions = async options => {
let validAccount = true;
if (!isTargetedCommand(options, SKIP_ACCOUNT_VALIDATION)) {
validAccount = await validateAccount(options);
}

if (!validAccount) {
process.exit(EXIT_CODES.ERROR);
}
};

const argv = yargs
.usage('The command line interface to interact with HubSpot.')
// loadConfigMiddleware loads the new hidden config for all commands
Expand All @@ -191,6 +250,7 @@ const argv = yargs
loadConfigMiddleware,
injectAccountIdMiddleware,
checkAndWarnGitInclusionMiddleware,
validateAccountOptions,
])
.exitProcess(false)
.fail(handleFailure)
Expand Down
2 changes: 0 additions & 2 deletions commands/account/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const {

const { trackCommandUsage } = require('../../lib/usageTracking');
const { i18n } = require('../../lib/lang');
const { loadAndValidateOptions } = require('../../lib/validation');
const { EXIT_CODES } = require('../../lib/enums/exitCodes');
const { addTestingOptions, addConfigOptions } = require('../../lib/commonOpts');
const { promptUser } = require('../../lib/prompts/promptUtils');
Expand All @@ -29,7 +28,6 @@ exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
const { qa } = options;
await loadAndValidateOptions(options, false);

trackCommandUsage('accounts-clean', null);

Expand Down
3 changes: 0 additions & 3 deletions commands/account/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const { logger } = require('@hubspot/local-dev-lib/logger');
const { getAccountConfig } = require('@hubspot/local-dev-lib/config');
const { getAccessToken } = require('@hubspot/local-dev-lib/personalAccessKey');
const { addConfigOptions } = require('../../lib/commonOpts');
const { loadAndValidateOptions } = require('../../lib/validation');
const { i18n } = require('../../lib/lang');
const { getTableContents } = require('../../lib/ui/table');

Expand All @@ -13,8 +12,6 @@ exports.describe = i18n(`${i18nKey}.describe`);
exports.command = 'info [account]';

exports.handler = async options => {
await loadAndValidateOptions(options);

const { derivedAccountId } = options;
const config = getAccountConfig(derivedAccountId);
// check if the provided account is using a personal access key, if not, show an error
Expand Down
3 changes: 0 additions & 3 deletions commands/account/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const { addConfigOptions } = require('../../lib/commonOpts');
const { getTableContents, getTableHeader } = require('../../lib/ui/table');

const { trackCommandUsage } = require('../../lib/usageTracking');
const { loadAndValidateOptions } = require('../../lib/validation');
const { isSandbox, isDeveloperTestAccount } = require('../../lib/accountTypes');

const { i18n } = require('../../lib/lang');
Expand Down Expand Up @@ -81,8 +80,6 @@ const getPortalData = mappedPortalData => {
};

exports.handler = async options => {
await loadAndValidateOptions(options, false);

const { derivedAccountId } = options;

trackCommandUsage('accounts-list', null, derivedAccountId);
Expand Down
2 changes: 0 additions & 2 deletions commands/account/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ const {
const { trackCommandUsage } = require('../../lib/usageTracking');
const { i18n } = require('../../lib/lang');
const { selectAccountFromConfig } = require('../../lib/prompts/accountsPrompt');
const { loadAndValidateOptions } = require('../../lib/validation');

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

exports.command = 'remove [account]';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
await loadAndValidateOptions(options, false);
const { account } = options;
let accountToRemove = account;

Expand Down
3 changes: 0 additions & 3 deletions commands/account/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { renameAccount } = require('@hubspot/local-dev-lib/config');

const { addConfigOptions, addAccountOptions } = require('../../lib/commonOpts');
const { trackCommandUsage } = require('../../lib/usageTracking');
const { loadAndValidateOptions } = require('../../lib/validation');
const { i18n } = require('../../lib/lang');

const i18nKey = 'commands.account.subcommands.rename';
Expand All @@ -13,8 +12,6 @@ exports.command = 'rename <accountName> <newName>';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
loadAndValidateOptions(options);

const { accountName, newName, derivedAccountId } = options;

trackCommandUsage('accounts-rename', null, derivedAccountId);
Expand Down
3 changes: 0 additions & 3 deletions commands/account/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ const {
const { trackCommandUsage } = require('../../lib/usageTracking');
const { i18n } = require('../../lib/lang');
const { selectAccountFromConfig } = require('../../lib/prompts/accountsPrompt');
const { loadAndValidateOptions } = require('../../lib/validation');

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

exports.command = 'use [account]';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
await loadAndValidateOptions(options, false);

let newDefaultAccount = options.account;

if (!newDefaultAccount) {
Expand Down
2 changes: 0 additions & 2 deletions commands/cms/lighthouseScore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
} = require('../../lib/commonOpts');
const { logger } = require('@hubspot/local-dev-lib/logger');
const { getTableContents, getTableHeader } = require('../../lib/ui/table');
const { loadAndValidateOptions } = require('../../lib/validation');
const { promptUser } = require('../../lib/prompts/promptUtils');
const { i18n } = require('../../lib/lang');
const { fetchThemes } = require('@hubspot/local-dev-lib/api/designManager');
Expand Down Expand Up @@ -67,7 +66,6 @@ const selectTheme = async accountId => {
};

exports.handler = async options => {
await loadAndValidateOptions(options);
const { target, verbose, theme, derivedAccountId } = options;

const includeDesktopScore = target === 'desktop' || !verbose;
Expand Down
3 changes: 0 additions & 3 deletions commands/config/set.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-nocheck
const { loadAndValidateOptions } = require('../../lib/validation');
const { i18n } = require('../../lib/lang');
const { trackCommandUsage } = require('../../lib/usageTracking');
const { promptUser } = require('../../lib/prompts/promptUtils');
Expand Down Expand Up @@ -55,8 +54,6 @@ const handleConfigUpdate = async (accountId, options) => {
};

exports.handler = async options => {
await loadAndValidateOptions(options);

const { derivedAccountId } = options;

trackCommandUsage('config-set', null, derivedAccountId);
Expand Down
7 changes: 1 addition & 6 deletions commands/customObject/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { inputPrompt } from '../../lib/prompts/promptUtils';
const { logger } = require('@hubspot/local-dev-lib/logger');
const { logError } = require('../../lib/errorHandlers/index');
const { getAbsoluteFilePath } = require('@hubspot/local-dev-lib/path');
const {
checkAndConvertToJson,
loadAndValidateOptions,
} = require('../../lib/validation');
const { checkAndConvertToJson } = require('../../lib/validation');
const { trackCommandUsage } = require('../../lib/usageTracking');
const {
batchCreateObjects,
Expand All @@ -24,8 +21,6 @@ exports.handler = async options => {
const { path, name: providedName, derivedAccountId } = options;
let definitionPath = path;

await loadAndValidateOptions(options);

trackCommandUsage('custom-object-batch-create', null, derivedAccountId);

if (!definitionPath) {
Expand Down
7 changes: 1 addition & 6 deletions commands/customObject/schema/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
const { logger } = require('@hubspot/local-dev-lib/logger');
const { logError } = require('../../../lib/errorHandlers/index');
const { getAbsoluteFilePath } = require('@hubspot/local-dev-lib/path');
const {
checkAndConvertToJson,
loadAndValidateOptions,
} = require('../../../lib/validation');
const { checkAndConvertToJson } = require('../../../lib/validation');
const { trackCommandUsage } = require('../../../lib/usageTracking');
const { addTestingOptions } = require('../../../lib/commonOpts');
const {
Expand Down Expand Up @@ -34,8 +31,6 @@ exports.describe = i18n(`${i18nKey}.describe`);
exports.handler = async options => {
const { path, derivedAccountId } = options;

await loadAndValidateOptions(options);

trackCommandUsage('custom-object-schema-create', null, derivedAccountId);

const filePath = getAbsoluteFilePath(path);
Expand Down
3 changes: 0 additions & 3 deletions commands/customObject/schema/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { confirmPrompt, listPrompt } from '../../../lib/prompts/promptUtils';
import { fetchObjectSchemas } from '@hubspot/local-dev-lib/api/customObjects';

const { logger } = require('@hubspot/local-dev-lib/logger');
const { loadAndValidateOptions } = require('../../../lib/validation');
const { trackCommandUsage } = require('../../../lib/usageTracking');
const {
deleteObjectSchema,
Expand All @@ -20,8 +19,6 @@ exports.describe = i18n(`${i18nKey}.describe`);
exports.handler = async options => {
const { name: providedName, force, derivedAccountId } = options;

await loadAndValidateOptions(options);

trackCommandUsage('custom-object-schema-delete', null, derivedAccountId);

let name;
Expand Down
3 changes: 0 additions & 3 deletions commands/customObject/schema/fetch-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { inputPrompt } from '../../../lib/prompts/promptUtils';

const { logger } = require('@hubspot/local-dev-lib/logger');
const { loadAndValidateOptions } = require('../../../lib/validation');
const { trackCommandUsage } = require('../../../lib/usageTracking');
const {
downloadSchemas,
Expand All @@ -18,8 +17,6 @@ exports.command = 'fetch-all [dest]';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
await loadAndValidateOptions(options);

const { derivedAccountId, dest: providedDest } = options;

trackCommandUsage('custom-object-schema-fetch-all', null, derivedAccountId);
Expand Down
3 changes: 0 additions & 3 deletions commands/customObject/schema/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
const { fetchSchema } = require('@hubspot/local-dev-lib/api/fileTransport');
const { getCwd } = require('@hubspot/local-dev-lib/path');

const { loadAndValidateOptions } = require('../../../lib/validation');
const { trackCommandUsage } = require('../../../lib/usageTracking');
const { i18n } = require('../../../lib/lang');
const { logError } = require('../../../lib/errorHandlers');
Expand All @@ -26,8 +25,6 @@ exports.describe = i18n(`${i18nKey}.describe`);
exports.handler = async options => {
const { name: providedName, dest: providedDest, derivedAccountId } = options;

await loadAndValidateOptions(options);

trackCommandUsage('custom-object-schema-fetch', null, derivedAccountId);
let name;

Expand Down
3 changes: 0 additions & 3 deletions commands/customObject/schema/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const { logger } = require('@hubspot/local-dev-lib/logger');
const { logError } = require('../../../lib/errorHandlers/index');

const { loadAndValidateOptions } = require('../../../lib/validation');
const { trackCommandUsage } = require('../../../lib/usageTracking');
const { listSchemas } = require('../../../lib/schema');
const { i18n } = require('../../../lib/lang');
Expand All @@ -13,8 +12,6 @@ exports.command = 'list';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
await loadAndValidateOptions(options);

const { derivedAccountId } = options;

trackCommandUsage('custom-object-schema-list', null, derivedAccountId);
Expand Down
7 changes: 1 addition & 6 deletions commands/customObject/schema/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ const { getAbsoluteFilePath } = require('@hubspot/local-dev-lib/path');
const {
ENVIRONMENTS,
} = require('@hubspot/local-dev-lib/constants/environments');
const {
checkAndConvertToJson,
loadAndValidateOptions,
} = require('../../../lib/validation');
const { checkAndConvertToJson } = require('../../../lib/validation');
const { trackCommandUsage } = require('../../../lib/usageTracking');
const { addTestingOptions } = require('../../../lib/commonOpts');
const { CONFIG_FLAGS } = require('../../../lib/constants');
Expand All @@ -37,8 +34,6 @@ exports.describe = i18n(`${i18nKey}.describe`);
exports.handler = async options => {
const { path, name: providedName, derivedAccountId } = options;

await loadAndValidateOptions(options);

trackCommandUsage('custom-object-schema-update', null, derivedAccountId);

const filePath = getAbsoluteFilePath(path);
Expand Down
7 changes: 1 addition & 6 deletions commands/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ const {
addGlobalOptions,
} = require('../lib/commonOpts');
const { resolveLocalPath } = require('../lib/filesystem');
const {
validateCmsPublishMode,
loadAndValidateOptions,
} = require('../lib/validation');
const { validateCmsPublishMode } = require('../lib/validation');
const { trackCommandUsage } = require('../lib/usageTracking');
const { i18n } = require('../lib/lang');

Expand All @@ -28,8 +25,6 @@ exports.describe = i18n(`${i18nKey}.describe`);
exports.handler = async options => {
const { src, dest } = options;

await loadAndValidateOptions(options);

if (!validateCmsPublishMode(options)) {
process.exit(EXIT_CODES.ERROR);
}
Expand Down
Loading

0 comments on commit 574f829

Please sign in to comment.