diff --git a/packages/sync-server/src/app.ts b/packages/sync-server/src/app.ts index 74d7fc3685..b6d13072dd 100644 --- a/packages/sync-server/src/app.ts +++ b/packages/sync-server/src/app.ts @@ -1,5 +1,6 @@ -import { createRequire } from 'module'; import fs, { readFileSync } from 'node:fs'; +import { join, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; import bodyParser from 'body-parser'; import cors from 'cors'; @@ -68,17 +69,42 @@ app.get('/mode', (req, res) => { }); app.get('/info', (_req, res) => { - const require = createRequire(import.meta.url); - const packageJsonPath = require.resolve( - '@actual-app/sync-server/package.json', - ); - const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); + function findPackageJson(startDir: string) { + // find the nearest package.json file while traversing up the directory tree + let currentPath = startDir; + let directoriesSearched = 0; + const pathRoot = resolve(currentPath, '/'); + try { + while (currentPath !== pathRoot && directoriesSearched < 5) { + const packageJsonPath = resolve(currentPath, 'package.json'); + if (fs.existsSync(packageJsonPath)) { + const packageJson = JSON.parse( + readFileSync(packageJsonPath, 'utf-8'), + ); + + if (packageJson.name === '@actual-app/sync-server') { + return packageJson; + } + } + + currentPath = resolve(join(currentPath, '..')); // Move up one directory + directoriesSearched++; + } + } catch (error) { + console.error('Error while searching for package.json:', error); + } + + return null; + } + + const dirname = resolve(fileURLToPath(import.meta.url), '../'); + const packageJson = findPackageJson(dirname); res.status(200).json({ build: { - name: packageJson.name, - description: packageJson.description, - version: packageJson.version, + name: packageJson?.name, + description: packageJson?.description, + version: packageJson?.version, }, }); }); diff --git a/upcoming-release-notes/5093.md b/upcoming-release-notes/5093.md new file mode 100644 index 0000000000..5e46b49dff --- /dev/null +++ b/upcoming-release-notes/5093.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [MikesGlitch] +--- + +Fix server version display on docker images