Skip to content

A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.

fix: unstyled email notifications in desktop installations.

towfiqicc4a807c68e7
diff --git a/backend/package.json b/backend/package.json
index a7df55e..23796b1 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -1,7 +1,7 @@
{
"name": "@plutonhq/core-backend",
"description": "Pluton Core Backend Library",
- "version": "0.1.0",
+ "version": "0.1.1",
"author": "plutonhq",
"license": "Apache-2.0",
"publishConfig": {
@@ -98,7 +98,8 @@
"public/**/*",
"node_modules/**/*",
"!node_modules/better-sqlite3/**/*",
- "drizzle/**/*"
+ "drizzle/**/*",
+ "src/notifications/templates/**/*"
]
},
"scripts": {
diff --git a/backend/scripts/build.js b/backend/scripts/build.js
index 7eedad3..922f986 100644
--- a/backend/scripts/build.js
+++ b/backend/scripts/build.js
@@ -1,5 +1,5 @@
import { execSync } from 'child_process';
-import { readdir, readFile, writeFile } from 'fs/promises';
+import { readdir, readFile, writeFile, cp } from 'fs/promises';
import { join, extname } from 'path';
import { minify } from 'terser';
// Read version from root package.json (monorepo source of truth)
@@ -27,7 +27,29 @@ try {
process.exit(1); // Exit with an error code
}
-// 3. Remove console.logs from production build
+// 3. Copy templates to dist folder (they are not compiled by tsc)
+async function copyTemplates() {
+ const srcTemplates = join(process.cwd(), 'src', 'notifications', 'templates');
+ const destTemplates = join(process.cwd(), 'dist', 'notifications', 'templates');
+
+ try {
+ await cp(srcTemplates, destTemplates, { recursive: true });
+ console.log('✅ Templates copied to dist folder!');
+ } catch (error) {
+ console.error('Error copying templates:', error);
+ throw error;
+ }
+}
+
+try {
+ console.log('Copying templates to dist folder...');
+ await copyTemplates();
+} catch (error) {
+ console.error('❌ Failed to copy templates:', error);
+ process.exit(1);
+}
+
+// 4. Remove console.logs from production build
async function removeConsoleLogs(dir) {
try {
const entries = await readdir(dir, { withFileTypes: true });
diff --git a/backend/src/notifications/BaseNotification.ts b/backend/src/notifications/BaseNotification.ts
index ca03698..567c0b0 100644
--- a/backend/src/notifications/BaseNotification.ts
+++ b/backend/src/notifications/BaseNotification.ts
@@ -1,8 +1,5 @@
-import { readFileSync } from 'fs';
-import path from 'path';
-import { dirname } from 'path';
-import { fileURLToPath } from 'url';
import { configService } from '../services/ConfigService';
+import { loadEmailWrapperTemplate } from './templateLoader';
export abstract class BaseNotification {
protected subject: string = '';
@@ -32,27 +29,7 @@ export abstract class BaseNotification {
}
protected loadEmailTemplate(): string {
- try {
- // Get the path to the email template
- // const __filename = fileURLToPath(import.meta.url);
- // const __dirname = dirname(__filename);
- // return readFileSync(path.join(__dirname, 'templates', 'email', 'email.html'), {
- // encoding: 'utf-8',
- // });
- const templatePath = path.join(
- process.cwd(),
- 'src',
- 'notifications',
- 'templates',
- 'email',
- 'email.html'
- );
- console.log('💌 template Path :', templatePath);
- return readFileSync(templatePath, 'utf-8');
- } catch (err) {
- console.log('Error loading email template:', err);
- return ''; // Fallback to empty string if file read fails
- }
+ return loadEmailWrapperTemplate();
}
protected applyEmailTemplate(
diff --git a/backend/src/notifications/channels/NotificationChannelResolver.ts b/backend/src/notifications/channels/NotificationChannelResolver.ts
index 4207316..96d8be1 100644
--- a/backend/src/notifications/channels/NotificationChannelResolver.ts
+++ b/backend/src/notifications/channels/NotificationChannelResolver.ts
@@ -56,7 +56,8 @@ export class NotificationChannelResolver {
console.log('[Decrypted] :', fieldPath, decryptedValue);
setNestedValue(integrationDecrypted, fieldPath, decryptedValue);
} catch (error) {
- setNestedValue(integrationDecrypted, fieldPath, value);
+ // Handle decryption errors (e.g., if value is not encrypted)
+ console.warn(`Failed to decrypt field ${fieldPath}:`, error);
}
}
});
diff --git a/backend/src/notifications/channels/SMTPChannel.ts b/backend/src/notifications/channels/SMTPChannel.ts
index a553ef8..99af409 100644
--- a/backend/src/notifications/channels/SMTPChannel.ts
+++ b/backend/src/notifications/channels/SMTPChannel.ts
@@ -15,7 +15,7 @@ export class SMTPChannel implements NotificationChannel {
const smtpConfig = this.smtpSettings;
const senderName = smtpConfig?.senderName || 'Pluton Backup';
- console.log('smtpConfig :', smtpConfig);
+ // console.log('smtpConfig :', smtpConfig);/
this.transporter = nodemailer.createTransport({
host: smtpConfig.server,
port: smtpConfig.port,
@@ -53,7 +53,7 @@ export class SMTPChannel implements NotificationChannel {
};
const result = await this.transporter.sendMail(mailOptions);
- console.log('Email Send Result :', result);
+ // console.log('Email Send Result :', result);
return {
success: true,
diff --git a/backend/src/notifications/templateLoader.ts b/backend/src/notifications/templateLoader.ts
new file mode 100644
index 0000000..42e03e5
--- /dev/null
+++ b/backend/src/notifications/templateLoader.ts
@@ -0,0 +1,86 @@
+import { readFileSync, existsSync } from 'fs';
+import path from 'path';
+import { fileURLToPath } from 'url';
+
+/**
+ * Utility for loading template files that works in:
+ * - Development mode (tsx, running from source)
+ * - Production mode (node, running from dist/ after tsc build)
+ * - Pkg mode (pkg executable with bundled assets in snapshot filesystem)
+ *
+ * In pkg builds, files are bundled into a virtual filesystem snapshot.
+ * We use import.meta.url to get the correct path relative to the module.
+ */
+
+// Check if running inside a pkg executable
+const isPkg = typeof (process as any).pkg !== 'undefined';
+
+// Get current directory using import.meta.url (works in ESM)
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+
+/**
+ * Get the base path for templates based on the execution context
+ */
+function getTemplateBasePath(): string {
+ if (isPkg) {
+ // In pkg, the bundled file is at dist/pkg-index.cjs
+ // __dirname will be /snapshot/.../backend/dist/
+ // Templates are bundled at /snapshot/.../backend/src/notifications/templates/
+ // So we go from dist/ → src/notifications/templates/
+ return path.join(__dirname, '..', 'src', 'notifications', 'templates');
+ }
+
+ // Try multiple paths for non-pkg environments:
+ // 1. Relative to __dirname (works for dist/ builds where templates are copied)
+ // 2. src/ folder relative to process.cwd() (works for development)
+ // 3. dist/ folder relative to process.cwd() (works for production builds)
+ const possiblePaths = [
+ // Templates relative to this module (dist/notifications/templates or src/notifications/templates)
+ path.join(__dirname, 'templates'),
+ // Development: src folder
+ path.join(process.cwd(), 'src', 'notifications', 'templates'),
+ // Production: dist folder (if templates are copied there)
+ path.join(process.cwd(), 'dist', 'notifications', 'templates'),
+ ];
+
+ for (const testPath of possiblePaths) {
+ if (existsSync(path.join(testPath, 'email'))) {
+ return testPath;
+ }
+ }
+
+ // Fallback to development path
+ return path.join(process.cwd(), 'src', 'notifications', 'templates');
+}
+
+/**
+ * Load a template file and return its contents as a string
+ * @param templatePath - Path relative to the templates directory (e.g., 'email/email.html')
+ */
+export function loadTemplate(templatePath: string): string {
+ const basePath = getTemplateBasePath();
+ const fullPath = path.join(basePath, templatePath);
+
+ try {
+ return readFileSync(fullPath, 'utf-8');
+ } catch (err) {
+ console.error(`[Templates] Error loading template: ${fullPath}`, err);
+ return '';
+ }
+}
+
+/**
+ * Load the main email wrapper template
+ */
+export function loadEmailWrapperTemplate(): string {
+ return loadTemplate('email/email.html');
+}
+
+/**
+ * Load a backup notification EJS template
+ * @param templateName - Name of the template file (e.g., 'BackupSuccessNotification.ejs')
+ */
+export function loadBackupTemplate(templateName: string): string {
+ return loadTemplate(`email/backup/${templateName}`);
+}
diff --git a/backend/src/notifications/templates/email/backup/BackupFailedNotification.ts b/backend/src/notifications/templates/email/backup/BackupFailedNotification.ts
index df2d98b..f0c996c 100644
--- a/backend/src/notifications/templates/email/backup/BackupFailedNotification.ts
+++ b/backend/src/notifications/templates/email/backup/BackupFailedNotification.ts
@@ -1,13 +1,11 @@
import ejs from 'ejs';
-import fs from 'fs';
-import path from 'path';
-import { fileURLToPath } from 'url';
import { configService } from '../../../../services/ConfigService';
import { Plan } from '../../../../db/schema/plans';
import { providers } from '../../../../utils/providers';
import { BackupNotificationJSONPayload, BackupTaskStats } from '../../../../types/backups';
import { formatBytes, formatDuration, formatNumberToK } from '../../../../utils/formatter';
import { BaseNotification } from '../../../BaseNotification';
+import { loadBackupTemplate } from '../../../templateLoader';
export interface BackupFailedNotificationPayload {
appTitle: string;
@@ -91,20 +89,7 @@ export class BackupFailedNotification extends BaseNotification {
? providers[data.storageType]?.name || data.storageType
: '';
- // const __filename = fileURLToPath(import.meta.url);
- // const __dirname = path.dirname(__filename);
- // const templatePath = path.join(__dirname, 'BackupFailedNotification.ejs');
- const templatePath = path.join(
- process.cwd(),
- 'src',
- 'notifications',
- 'templates',
- 'email',
- 'backup',
- 'BackupFailedNotification.ejs'
- );
- const templateString = fs.readFileSync(templatePath, 'utf-8');
-
+ const templateString = loadBackupTemplate('BackupFailedNotification.ejs');
const renderedBody = ejs.render(templateString, {
...data,
appUrl: configService.config.APP_URL,
diff --git a/backend/src/notifications/templates/email/backup/BackupStartedNotification.ts b/backend/src/notifications/templates/email/backup/BackupStartedNotification.ts
index 59f54a0..bce0a0b 100644
--- a/backend/src/notifications/templates/email/backup/BackupStartedNotification.ts
+++ b/backend/src/notifications/templates/email/backup/BackupStartedNotification.ts
@@ -1,13 +1,11 @@
import ejs from 'ejs';
-import fs from 'fs';
-import path from 'path';
-import { fileURLToPath } from 'url';
import { configService } from '../../../../services/ConfigService';
import { Plan } from '../../../../db/schema/plans';
import { providers } from '../../../../utils/providers';
import { BackupNotificationJSONPayload, BackupTaskStats } from '../../../../types/backups';
import { formatBytes, formatNumberToK } from '../../../../utils/formatter';
import { BaseNotification } from '../../../BaseNotification';
+import { loadBackupTemplate } from '../../../templateLoader';
export interface BackupStartedPayload {
appTitle: string;
@@ -71,20 +69,7 @@ export class BackupStartedNotification extends BaseNotification {
? providers[data.storageType]?.name || data.storageType
: '';
- // const __filename = fileURLToPath(import.meta.url);
- // const __dirname = path.dirname(__filename);
- // const templatePath = path.join(__dirname, 'BackupStartedNotification.ejs');
- const templatePath = path.join(
- process.cwd(),
- 'src',
- 'notifications',
- 'templates',
- 'email',
- 'backup',
- 'BackupStartedNotification.ejs'
- );
- const templateString = fs.readFileSync(templatePath, 'utf-8');
-
+ const templateString = loadBackupTemplate('BackupStartedNotification.ejs');
const renderedBody = ejs.render(templateString, {
...data,
appUrl: configService.config.APP_URL,
diff --git a/backend/src/notifications/templates/email/backup/BackupSuccessNotification.ts b/backend/src/notifications/templates/email/backup/BackupSuccessNotification.ts
index 68d7314..bd20134 100644
--- a/backend/src/notifications/templates/email/backup/BackupSuccessNotification.ts
+++ b/backend/src/notifications/templates/email/backup/BackupSuccessNotification.ts
@@ -1,13 +1,11 @@
import ejs from 'ejs';
-import fs from 'fs';
-import path from 'path';
-import { fileURLToPath } from 'url';
import { configService } from '../../../../services/ConfigService';
import { Plan } from '../../../../db/schema/plans';
import { providers } from '../../../../utils/providers';
import { BackupCompletionStats, BackupNotificationJSONPayload } from '../../../../types/backups';
import { formatBytes, formatDuration, formatNumberToK } from '../../../../utils/formatter';
import { BaseNotification } from '../../../BaseNotification';
+import { loadBackupTemplate } from '../../../templateLoader';
export interface BackupSuccessNotificationPayload {
appTitle: string;
@@ -79,20 +77,7 @@ export class BackupSuccessNotification extends BaseNotification {
const storageTypeName = data.storageType
? providers[data.storageType]?.name || data.storageType
: '';
- // const __filename = fileURLToPath(import.meta.url);
- // const __dirname = path.dirname(__filename);
- // const templatePath = path.join(__dirname, 'BackupSuccessNotification.ejs');
- const templatePath = path.join(
- process.cwd(),
- 'src',
- 'notifications',
- 'templates',
- 'email',
- 'backup',
- 'BackupSuccessNotification.ejs'
- );
- const templateString = fs.readFileSync(templatePath, 'utf-8');
-
+ const templateString = loadBackupTemplate('BackupSuccessNotification.ejs');
const renderedBody = ejs.render(templateString, {
...data,
appUrl: configService.config.APP_URL,