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

Port DevServerManager to TS #1335

Merged
merged 2 commits into from
Jan 13, 2025
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
7 changes: 3 additions & 4 deletions commands/project/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ const {
isAppDeveloperAccount,
} = require('../../lib/accountTypes');
const { getValidEnv } = require('@hubspot/local-dev-lib/environment');

const { ComponentTypes } = require('../../types/Projects');
const {
findProjectComponents,
getProjectComponentTypes,
COMPONENT_TYPES,
} = require('../../lib/projects/structure');
const {
confirmDefaultAccountIsTarget,
Expand Down Expand Up @@ -82,8 +81,8 @@ exports.handler = async options => {
const components = await findProjectComponents(projectDir);
const runnableComponents = components.filter(component => component.runnable);
const componentTypes = getProjectComponentTypes(runnableComponents);
const hasPrivateApps = !!componentTypes[COMPONENT_TYPES.privateApp];
const hasPublicApps = !!componentTypes[COMPONENT_TYPES.publicApp];
const hasPrivateApps = !!componentTypes[ComponentTypes.PrivateApp];
const hasPublicApps = !!componentTypes[ComponentTypes.PublicApp];

if (runnableComponents.length === 0) {
logger.error(
Expand Down
8 changes: 3 additions & 5 deletions commands/theme/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ const { ApiErrorContext, logError } = require('../../lib/errorHandlers/index');
const { handleExit, handleKeypress } = require('../../lib/process');
const { getThemeJSONPath } = require('@hubspot/local-dev-lib/cms/themes');
const { getProjectConfig } = require('../../lib/projects');
const {
findProjectComponents,
COMPONENT_TYPES,
} = require('../../lib/projects/structure');
const { findProjectComponents } = require('../../lib/projects/structure');
const { ComponentTypes } = require('../../types/Projects');
const { preview } = require('@hubspot/theme-preview-dev-server');
const { hasFeature } = require('../../lib/hasFeature');
const i18nKey = 'commands.theme.subcommands.preview';
Expand Down Expand Up @@ -85,7 +83,7 @@ const determineSrcAndDest = async options => {
if (!themeJsonPath) {
const projectComponents = await findProjectComponents(projectDir);
const themeComponents = projectComponents.filter(
c => c.type === COMPONENT_TYPES.hublTheme
c => c.type === ComponentTypes.HublTheme
);
if (themeComponents.length === 0) {
logger.error(i18n(`${i18nKey}.errors.noThemeComponents`));
Expand Down
116 changes: 88 additions & 28 deletions lib/DevServerManager.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,77 @@
// @ts-nocheck
const { logger } = require('@hubspot/local-dev-lib/logger');
const { COMPONENT_TYPES } = require('./projects/structure');
const { i18n } = require('./lang');
const { promptUser } = require('./prompts/promptUtils');
const { DevModeInterface } = require('@hubspot/ui-extensions-dev-server');
const {
import { logger } from '@hubspot/local-dev-lib/logger';
import { Environment } from '@hubspot/local-dev-lib/types/Config';
import { i18n } from './lang';
import { promptUser } from './prompts/promptUtils';
import { DevModeInterface as UIEDevModeInterface } from '@hubspot/ui-extensions-dev-server';
import {
startPortManagerServer,
stopPortManagerServer,
requestPorts,
} = require('@hubspot/local-dev-lib/portManager');
const {
} from '@hubspot/local-dev-lib/portManager';
import {
getHubSpotApiOrigin,
getHubSpotWebsiteOrigin,
} = require('@hubspot/local-dev-lib/urls');
const { getAccountConfig } = require('@hubspot/local-dev-lib/config');
} from '@hubspot/local-dev-lib/urls';
import { getAccountConfig } from '@hubspot/local-dev-lib/config';
import { ProjectConfig, ComponentTypes, Component } from '../types/Projects';

const i18nKey = 'lib.DevServerManager';

const SERVER_KEYS = {
privateApp: 'privateApp',
publicApp: 'publicApp',
} as const;

type ServerKey = keyof typeof SERVER_KEYS;

type DevServerInterface = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
setup?: Function;
start?: (options: object) => Promise<void>;
brandenrodgers marked this conversation as resolved.
Show resolved Hide resolved
fileChange?: (filePath: string, event: string) => Promise<void>;
cleanup?: () => Promise<void>;
};

type DevServer = {
componentType: ComponentTypes;
serverInterface: DevServerInterface;
};

type ComponentsByType = {
[key in ComponentTypes]?: { [key: string]: Component };
};

class DevServerManager {
private initialized: boolean;
private started: boolean;
private componentsByType: ComponentsByType;
private devServers: { [key in ServerKey]: DevServer };

constructor() {
this.initialized = false;
this.started = false;
this.componentsByType = {};
this.server = null;
this.path = null;
this.devServers = {
[SERVER_KEYS.privateApp]: {
componentType: COMPONENT_TYPES.privateApp,
serverInterface: DevModeInterface,
componentType: ComponentTypes.PrivateApp,
serverInterface: UIEDevModeInterface,
},
[SERVER_KEYS.publicApp]: {
componentType: COMPONENT_TYPES.publicApp,
serverInterface: DevModeInterface,
componentType: ComponentTypes.PublicApp,
serverInterface: UIEDevModeInterface,
},
};
}

async iterateDevServers(callback) {
const serverKeys = Object.keys(this.devServers);
async iterateDevServers(
callback: (
serverInterface: DevServerInterface,
compatibleComponents: {
[key: string]: Component;
}
) => Promise<void>
): Promise<void> {
const serverKeys: ServerKey[] = Object.keys(this.devServers) as ServerKey[];

brandenrodgers marked this conversation as resolved.
Show resolved Hide resolved
for (let i = 0; i < serverKeys.length; i++) {
const serverKey = serverKeys[i];
Expand All @@ -59,21 +88,37 @@ class DevServerManager {
}
}

arrangeComponentsByType(components) {
return components.reduce((acc, component) => {
arrangeComponentsByType(components: Component[]): ComponentsByType {
return components.reduce<ComponentsByType>((acc, component) => {
if (!acc[component.type]) {
acc[component.type] = {};
}

acc[component.type][component.config.name] = component;
if ('name' in component.config && component.config.name) {
acc[component.type]![component.config.name] = component;
}

return acc;
}, {});
}

async setup({ components, onUploadRequired, accountId, setActiveApp }) {
async setup({
components,
onUploadRequired,
accountId,
setActiveApp,
}: {
components: Component[];
onUploadRequired: () => void;
accountId: number;
setActiveApp: (appUid: string | undefined) => Promise<void>;
}): Promise<void> {
this.componentsByType = this.arrangeComponentsByType(components);
const { env } = getAccountConfig(accountId);
let env: Environment;
const accountConfig = getAccountConfig(accountId);
if (accountConfig) {
env = accountConfig.env;
}
await startPortManagerServer();
await this.iterateDevServers(
async (serverInterface, compatibleComponents) => {
Expand All @@ -96,7 +141,13 @@ class DevServerManager {
this.initialized = true;
}

async start({ accountId, projectConfig }) {
async start({
accountId,
projectConfig,
}: {
accountId: number;
projectConfig: ProjectConfig;
}): Promise<void> {
if (this.initialized) {
await this.iterateDevServers(async serverInterface => {
if (serverInterface.start) {
Expand All @@ -114,7 +165,13 @@ class DevServerManager {
this.started = true;
}

fileChange({ filePath, event }) {
async fileChange({
filePath,
event,
}: {
filePath: string;
event: string;
}): Promise<void> {
if (this.started) {
this.iterateDevServers(async serverInterface => {
if (serverInterface.fileChange) {
Expand All @@ -124,7 +181,7 @@ class DevServerManager {
}
}

async cleanup() {
async cleanup(): Promise<void> {
if (this.started) {
await this.iterateDevServers(async serverInterface => {
if (serverInterface.cleanup) {
Expand All @@ -137,4 +194,7 @@ class DevServerManager {
}
}

module.exports = new DevServerManager();
const Manager = new DevServerManager();

export default Manager;
module.exports = Manager;
17 changes: 7 additions & 10 deletions lib/LocalDevManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ const DevServerManager = require('./DevServerManager');
const { EXIT_CODES } = require('./enums/exitCodes');
const { getProjectDetailUrl } = require('./projects/urls');
const { getAccountHomeUrl } = require('./localDev');
const {
CONFIG_FILES,
COMPONENT_TYPES,
getAppCardConfigs,
} = require('./projects/structure');
const { CONFIG_FILES, getAppCardConfigs } = require('./projects/structure');
const { ComponentTypes } = require('../types/Projects');
const {
UI_COLORS,
uiCommandReference,
Expand Down Expand Up @@ -92,7 +89,7 @@ class LocalDevManager {
return component.config.uid === appUid;
});

if (this.activeApp.type === COMPONENT_TYPES.publicApp) {
if (this.activeApp.type === ComponentTypes.PublicApp) {
try {
await this.setActivePublicAppData();
await this.checkActivePublicAppInstalls();
Expand Down Expand Up @@ -212,7 +209,7 @@ class LocalDevManager {
)
);

if (this.activeApp.type === COMPONENT_TYPES.publicApp) {
if (this.activeApp.type === ComponentTypes.PublicApp) {
logger.log(
uiLink(
i18n(`${i18nKey}.viewTestAccountLink`),
Expand Down Expand Up @@ -319,7 +316,7 @@ class LocalDevManager {
let warning = reason;
if (!reason) {
warning =
this.activeApp.type === COMPONENT_TYPES.publicApp &&
this.activeApp.type === ComponentTypes.PublicApp &&
this.publicAppActiveInstalls > 0
? i18n(`${i18nKey}.uploadWarning.defaultPublicAppWarning`, {
installCount: this.publicAppActiveInstalls,
Expand Down Expand Up @@ -383,7 +380,7 @@ class LocalDevManager {
const missingComponents = [];

this.runnableComponents.forEach(({ type, config, path }) => {
if (Object.values(COMPONENT_TYPES).includes(type)) {
if (Object.values(ComponentTypes).includes(type)) {
const cardConfigs = getAppCardConfigs(config, path);

if (!deployedComponentNames.includes(config.name)) {
Expand Down Expand Up @@ -423,7 +420,7 @@ class LocalDevManager {
});

const configPaths = this.runnableComponents
.filter(({ type }) => Object.values(COMPONENT_TYPES).includes(type))
.filter(({ type }) => Object.values(ComponentTypes).includes(type))
.map(component => {
const appConfigPath = path.join(
component.path,
Expand Down
Loading
Loading