Skip to content

Commit 849d5e1

Browse files
committed
refactor: extract project version reading to reusable utility
1 parent 35d1c64 commit 849d5e1

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

api/src/rpc/router.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// SPDX-License-Identifier: MIT
44

55
import { initTRPC, TRPCError } from "@trpc/server";
6-
import * as fs from "fs";
7-
import { join as pathJoin } from "path";
86
import superjson from "superjson";
97
import type { Equals, ReturnType } from "tsafe";
108
import { assert } from "tsafe/assert";
@@ -21,7 +19,7 @@ import {
2119
SoftwareType,
2220
UserWithId
2321
} from "../core/usecases/readWriteSillData";
24-
import { getMonorepoRootPackageJson } from "../tools/getMonorepoRootPackageJson";
22+
import { projectVersion } from "../tools/projectVersion";
2523
import type { OptionalIfCanBeUndefined } from "../tools/OptionalIfCanBeUndefined";
2624
import type { Context } from "./context";
2725
import type { OidcParams } from "../core/usecases/auth/oidcClient";
@@ -87,15 +85,7 @@ export function createRouter(params: {
8785
// PUBLIC PROCEDURES
8886
"getRedirectUrl": loggedProcedure.query(() => redirectUrl),
8987
"getExternalSoftwareDataOrigin": loggedProcedure.query(async () => (await dbApi.source.getMainSource()).kind),
90-
"getApiVersion": loggedProcedure.query(
91-
(() => {
92-
const out: string = JSON.parse(
93-
fs.readFileSync(pathJoin(getMonorepoRootPackageJson(), "package.json")).toString("utf8")
94-
)["version"];
95-
96-
return () => out;
97-
})()
98-
),
88+
"getApiVersion": loggedProcedure.query(() => projectVersion),
9989
"getOidcManageProfileUrl": loggedProcedure.query(() => oidcParams.manageProfileUrl),
10090
"getUiConfig": loggedProcedure.query(async () => ({
10191
uiConfig,

api/src/tools/getMonorepoRootPackageJson.ts renamed to api/src/tools/projectVersion.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import * as fs from "fs";
66
import * as path from "path";
7+
import { join as pathJoin } from "path";
78

89
function getProjectRootRec(dirPath: string): string {
910
if (fs.existsSync(path.join(dirPath, "package.json"))) {
@@ -16,7 +17,7 @@ function getProjectRootRec(dirPath: string): string {
1617
}
1718

1819
let monorepoRoot: string | undefined = undefined;
19-
export function getMonorepoRootPackageJson(): string {
20+
function getMonorepoRootPackageJson(): string {
2021
if (monorepoRoot !== undefined) {
2122
return monorepoRoot;
2223
}
@@ -30,3 +31,7 @@ export function getMonorepoRootPackageJson(): string {
3031

3132
return monorepoRoot;
3233
}
34+
35+
export const projectVersion: string = JSON.parse(
36+
fs.readFileSync(pathJoin(getMonorepoRootPackageJson(), "package.json")).toString("utf8")
37+
)["version"];

0 commit comments

Comments
 (0)