From b74ae8f41fac6f5e60b1aa2051114aff68233d82 Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Tue, 17 Dec 2024 16:47:05 -0500 Subject: [PATCH 1/3] port validation and configOptions to TS --- lang/en.lyaml | 2 +- lib/configOptions.ts | 102 ++++++++++++++++++++++++------------------- lib/validation.ts | 89 +++++++++++++++++-------------------- package.json | 2 +- yarn.lock | 8 ++-- 5 files changed, 102 insertions(+), 101 deletions(-) diff --git a/lang/en.lyaml b/lang/en.lyaml index d7078bf9c..a9ea979c2 100644 --- a/lang/en.lyaml +++ b/lang/en.lyaml @@ -113,7 +113,7 @@ en: defaultMode: describe: "Set the default CMS publish mode" promptMessage: "Select CMS publish mode to be used as the default" - error: "The CMS publish mode \"{{ mode }}\" is invalid. Valid values are {{ validModes }}." + error: "The provided CMS publish mode is invalid. Valid values are {{ validModes }}." success: "Default mode updated to: {{ mode }}" allowUsageTracking: describe: "Enable or disable usage tracking" diff --git a/lib/configOptions.ts b/lib/configOptions.ts index b58f93d50..180601620 100644 --- a/lib/configOptions.ts +++ b/lib/configOptions.ts @@ -1,23 +1,22 @@ -// @ts-nocheck -const { logger } = require('@hubspot/local-dev-lib/logger'); -const { +import { logger } from '@hubspot/local-dev-lib/logger'; +import { updateAllowUsageTracking, updateDefaultCmsPublishMode, updateHttpTimeout, -} = require('@hubspot/local-dev-lib/config'); -const { CMS_PUBLISH_MODE } = require('@hubspot/local-dev-lib/constants/files'); -const { commaSeparatedValues } = require('@hubspot/local-dev-lib/text'); -const { trackCommandUsage } = require('./usageTracking'); -const { promptUser } = require('./prompts/promptUtils'); -const { i18n } = require('../lib/lang'); +} from '@hubspot/local-dev-lib/config'; +import { CmsPublishMode } from '@hubspot/local-dev-lib/types/Files'; +import { CMS_PUBLISH_MODE } from '@hubspot/local-dev-lib/constants/files'; +import { commaSeparatedValues } from '@hubspot/local-dev-lib/text'; +import { trackCommandUsage } from './usageTracking'; +import { promptUser } from './prompts/promptUtils'; +import { i18n } from '../lib/lang'; const i18nKey = 'commands.config.subcommands.set.options'; -const enableOrDisableUsageTracking = async () => { - const { isEnabled } = await promptUser([ +async function enableOrDisableUsageTracking(): Promise { + const { isEnabled } = await promptUser<{ isEnabled: boolean }>([ { type: 'list', - look: false, name: 'isEnabled', pageSize: 20, message: i18n(`${i18nKey}.allowUsageTracking.promptMessage`), @@ -36,12 +35,18 @@ const enableOrDisableUsageTracking = async () => { ]); return isEnabled; -}; +} -const setAllowUsageTracking = async ({ accountId, allowUsageTracking }) => { - trackCommandUsage('config-set-allow-usage-tracking', null, accountId); +export async function setAllowUsageTracking({ + accountId, + allowUsageTracking, +}: { + accountId: number; + allowUsageTracking?: boolean; +}): Promise { + trackCommandUsage('config-set-allow-usage-tracking', undefined, accountId); - let isEnabled; + let isEnabled: boolean; if (typeof allowUsageTracking === 'boolean') { isEnabled = allowUsageTracking; @@ -51,18 +56,21 @@ const setAllowUsageTracking = async ({ accountId, allowUsageTracking }) => { updateAllowUsageTracking(isEnabled); - return logger.log( - i18n(`${i18nKey}.allowUsageTracking.success`, { isEnabled }) + logger.success( + i18n(`${i18nKey}.allowUsageTracking.success`, { + isEnabled: isEnabled.toString(), + }) ); -}; +} const ALL_CMS_PUBLISH_MODES = Object.values(CMS_PUBLISH_MODE); -const selectCmsPublishMode = async () => { - const { cmsPublishMode } = await promptUser([ +async function selectCmsPublishMode(): Promise { + const { cmsPublishMode } = await promptUser<{ + cmsPublishMode: CmsPublishMode; + }>([ { type: 'list', - look: false, name: 'cmsPublishMode', pageSize: 20, message: i18n(`${i18nKey}.defaultMode.promptMessage`), @@ -72,15 +80,18 @@ const selectCmsPublishMode = async () => { ]); return cmsPublishMode; -}; +} -const setDefaultCmsPublishMode = async ({ +export async function setDefaultCmsPublishMode({ accountId, defaultCmsPublishMode, -}) => { - trackCommandUsage('config-set-default-mode', null, accountId); +}: { + accountId: number; + defaultCmsPublishMode?: CmsPublishMode; +}): Promise { + trackCommandUsage('config-set-default-mode', undefined, accountId); - let newDefault; + let newDefault: CmsPublishMode; if (!defaultCmsPublishMode) { newDefault = await selectCmsPublishMode(); @@ -91,25 +102,24 @@ const setDefaultCmsPublishMode = async ({ newDefault = defaultCmsPublishMode; } else { logger.error( - i18n(`${i18nKey}.defaultMode.errors`, { - mode: newDefault, + i18n(`${i18nKey}.defaultMode.error`, { validModes: commaSeparatedValues(ALL_CMS_PUBLISH_MODES), }) ); - newDefault = await selectCMsPublishMode(); + newDefault = await selectCmsPublishMode(); } updateDefaultCmsPublishMode(newDefault); - return logger.success( + logger.success( i18n(`${i18nKey}.defaultMode.success`, { mode: newDefault, }) ); -}; +} -const enterTimeout = async () => { - const { timeout } = await promptUser([ +async function enterTimeout(): Promise { + const { timeout } = await promptUser<{ timeout: string }>([ { name: 'timeout', message: i18n(`${i18nKey}.httpTimeout.promptMessage`), @@ -119,12 +129,18 @@ const enterTimeout = async () => { ]); return timeout; -}; +} -const setHttpTimeout = async ({ accountId, httpTimeout }) => { - trackCommandUsage('config-set-http-timeout', null, accountId); +export async function setHttpTimeout({ + accountId, + httpTimeout, +}: { + accountId: number; + httpTimeout?: string; +}): Promise { + trackCommandUsage('config-set-http-timeout', undefined, accountId); - let newHttpTimeout; + let newHttpTimeout: string; if (!httpTimeout) { newHttpTimeout = await enterTimeout(); @@ -134,13 +150,7 @@ const setHttpTimeout = async ({ accountId, httpTimeout }) => { updateHttpTimeout(newHttpTimeout); - return logger.success( + logger.success( i18n(`${i18nKey}.httpTimeout.success`, { timeout: newHttpTimeout }) ); -}; - -module.exports = { - setAllowUsageTracking, - setDefaultCmsPublishMode, - setHttpTimeout, -}; +} diff --git a/lib/validation.ts b/lib/validation.ts index e770e2348..300aa85a8 100644 --- a/lib/validation.ts +++ b/lib/validation.ts @@ -1,34 +1,36 @@ -// @ts-nocheck -const fs = require('fs'); -const path = require('path'); -const { logger } = require('@hubspot/local-dev-lib/logger'); -const { CMS_PUBLISH_MODE } = require('@hubspot/local-dev-lib/constants/files'); -const { +import * as fs from 'fs'; +import * as path from 'path'; +import { Arguments } from 'yargs'; +import { logger } from '@hubspot/local-dev-lib/logger'; +import { CMS_PUBLISH_MODE } from '@hubspot/local-dev-lib/constants/files'; +import { CmsPublishMode } from '@hubspot/local-dev-lib/types/Files'; +import { API_KEY_AUTH_METHOD, OAUTH_AUTH_METHOD, PERSONAL_ACCESS_KEY_AUTH_METHOD, -} = require('@hubspot/local-dev-lib/constants/auth'); -const { commaSeparatedValues } = require('@hubspot/local-dev-lib/text'); -const { +} from '@hubspot/local-dev-lib/constants/auth'; +import { commaSeparatedValues } from '@hubspot/local-dev-lib/text'; +import { getConfigPath, validateConfig, getAccountConfig, loadConfigFromEnvironment, -} = require('@hubspot/local-dev-lib/config'); -const { getOauthManager } = require('@hubspot/local-dev-lib/oauth'); -const { - accessTokenForPersonalAccessKey, -} = require('@hubspot/local-dev-lib/personalAccessKey'); -const { +} from '@hubspot/local-dev-lib/config'; +import { getOauthManager } from '@hubspot/local-dev-lib/oauth'; +import { accessTokenForPersonalAccessKey } from '@hubspot/local-dev-lib/personalAccessKey'; +import { getAbsoluteFilePath, getCwd, getExt, -} = require('@hubspot/local-dev-lib/path'); -const { getAccountId, getCmsPublishMode } = require('./commonOpts'); -const { EXIT_CODES } = require('./enums/exitCodes'); -const { logError } = require('./errorHandlers/index'); - -async function loadAndValidateOptions(options, shouldValidateAccount = true) { +} from '@hubspot/local-dev-lib/path'; +import { getAccountId, getCmsPublishMode } from './commonOpts'; +import { EXIT_CODES } from './enums/exitCodes'; +import { logError } from './errorHandlers/index'; + +export async function loadAndValidateOptions( + options: Arguments<{ account?: string; accountId?: string }>, + shouldValidateAccount = true +): Promise { let validAccount = true; if (shouldValidateAccount) { validAccount = await validateAccount(options); @@ -39,14 +41,9 @@ async function loadAndValidateOptions(options, shouldValidateAccount = true) { } } -/** - * Validate that an account was passed to the command and that the account's configuration is valid - * - * - * @param {object} command options - * @returns {boolean} - */ -async function validateAccount(options) { +export async function validateAccount( + options: Arguments<{ account?: string; accountId?: string }> +): Promise { const accountId = getAccountId(options); const { accountId: accountIdOption, account: accountOption } = options; @@ -113,7 +110,11 @@ async function validateAccount(options) { const oauth = getOauthManager(accountId, accountConfig); try { - const accessToken = await oauth.accessToken(); + let accessToken: string | undefined; + + if (oauth) { + accessToken = await oauth.accessToken(); + } if (!accessToken) { logger.error( `The OAuth2 access token could not be found for accountId ${accountId}` @@ -121,7 +122,7 @@ async function validateAccount(options) { return false; } } catch (e) { - logger.error(e.message); + logError(e); return false; } } else if (authType === 'personalaccesskey') { @@ -154,11 +155,9 @@ async function validateAccount(options) { return true; } -/** - * @param {object} command options - * @returns {boolean} - */ -function validateCmsPublishMode(options) { +export function validateCmsPublishMode( + options: Arguments<{ cmsPublishMode?: CmsPublishMode }> +): boolean { const cmsPublishMode = getCmsPublishMode(options); if (CMS_PUBLISH_MODE[cmsPublishMode]) { return true; @@ -181,8 +180,8 @@ function validateCmsPublishMode(options) { return false; } -const fileExists = _path => { - let isFile; +export function fileExists(_path: string): boolean { + let isFile: boolean; try { const absoluteSrcPath = path.resolve(getCwd(), _path); if (!absoluteSrcPath) return false; @@ -200,9 +199,9 @@ const fileExists = _path => { } return true; -}; +} -const checkAndConvertToJson = _path => { +export function checkAndConvertToJson(_path: string): object | boolean | null { const filePath = getAbsoluteFilePath(_path); if (!fileExists(filePath)) return false; @@ -221,12 +220,4 @@ const checkAndConvertToJson = _path => { } return result; -}; - -module.exports = { - validateCmsPublishMode, - validateAccount, - checkAndConvertToJson, - fileExists, - loadAndValidateOptions, -}; +} diff --git a/package.json b/package.json index d9c4db28b..4eac4663f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "license": "Apache-2.0", "repository": "https://github.com/HubSpot/hubspot-cli", "dependencies": { - "@hubspot/local-dev-lib": "3.0.1", + "@hubspot/local-dev-lib": "3.0.2", "@hubspot/serverless-dev-runtime": "7.0.0", "@hubspot/theme-preview-dev-server": "0.0.10", "@hubspot/ui-extensions-dev-server": "0.8.33", diff --git a/yarn.lock b/yarn.lock index 52879ffe0..193394f91 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1463,10 +1463,10 @@ semver "^6.3.0" unixify "^1.0.0" -"@hubspot/local-dev-lib@3.0.1": - version "3.0.1" - resolved "https://registry.yarnpkg.com/@hubspot/local-dev-lib/-/local-dev-lib-3.0.1.tgz#1dd22f439d6e262353f14915a354115bbc1f5f76" - integrity sha512-h1jOmZJNdHZFbrOA5Gn815YCsix8eY81A4dkrUuDZI4MzVaJH3o4RoRalMl+Hr2e35nDrbrbcIR1RMQADPSmwg== +"@hubspot/local-dev-lib@3.0.2": + version "3.0.2" + resolved "https://registry.yarnpkg.com/@hubspot/local-dev-lib/-/local-dev-lib-3.0.2.tgz#8412c26c51d26cdb015312f5c5ac2daeced5e30f" + integrity sha512-brpFWcQIdP71YzwQ2ZMKqChzIEo4cCLJYPCTTVmMUwmuXtL35T/7vL06OukNEND5+B8VlmZeJ4PMrvsAT4YuWA== dependencies: address "^2.0.1" axios "^1.3.5" From 3fb6f47cfc4486399612e42cd3c19ca26a08f9a7 Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Wed, 18 Dec 2024 14:57:38 -0500 Subject: [PATCH 2/3] bump LDL dep --- package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 4eac4663f..3b0c3dfc9 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "license": "Apache-2.0", "repository": "https://github.com/HubSpot/hubspot-cli", "dependencies": { - "@hubspot/local-dev-lib": "3.0.2", + "@hubspot/local-dev-lib": "3.1.0", "@hubspot/serverless-dev-runtime": "7.0.0", "@hubspot/theme-preview-dev-server": "0.0.10", "@hubspot/ui-extensions-dev-server": "0.8.33", diff --git a/yarn.lock b/yarn.lock index 193394f91..f582764b9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1463,10 +1463,10 @@ semver "^6.3.0" unixify "^1.0.0" -"@hubspot/local-dev-lib@3.0.2": - version "3.0.2" - resolved "https://registry.yarnpkg.com/@hubspot/local-dev-lib/-/local-dev-lib-3.0.2.tgz#8412c26c51d26cdb015312f5c5ac2daeced5e30f" - integrity sha512-brpFWcQIdP71YzwQ2ZMKqChzIEo4cCLJYPCTTVmMUwmuXtL35T/7vL06OukNEND5+B8VlmZeJ4PMrvsAT4YuWA== +"@hubspot/local-dev-lib@3.1.0": + version "3.1.0" + resolved "https://registry.yarnpkg.com/@hubspot/local-dev-lib/-/local-dev-lib-3.1.0.tgz#76b5d524aa694aad2bfc6e199ee1dac314f15774" + integrity sha512-iop3PgZ0ZWejCH6PmSeYnnGwV8vGIuA4F+w7SxukdX3QfhivlczClATWPaZanv1CYJHdflqoItq1kdF8ASJJmA== dependencies: address "^2.0.1" axios "^1.3.5" From d625070d6db3e8245d0ef57fa0ffcc74d6a52336 Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Wed, 18 Dec 2024 15:43:11 -0500 Subject: [PATCH 3/3] return null instead of false --- lib/validation.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/validation.ts b/lib/validation.ts index 300aa85a8..8d56e4c8b 100644 --- a/lib/validation.ts +++ b/lib/validation.ts @@ -201,9 +201,9 @@ export function fileExists(_path: string): boolean { return true; } -export function checkAndConvertToJson(_path: string): object | boolean | null { +export function checkAndConvertToJson(_path: string): object | null { const filePath = getAbsoluteFilePath(_path); - if (!fileExists(filePath)) return false; + if (!fileExists(filePath)) return null; if (getExt(_path) !== 'json') { logger.error(`The file "${_path}" must be a valid JSON file`);