From 8e95121ac82dee1b2b6bc4f9123fa9614c627e22 Mon Sep 17 00:00:00 2001 From: adrinr Date: Mon, 17 Apr 2023 14:07:49 +0100 Subject: [PATCH] Implement find version --- packages/backend-core/src/environment.ts | 29 +++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/backend-core/src/environment.ts b/packages/backend-core/src/environment.ts index d4a981b05c..0d413b8fa9 100644 --- a/packages/backend-core/src/environment.ts +++ b/packages/backend-core/src/environment.ts @@ -1,3 +1,5 @@ +import { existsSync, readFileSync } from "fs" + function isTest() { return isCypress() || isJest() } @@ -46,7 +48,32 @@ function httpLogging() { } function findVersion() { - return "0.0.1" + function findFileInAncestors( + fileName: string, + currentDir: string + ): string | null { + const filePath = `${currentDir}/${fileName}` + if (existsSync(filePath)) { + return filePath + } + + const parentDir = `${currentDir}/..` + if (parentDir === currentDir) { + // reached root directory + return null + } + + return findFileInAncestors(fileName, parentDir) + } + + try { + const packageJsonFile = findFileInAncestors("package.json", process.cwd()) + const content = readFileSync(packageJsonFile!, "utf-8") + const version = JSON.parse(content).version + return version + } catch { + throw new Error("Cannot find a valid version in its package.json") + } } const environment = {