refactor: update project root detection and quest directory path

This commit is contained in:
Justin Lin committed 2025-04-26 18:02:49 +08:00
1 parent fbe1319183
commit cb1e395579
2 files changed
+17 -4

No files matched your search

+15 -3
View File
@@ -1,8 +1,9 @@
import validator from 'validator' import validator from 'validator'
import { config as dotenvConfig } from 'dotenv' import { config as dotenvConfig } from 'dotenv'
import path from 'path' import path from 'path'
import { get } from './utils/env.js' import fs from 'fs'
import { loadOrCreateKeyPair } from './utils/crypto.js' import { loadOrCreateKeyPair } from './utils/crypto.js'
import { get } from './utils/env.js'
// Import dotenv and load ../.env // Import dotenv and load ../.env
dotenvConfig({ path: '../.env' }) dotenvConfig({ path: '../.env' })
@@ -92,8 +93,19 @@ function createConfig() {
} }
function getProjectRoot(): string { function getProjectRoot(): string {
const root = path.join(path.dirname(__filename), '..', '..') // Start from the current file's directory
return root 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() const config = createConfig()
+2 -1
View File
@@ -9,6 +9,7 @@ import {
IQuest, IQuest,
} from '../../../packages/game' } from '../../../packages/game'
import logger from '../utils/logger.js' import logger from '../utils/logger.js'
import config from '../config.js'
class QuestValidationError extends Error { class QuestValidationError extends Error {
questId: string questId: string
@@ -37,7 +38,7 @@ class QuestValidationError extends Error {
class QuestManager { class QuestManager {
private quests = new Map<string, IQuest>() private quests = new Map<string, IQuest>()
private questDirectory = path.join(process.cwd(), '..', 'quests') private questDirectory = path.join(config.projectRoot, 'quests')
get(id: string) { get(id: string) {
return this.quests.get(id) return this.quests.get(id)