Skip to content

Commit

Permalink
chore: Port links.ts util to TS (#1250)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenrodgers authored Nov 19, 2024
1 parent 5e7e9bb commit ef9d466
Showing 1 changed file with 100 additions and 105 deletions.
205 changes: 100 additions & 105 deletions lib/links.ts
Original file line number Diff line number Diff line change
@@ -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}]` : ''}`,
'=>',
Expand All @@ -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)
);

Expand All @@ -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`);
}

0 comments on commit ef9d466

Please sign in to comment.