From cb1e39557971b4b448387a7d756dd5a1042aa0dc Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sat, 26 Apr 2025 18:02:07 +0800 Subject: [PATCH] refactor: update project root detection and quest directory path --- server/src/config.ts | 18 +++++++++++++++--- server/src/models/quest.ts | 3 ++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/server/src/config.ts b/server/src/config.ts index 4c942e0..117909b 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -1,8 +1,9 @@ import validator from 'validator' import { config as dotenvConfig } from 'dotenv' import path from 'path' -import { get } from './utils/env.js' +import fs from 'fs' import { loadOrCreateKeyPair } from './utils/crypto.js' +import { get } from './utils/env.js' // Import dotenv and load ../.env dotenvConfig({ path: '../.env' }) @@ -92,8 +93,19 @@ function createConfig() { } function getProjectRoot(): string { - const root = path.join(path.dirname(__filename), '..', '..') - return root + // Start from the current file's directory + let currentDir = __dirname + + // Keep going up until we find the root package.json with workspaces + while (currentDir !== path.parse(currentDir).root) { + const packageJsonPath = path.join(currentDir, 'package.json') + if (fs.existsSync(packageJsonPath)) { + return path.join(currentDir, '..') + } + currentDir = path.dirname(currentDir) + } + + throw new Error('Could not find project root directory') } const config = createConfig() diff --git a/server/src/models/quest.ts b/server/src/models/quest.ts index b2740ec..fa5f71d 100644 --- a/server/src/models/quest.ts +++ b/server/src/models/quest.ts @@ -9,6 +9,7 @@ import { IQuest, } from '../../../packages/game' import logger from '../utils/logger.js' +import config from '../config.js' class QuestValidationError extends Error { questId: string @@ -37,7 +38,7 @@ class QuestValidationError extends Error { class QuestManager { private quests = new Map() - private questDirectory = path.join(process.cwd(), '..', 'quests') + private questDirectory = path.join(config.projectRoot, 'quests') get(id: string) { return this.quests.get(id)