Skip to content

Commit bb0f504

Browse files
author
Lasim
committed
feat(backend): resolve plugin path determination for production environment
1 parent 2da79a9 commit bb0f504

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

services/backend/src/plugin-system/plugin-manager.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,12 @@ export class PluginManager {
199199
await fsPromises.access(packageJsonPath);
200200
this.logger?.debug(`Found package.json in: ${entryPath}`);
201201

202-
// Check if we're running from dist directory
202+
// Determine file extension based on whether we're running from dist
203203
const isRunningFromDist = __dirname.includes('/dist/');
204+
const pluginExtension = isRunningFromDist ? 'js' : 'ts';
204205

205-
// Determine the appropriate plugin file path and extension
206-
let pluginBasePath = entryPath;
207-
let pluginExtension = isRunningFromDist ? 'js' : 'ts';
208-
209-
// If running from dist and looking at a source path, redirect to dist
210-
if (isRunningFromDist && pluginBasePath.includes('/src/')) {
211-
pluginBasePath = pluginBasePath.replace('/src/', '/dist/');
212-
}
206+
// Use entryPath as-is since server.ts now provides the correct path
207+
const pluginBasePath = entryPath;
213208

214209
const mainPath = path.join(pluginBasePath, `index.${pluginExtension}`);
215210

services/backend/src/server.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,36 @@ export const createServer = async () => {
446446

447447
// Create and configure the plugin manager
448448
const isDevelopment = process.env.NODE_ENV !== 'production';
449+
450+
// Determine plugin path based on where we're running from
451+
let pluginPath: string;
452+
if (process.env.PLUGINS_PATH) {
453+
pluginPath = process.env.PLUGINS_PATH;
454+
} else if (isDevelopment) {
455+
// Development: src/plugins
456+
pluginPath = path.join(process.cwd(), 'src', 'plugins');
457+
} else {
458+
// Production: need to find dist/plugins relative to the compiled file
459+
// __dirname when running from dist/ is: /path/to/project/dist (or similar)
460+
// We need to go to dist/plugins
461+
const currentDir = __dirname;
462+
463+
// Check if we're in a nested dist structure (e.g., dist/some/path)
464+
// Find the dist directory root
465+
const distMatch = currentDir.match(/^(.+?\/dist)(?:\/|$)/);
466+
if (distMatch) {
467+
// We found /dist/ in the path, use that as the base
468+
pluginPath = path.join(distMatch[1], 'plugins');
469+
} else {
470+
// Fallback: assume dist is at current directory
471+
pluginPath = path.join(currentDir, 'plugins');
472+
}
473+
}
474+
475+
server.log.info(`Plugin path resolved to: ${pluginPath}`);
476+
449477
const pluginManager = new PluginManager({
450-
paths: [
451-
process.env.PLUGINS_PATH || (isDevelopment
452-
? path.join(process.cwd(), 'src', 'plugins')
453-
: path.join(__dirname, '..', 'plugins')),
454-
],
478+
paths: [pluginPath],
455479
plugins: {}
456480
})
457481

services/backend/webpack.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ module.exports = {
2525
dot: true, // Include dotfiles if any
2626
},
2727
},
28+
// Copy plugin package.json files to dist directory
29+
{
30+
from: 'src/plugins/**/package.json',
31+
to: ({ context, absoluteFilename }) => {
32+
// Extract the plugin directory name from the absolute filename
33+
// e.g., from 'src/plugins/send-discord-message-on-register/package.json'
34+
// we want 'plugins/send-discord-message-on-register/package.json'
35+
const relativePath = path.relative(path.join(context, 'src'), absoluteFilename);
36+
return relativePath;
37+
},
38+
globOptions: {
39+
dot: true,
40+
},
41+
},
2842
],
2943
}),
3044
],

0 commit comments

Comments
 (0)