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

Convert ProjectLogsManager to TS #1312

Merged
merged 3 commits into from
Dec 20, 2024
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
6 changes: 4 additions & 2 deletions commands/project/__tests__/logs.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @ts-nocheck
const yargs = require('yargs');
const { addUseEnvironmentOptions } = require('../../../lib/commonOpts');
const ProjectLogsManager = require('../../../lib/ProjectLogsManager');
const {
ProjectLogsManager,
} = require('../../../lib/projects/ProjectLogsManager');
const {
projectLogsPrompt,
} = require('../../../lib/prompts/projectsLogsPrompt');
Expand All @@ -16,7 +18,7 @@ jest.mock('@hubspot/local-dev-lib/logger');
jest.mock('../../../lib/commonOpts');
jest.mock('../../../lib/usageTracking');
jest.mock('../../../lib/validation');
jest.mock('../../../lib/ProjectLogsManager');
jest.mock('../../../lib/projects/ProjectLogsManager');
jest.mock('../../../lib/prompts/projectsLogsPrompt');
jest.mock('../../../lib/ui/table');
jest.mock('../../../lib/errorHandlers');
Expand Down
2 changes: 1 addition & 1 deletion commands/project/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { uiBetaTag, uiLine, uiLink } = require('../../lib/ui');
const { projectLogsPrompt } = require('../../lib/prompts/projectsLogsPrompt');
const { i18n } = require('../../lib/lang');
const { EXIT_CODES } = require('../../lib/enums/exitCodes');
const ProjectLogsManager = require('../../lib/ProjectLogsManager');
const { ProjectLogsManager } = require('../../lib/projects/ProjectLogsManager');

const i18nKey = 'commands.project.subcommands.logs';

Expand Down
2 changes: 2 additions & 0 deletions lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ en:
noFunctionsInProject: "There aren't any functions in this project\n\t- Run `{{#orange}}hs project logs --help{{/orange}}` to learn more about logs\n\t- {{link}} to learn more about serverless functions"
noFunctionWithName: "No function with name \"{{ name }}\""
functionNotDeployed: "The function with name \"{{ name }}\" is not deployed"
projectLogsManagerNotInitialized: "Function called on ProjectLogsManager before initialization"
generic: "Error fetching logs"
logs:
showingLogs: "Showing logs for:"
hubspotLogsDirectLink: "View function logs in HubSpot"
Expand Down
15 changes: 7 additions & 8 deletions lib/__tests__/ProjectLogsManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
const ProjectLogsManager = require('../ProjectLogsManager');
const { ProjectLogsManager } = require('../projects/ProjectLogsManager');
const { getProjectConfig, ensureProjectExists } = require('../projects');
const {
fetchProjectComponentsMetadata,
Expand All @@ -8,7 +8,7 @@ const {
jest.mock('../projects');
jest.mock('@hubspot/local-dev-lib/api/projects');

describe('lib/ProjectLogsManager', () => {
describe('lib/projects/ProjectLogsManager', () => {
const accountId = 12345678;
const appId = 999999;
const projectName = 'super cool test project';
Expand Down Expand Up @@ -143,8 +143,8 @@ describe('lib/ProjectLogsManager', () => {
});

describe('getFunctionNames', () => {
it('should return an empty array if functions is nullable', async () => {
ProjectLogsManager.functions = undefined;
it('should return an empty array if functions is empty', async () => {
ProjectLogsManager.functions = [];
expect(ProjectLogsManager.getFunctionNames()).toEqual([]);
});

Expand All @@ -158,8 +158,8 @@ describe('lib/ProjectLogsManager', () => {
});

describe('setFunction', () => {
it('should throw an error when functions is nullable', async () => {
ProjectLogsManager.functions = undefined;
it('should throw an error when functions is empty', async () => {
ProjectLogsManager.functions = [];
expect(() => ProjectLogsManager.setFunction('foo')).toThrow(
`There aren't any functions in this project`
);
Expand All @@ -180,15 +180,14 @@ describe('lib/ProjectLogsManager', () => {
name: 'APP_FUNCTION',
},
deployOutput: {
endpoint: { path: 'yooooooo', method: ['GET'] },
endpoint: { path: 'yooooooo', methods: ['GET'] },
},
};
ProjectLogsManager.functions = [functionToChoose];
ProjectLogsManager.setFunction('function1');
expect(ProjectLogsManager.functionName).toEqual('function1');
expect(ProjectLogsManager.endpointName).toEqual('yooooooo');
expect(ProjectLogsManager.selectedFunction).toEqual(functionToChoose);
expect(ProjectLogsManager.method).toEqual(['GET']);
expect(ProjectLogsManager.isPublicFunction).toEqual(true);
});

Expand Down
78 changes: 45 additions & 33 deletions lib/ProjectLogsManager.ts → lib/projects/ProjectLogsManager.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
// @ts-nocheck
const { getProjectConfig, ensureProjectExists } = require('./projects');
const {
fetchProjectComponentsMetadata,
} = require('@hubspot/local-dev-lib/api/projects');
const { i18n } = require('./lang');
const { uiLink } = require('./ui');
import { getProjectConfig, ensureProjectExists } from './index';
import { fetchProjectComponentsMetadata } from '@hubspot/local-dev-lib/api/projects';
import { AppFunctionComponentMetadata } from '@hubspot/local-dev-lib/types/ComponentStructure';
import { logger } from '@hubspot/local-dev-lib/logger';
import { i18n } from '../lang';
import { uiLink } from '../ui';

const i18nKey = 'commands.project.subcommands.logs';

class ProjectLogsManager {
reset() {
Object.keys(this).forEach(key => {
if (Object.hasOwn(this, key)) {
this[key] = undefined;
}
});
class _ProjectLogsManager {
projectName: string | undefined;
projectId: number | undefined;
accountId: number | undefined;
functions: AppFunctionComponentMetadata[];
selectedFunction: AppFunctionComponentMetadata | undefined;
functionName: string | undefined;
appId: number | undefined;
isPublicFunction: boolean | undefined;
endpointName: string | undefined;

reset(): void {
this.projectName = undefined;
this.projectId = undefined;
this.accountId = undefined;
this.functions = [];
this.selectedFunction = undefined;
this.functionName = undefined;
this.appId = undefined;
this.isPublicFunction = undefined;
this.endpointName = undefined;
}

constructor() {
this.functions = [];
}

async init(accountId) {
async init(accountId: number): Promise<void> {
const { projectConfig } = await getProjectConfig();

if (!projectConfig || !projectConfig.name) {
Expand Down Expand Up @@ -50,11 +67,16 @@ class ProjectLogsManager {
await this.fetchFunctionDetails();
}

async fetchFunctionDetails() {
async fetchFunctionDetails(): Promise<void> {
if (!this.projectId) {
throw new Error(i18n(`${i18nKey}.errors.noProjectConfig`));
}

if (!this.accountId) {
logger.debug(i18n(`${i18nKey}.errors.projectLogsManagerNotInitialized`));
throw new Error(i18n(`${i18nKey}.errors.generic`));
}

const {
data: { topLevelComponentMetadata },
} = await fetchProjectComponentsMetadata(this.accountId, this.projectId);
Expand All @@ -64,15 +86,12 @@ class ProjectLogsManager {
return type && type.name === 'PRIVATE_APP';
});

if (!this.functions) {
this.functions = [];
}

apps.forEach(app => {
this.functions.push(
...app.featureComponents.filter(
// If component type is APP_FUNCTION, we can safely cast as AppFunctionComponentMetadata
...(app.featureComponents.filter(
component => component.type.name === 'APP_FUNCTION'
)
) as AppFunctionComponentMetadata[])
);
});

Expand All @@ -89,26 +108,20 @@ class ProjectLogsManager {
}

getFunctionNames() {
if (!this.functions) {
return [];
}
return this.functions.map(
serverlessFunction => serverlessFunction.componentName
);
}

setFunction(functionName) {
if (!this.functions) {
setFunction(functionName: string) {
if (!(this.functions.length > 0)) {
throw new Error(
i18n(`${i18nKey}.errors.noFunctionsInProject`, {
link: uiLink(
i18n(`${i18nKey}.errors.noFunctionsLinkText`),
'https://developers.hubspot.com/docs/platform/serverless-functions'
),
}),
{
projectName: this.projectName,
}
})
);
}

Expand All @@ -133,12 +146,11 @@ class ProjectLogsManager {

if (this.selectedFunction.deployOutput.endpoint) {
this.endpointName = this.selectedFunction.deployOutput.endpoint.path;
this.method = this.selectedFunction.deployOutput.endpoint.method;
this.isPublicFunction = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was both trying to access a field that doesn't exist (it should be methods) and not actually used anywhere so I just removed it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😭 Well, it's good we caught it now

} else {
this.isPublicFunction = false;
}
}
}

module.exports = new ProjectLogsManager();
export const ProjectLogsManager = new _ProjectLogsManager();
Loading