Skip to content

Commit 557260d

Browse files
committed
small module fixes
1 parent e99b0d6 commit 557260d

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

apps/api/scripts/generate-metadata.ts

+25-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
/**
2-
* This file is responsible for generating Nest.js metadata for the API.
3-
* Metadata generation is required when using SWC with Nest.js due to SWC
4-
* not natively supporting Typescript, which is required to use the `reflect-metadata`
5-
* API and in turn, resolve types for the OpenAPI specification.
6-
*
7-
* @see https://docs.nestjs.com/recipes/swc#monorepo-and-cli-plugins
8-
*/
91
import fs from 'node:fs';
102
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
114
import { PluginMetadataGenerator } from '@nestjs/cli/lib/compiler/plugins';
125
import { ReadonlyVisitor } from '@nestjs/swagger/dist/plugin';
136

14-
const tsconfigPath = 'tsconfig.build.json';
15-
const srcPath = path.join(__dirname, '..', 'src');
16-
const metadataPath = path.join(srcPath, 'metadata.ts');
7+
// Get the current file's directory
8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = path.dirname(__filename);
10+
11+
// More precise path resolution
12+
const projectRoot = path.resolve(__dirname, '..', '..');
13+
const apiRoot = path.resolve(projectRoot, 'api');
14+
const srcPath = path.resolve(apiRoot, 'src');
15+
const metadataPath = path.resolve(srcPath, 'metadata.ts');
16+
17+
// Ensure directories exist
18+
fs.mkdirSync(path.dirname(metadataPath), { recursive: true });
1719

1820
/*
1921
* We create an empty metadata file to ensure that files importing `metadata.ts`
@@ -24,9 +26,15 @@ const defaultContent = `export default async () => { return {}; };`;
2426
fs.writeFileSync(metadataPath, defaultContent, 'utf8');
2527
console.log('metadata.ts file has been generated with default content.');
2628

27-
const generator = new PluginMetadataGenerator();
28-
generator.generate({
29-
visitors: [new ReadonlyVisitor({ introspectComments: true, pathToSource: srcPath })],
30-
outputDir: srcPath,
31-
tsconfigPath,
32-
});
29+
// Wrap in try-catch for better error handling
30+
try {
31+
const generator = new PluginMetadataGenerator();
32+
generator.generate({
33+
visitors: [new ReadonlyVisitor({ introspectComments: true, pathToSource: srcPath })],
34+
outputDir: srcPath,
35+
tsconfigPath: 'tsconfig.build.json',
36+
});
37+
} catch (error) {
38+
console.error('Error generating metadata:', error);
39+
process.exit(1);
40+
}

apps/api/src/app/content-templates/content-templates.controller.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
/* eslint-disable global-require */
21
import { Body, Controller, Logger, Post } from '@nestjs/common';
32
import { ApiExcludeController } from '@nestjs/swagger';
43
import { format } from 'date-fns';
5-
import i18next from 'i18next';
4+
import i18next, { i18n } from 'i18next';
65
import { ModuleRef } from '@nestjs/core';
76
import {
87
ApiException,
@@ -148,18 +147,24 @@ export class ContentTemplatesController {
148147
);
149148
}
150149

151-
protected async initiateTranslations(environmentId: string, organizationId: string, locale: string | undefined) {
150+
async initiateTranslations(
151+
environmentId: string,
152+
organizationId: string,
153+
locale: string | undefined
154+
): Promise<i18n | undefined> {
152155
try {
153156
if (process.env.NOVU_ENTERPRISE === 'true' || process.env.CI_EE_TEST === 'true') {
157+
// eslint-disable-next-line global-require
154158
if (!require('@novu/ee-shared-services')?.TranslationsService) {
155159
throw new ApiException('Translation module is not loaded');
156160
}
161+
// eslint-disable-next-line global-require
157162
const service = this.moduleRef.get(require('@novu/ee-shared-services')?.TranslationsService, { strict: false });
158163
const { namespaces, resources, defaultLocale } = await service.getTranslationsList(
159164
environmentId,
160165
organizationId
161166
);
162-
const instance = i18next.createInstance();
167+
const instance: i18n = i18next.createInstance();
163168
await instance.init({
164169
resources,
165170
ns: namespaces,
@@ -184,6 +189,7 @@ export class ContentTemplatesController {
184189
}
185190
} catch (e) {
186191
Logger.error(e, `Unexpected error while importing enterprise modules`, 'TranslationsService');
192+
throw e;
187193
}
188194
}
189195
}

0 commit comments

Comments
 (0)