From ef9d4660a74aa3072456b282d0bdcae9d725692f Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Tue, 19 Nov 2024 17:00:03 -0500 Subject: [PATCH] chore: Port links.ts util to TS (#1250) --- lib/links.ts | 205 +++++++++++++++++++++++++-------------------------- 1 file changed, 100 insertions(+), 105 deletions(-) diff --git a/lib/links.ts b/lib/links.ts index 71b4a91f9..f4ca3b6b3 100644 --- a/lib/links.ts +++ b/lib/links.ts @@ -1,15 +1,96 @@ -// @ts-nocheck -const { getEnv } = require('@hubspot/local-dev-lib/config'); -const { - ENVIRONMENTS, -} = require('@hubspot/local-dev-lib/constants/environments'); -const { getHubSpotWebsiteOrigin } = require('@hubspot/local-dev-lib/urls'); -const { logger } = require('@hubspot/local-dev-lib/logger'); -const { getTableContents, getTableHeader } = require('./ui/table'); +import open from 'open'; +import { getEnv } from '@hubspot/local-dev-lib/config'; +import { ENVIRONMENTS } from '@hubspot/local-dev-lib/constants/environments'; +import { getHubSpotWebsiteOrigin } from '@hubspot/local-dev-lib/urls'; +import { logger } from '@hubspot/local-dev-lib/logger'; +import { getTableContents, getTableHeader } from './ui/table'; -const open = require('open'); +type SiteLink = { + shortcut: string; + alias?: string; + getUrl: (a: number) => string; + url?: string; +}; + +const SITE_LINKS: { [key: string]: SiteLink } = { + APPS_MARKETPLACE: { + shortcut: 'apps-marketplace', + alias: 'apm', + getUrl: (a: number): string => `ecosystem/${a}/marketplace/apps`, + }, + ASSET_MARKETPLACE: { + shortcut: 'asset-marketplace', + alias: 'asm', + getUrl: (a: number): string => `ecosystem/${a}/marketplace/products`, + }, + CONTENT_STAGING: { + shortcut: 'content-staging', + alias: 'cs', + getUrl: (a: number): string => `content/${a}/staging`, + }, + DESIGN_MANAGER: { + shortcut: 'design-manager', + alias: 'dm', + getUrl: (a: number): string => `design-manager/${a}`, + }, + DOCS: { + shortcut: 'docs', + getUrl: (): string => 'https://developers.hubspot.com', + }, + FILE_MANAGER: { + shortcut: 'file-manager', + alias: 'fm', + getUrl: (a: number): string => `files/${a}`, + }, + FORUMS: { + shortcut: 'forums', + getUrl: (): string => 'https://community.hubspot.com', + }, + HUBDB: { + shortcut: 'hubdb', + alias: 'hdb', + getUrl: (a: number): string => `hubdb/${a}`, + }, + SETTINGS: { + shortcut: 'settings', + alias: 's', + getUrl: (a: number): string => `settings/${a}`, + }, + SETTINGS_NAVIGATION: { + shortcut: 'settings/navigation', + alias: 'sn', + getUrl: (a: number): string => `menus/${a}/edit/`, + }, + SETTINGS_WEBSITE: { + shortcut: 'settings/website', + alias: 'sw', + getUrl: (a: number): string => + `settings/${a}/website/pages/all-domains/page-templates`, + }, + SETTINGS_getUrl_REDIRECTS: { + shortcut: 'settings/url-redirects', + alias: 'sur', + getUrl: (a: number): string => `domains/${a}/url-redirects`, + }, + PURCHASED_ASSETS: { + shortcut: 'purchased-assets', + alias: 'pa', + getUrl: (a: number): string => `marketplace/${a}/manage-purchases`, + }, + WEBSITE_PAGES: { + shortcut: 'website-pages', + alias: 'wp', + getUrl: (a: number): string => `website/${a}/pages/site`, + }, +}; -const logSiteLinks = accountId => { +export function getSiteLinksAsArray(accountId: number): SiteLink[] { + return Object.values(SITE_LINKS) + .sort((a, b) => (a.shortcut < b.shortcut ? -1 : 1)) + .map(l => ({ ...l, url: l.getUrl(accountId) })); +} + +export function logSiteLinks(accountId: number): void { const linksAsArray = getSiteLinksAsArray(accountId).map(l => [ `${l.shortcut}${l.alias ? ` [alias: ${l.alias}]` : ''}`, '=>', @@ -19,93 +100,10 @@ const logSiteLinks = accountId => { linksAsArray.unshift(getTableHeader(['Shortcut', '', 'Url'])); logger.log(getTableContents(linksAsArray)); -}; +} -const getSiteLinksAsArray = accountId => - Object.values(getSiteLinks(accountId)).sort((a, b) => - a.shortcut < b.shortcut ? -1 : 1 - ); - -const getSiteLinks = accountId => { - const baseUrl = getHubSpotWebsiteOrigin( - getEnv() === 'qa' ? ENVIRONMENTS.QA : ENVIRONMENTS.PROD - ); - - return { - APPS_MARKETPLACE: { - shortcut: 'apps-marketplace', - alias: 'apm', - url: `${baseUrl}/ecosystem/${accountId}/marketplace/apps`, - }, - ASSET_MARKETPLACE: { - shortcut: 'asset-marketplace', - alias: 'asm', - url: `${baseUrl}/ecosystem/${accountId}/marketplace/products`, - }, - CONTENT_STAGING: { - shortcut: 'content-staging', - alias: 'cs', - url: `${baseUrl}/content/${accountId}/staging`, - }, - DESIGN_MANAGER: { - shortcut: 'design-manager', - alias: 'dm', - url: `${baseUrl}/design-manager/${accountId}`, - }, - DOCS: { - shortcut: 'docs', - url: 'https://developers.hubspot.com', - }, - FILE_MANAGER: { - shortcut: 'file-manager', - alias: 'fm', - url: `${baseUrl}/files/${accountId}`, - }, - FORUMS: { - shortcut: 'forums', - url: 'https://community.hubspot.com', - }, - HUBDB: { - shortcut: 'hubdb', - alias: 'hdb', - url: `${baseUrl}/hubdb/${accountId}`, - }, - SETTINGS: { - shortcut: 'settings', - alias: 's', - url: `${baseUrl}/settings/${accountId}`, - }, - SETTINGS_NAVIGATION: { - shortcut: 'settings/navigation', - alias: 'sn', - url: `${baseUrl}/menus/${accountId}/edit/`, - }, - SETTINGS_WEBSITE: { - shortcut: 'settings/website', - alias: 'sw', - url: `${baseUrl}/settings/${accountId}/website/pages/all-domains/page-templates`, - }, - SETTINGS_URL_REDIRECTS: { - shortcut: 'settings/url-redirects', - alias: 'sur', - url: `${baseUrl}/domains/${accountId}/url-redirects`, - }, - PURCHASED_ASSETS: { - shortcut: 'purchased-assets', - alias: 'pa', - url: `${baseUrl}/marketplace/${accountId}/manage-purchases`, - }, - - WEBSITE_PAGES: { - shortcut: 'website-pages', - alias: 'wp', - url: `${baseUrl}/website/${accountId}/pages/site`, - }, - }; -}; - -const openLink = (accountId, shortcut) => { - const match = Object.values(getSiteLinks(accountId)).find( +export function openLink(accountId: number, shortcut: string): void { + const match = Object.values(SITE_LINKS).find( l => l.shortcut === shortcut || (l.alias && l.alias === shortcut) ); @@ -116,13 +114,10 @@ const openLink = (accountId, shortcut) => { return; } - open(match.url, { url: true }); - logger.success(`We opened ${match.url} in your browser`); -}; + const baseUrl = getHubSpotWebsiteOrigin( + getEnv() === 'qa' ? ENVIRONMENTS.QA : ENVIRONMENTS.PROD + ); -module.exports = { - getSiteLinks, - getSiteLinksAsArray, - logSiteLinks, - openLink, -}; + open(`${baseUrl}/${match.getUrl(accountId)}`, { url: true }); + logger.success(`We opened ${match.getUrl(accountId)} in your browser`); +}