Skip to content

Commit

Permalink
chore: port projectStructure to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenrodgers committed Dec 12, 2024
1 parent a8858bd commit c2c9ca7
Showing 1 changed file with 109 additions and 38 deletions.
147 changes: 109 additions & 38 deletions lib/projectStructure.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,103 @@
// @ts-nocheck
const fs = require('fs');
const path = require('path');
const { walk } = require('@hubspot/local-dev-lib/fs');
const { logger } = require('@hubspot/local-dev-lib/logger');
const { logError } = require('./errorHandlers/index');

const COMPONENT_TYPES = Object.freeze({
import * as fs from 'fs';
import * as path from 'path';
import { walk } from '@hubspot/local-dev-lib/fs';
import { logger } from '@hubspot/local-dev-lib/logger';
import { logError } from './errorHandlers/index';

type ComponentTypes = 'private-app' | 'public-app' | 'hubl-theme';
type ValueOf<T> = T[keyof T];

export type Component = {
type: ComponentTypes;
config: object;
runnable: boolean;
path: string;
};

type PrivateAppComponentConfigType = {
name: string;
description: string;
uid: string;
scopes: Array<string>;
public: boolean;
extensions?: {
crm: {
cards: Array<{ file: string }>;
};
};
};

type PublicAppComponentConfigType = {
name: string;
uid: string;
description: string;
allowedUrls: Array<string>;
auth: {
redirectUrls: Array<string>;
requiredScopes: Array<string>;
optionalScopes: Array<string>;
conditionallyRequiredScopes: Array<string>;
};
support: {
supportEmail: string;
documentationUrl: string;
supportUrl: string;
supportPhone: string;
};
extensions?: {
crm: {
cards: Array<{ file: string }>;
};
};
webhooks?: {
file: string;
};
};

type AppCardComponentConfigType = {
type: 'crm-card';
data: {
title: string;
uid: string;
location: string;
module: {
file: string;
};
objectTypes: Array<{ name: string }>;
};
};

export const COMPONENT_TYPES = {
privateApp: 'private-app',
publicApp: 'public-app',
hublTheme: 'hubl-theme',
});
} as const;

const CONFIG_FILES = {
export const CONFIG_FILES: {
[k in ValueOf<typeof COMPONENT_TYPES>]: string;
} = {
[COMPONENT_TYPES.privateApp]: 'app.json',
[COMPONENT_TYPES.publicApp]: 'public-app.json',
[COMPONENT_TYPES.hublTheme]: 'theme.json',
};

function getTypeFromConfigFile(configFile) {
for (const key in CONFIG_FILES) {
function getTypeFromConfigFile(
configFile: ValueOf<typeof CONFIG_FILES>
): ComponentTypes | null {
let key: ComponentTypes;
for (key in CONFIG_FILES) {
if (CONFIG_FILES[key] === configFile) {
return key;
}
}
return null;
}

function loadConfigFile(configPath) {
function loadConfigFile(configPath: string) {
if (configPath) {
try {
const source = fs.readFileSync(configPath);
const parsedConfig = JSON.parse(source);
const parsedConfig = JSON.parse(source.toString());
return parsedConfig;
} catch (e) {
logger.debug(e);
Expand All @@ -39,16 +106,19 @@ function loadConfigFile(configPath) {
return null;
}

function getAppCardConfigs(appConfig, appPath) {
const cardConfigs = [];
export function getAppCardConfigs(
appConfig: PublicAppComponentConfigType | PrivateAppComponentConfigType,
appPath: string
) {
const cardConfigs: Array<AppCardComponentConfigType> = [];
let cards;

if (appConfig && appConfig.extensions && appConfig.extensions.crm) {
cards = appConfig.extensions.crm.cards;
}

if (cards) {
cards.forEach(({ file }) => {
cards.forEach(({ file }: { file?: string }) => {
if (typeof file === 'string') {
const cardConfigPath = path.join(appPath, file);
const cardConfig = loadConfigFile(cardConfigPath);
Expand All @@ -63,7 +133,10 @@ function getAppCardConfigs(appConfig, appPath) {
return cardConfigs;
}

function getIsLegacyApp(appConfig, appPath) {
function getIsLegacyApp(
appConfig: PublicAppComponentConfigType | PrivateAppComponentConfigType,
appPath: string
) {
const cardConfigs = getAppCardConfigs(appConfig, appPath);

if (!cardConfigs.length) {
Expand All @@ -88,9 +161,11 @@ function getIsLegacyApp(appConfig, appPath) {
return !hasAnyReactExtensions;
}

async function findProjectComponents(projectSourceDir) {
const components = [];
let projectFiles = [];
export async function findProjectComponents(
projectSourceDir: string
): Promise<Array<Component>> {
const components: Array<Component> = [];
let projectFiles: Array<string> = [];

try {
projectFiles = await walk(projectSourceDir);
Expand All @@ -108,30 +183,26 @@ async function findProjectComponents(projectSourceDir) {
if (parsedAppConfig) {
const isLegacy = getIsLegacyApp(parsedAppConfig, dir);
const isHublTheme = base === CONFIG_FILES[COMPONENT_TYPES.hublTheme];

components.push({
type: getTypeFromConfigFile(base),
config: parsedAppConfig,
runnable: !isLegacy && !isHublTheme,
path: dir,
});
const type = getTypeFromConfigFile(base);

if (type) {
components.push({
type,
config: parsedAppConfig,
runnable: !isLegacy && !isHublTheme,
path: dir,
});
}
}
}
});

return components;
}

function getProjectComponentTypes(components) {
const projectContents = {};
export function getProjectComponentTypes(components: Array<Component>) {
const projectContents: { [key in ComponentTypes]?: boolean } = {};

components.forEach(({ type }) => (projectContents[type] = true));
return projectContents;
}

module.exports = {
CONFIG_FILES,
COMPONENT_TYPES,
findProjectComponents,
getAppCardConfigs,
getProjectComponentTypes,
};

0 comments on commit c2c9ca7

Please sign in to comment.