fix docker build
This commit is contained in:
@@ -3,6 +3,7 @@ import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import process from 'node:process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { createRequire } from 'node:module';
|
||||
import { z } from 'zod';
|
||||
import { AppDataSource } from '../config/data-source.js';
|
||||
import { env } from '../config/env.js';
|
||||
@@ -38,42 +39,53 @@ const userUpdateSchema = z.object({
|
||||
defaultCurrency: z.string().min(3).max(8).optional()
|
||||
});
|
||||
|
||||
type PackageJson = { version?: string };
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
const readPackageJsonSafe = (filePath: string | null): PackageJson | null => {
|
||||
if (!filePath || !fs.existsSync(filePath)) return null;
|
||||
type PackageLike = { version?: string };
|
||||
|
||||
function readPackageJsonSafe(filePath: string): PackageLike | null {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(filePath, 'utf8')) as PackageJson;
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
return JSON.parse(fs.readFileSync(filePath, 'utf8')) as PackageLike;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const findNearestPackageJson = (startDir: string): string | null => {
|
||||
let currentDir = path.resolve(startDir);
|
||||
function findApiPackage(): PackageLike | null {
|
||||
return (
|
||||
readPackageJsonSafe(path.resolve(__dirname, '../../package.json')) ??
|
||||
readPackageJsonSafe(path.resolve(__dirname, '../package.json'))
|
||||
);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
const candidate = path.join(currentDir, 'package.json');
|
||||
if (fs.existsSync(candidate)) return candidate;
|
||||
|
||||
const parentDir = path.dirname(currentDir);
|
||||
if (parentDir == currentDir) return null;
|
||||
currentDir = parentDir;
|
||||
function findRootPackage(): PackageLike | null {
|
||||
const candidates = [
|
||||
path.resolve(__dirname, '../../../package.json'),
|
||||
path.resolve(__dirname, '../../package.json'),
|
||||
path.resolve(__dirname, '../package.json')
|
||||
];
|
||||
for (const candidate of candidates) {
|
||||
const pkg = readPackageJsonSafe(candidate);
|
||||
if (!pkg) continue;
|
||||
if (candidate.endsWith('/api/package.json') || candidate.endsWith('\\api\\package.json')) continue;
|
||||
return pkg;
|
||||
}
|
||||
};
|
||||
return null;
|
||||
}
|
||||
|
||||
const apiPackagePath = findNearestPackageJson(__dirname);
|
||||
const apiPackage = readPackageJsonSafe(apiPackagePath);
|
||||
const apiRootDir = apiPackagePath ? path.dirname(apiPackagePath) : null;
|
||||
const projectRootPackagePath = apiRootDir ? findNearestPackageJson(path.dirname(apiRootDir)) : null;
|
||||
const rootPackage = readPackageJsonSafe(projectRootPackagePath);
|
||||
const webPackage = readPackageJsonSafe(
|
||||
projectRootPackagePath ? path.join(path.dirname(projectRootPackagePath), 'web', 'package.json') : null
|
||||
);
|
||||
function findWebPackage(): PackageLike | null {
|
||||
return (
|
||||
readPackageJsonSafe(path.resolve(__dirname, '../../../web/package.json')) ??
|
||||
readPackageJsonSafe(path.resolve(__dirname, '../../web/package.json'))
|
||||
);
|
||||
}
|
||||
|
||||
const rootPackage = findRootPackage();
|
||||
const apiPackage = findApiPackage();
|
||||
const webPackage = findWebPackage();
|
||||
|
||||
const settingsRepo = () => AppDataSource.getRepository(AppSetting);
|
||||
const userRepo = () => AppDataSource.getRepository(User);
|
||||
|
||||
Reference in New Issue
Block a user