From 942cb44e05c3e27c2e3ac1bd72a8f9abd80ce138 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Mon, 1 Mar 2021 20:52:46 +0100 Subject: [PATCH] Utils: Extract getServerVersionFromPackageJson into own file We need this function in at least on other part of the application so extracting it into an util file was only logical. Signed-off-by: Philip Molares --- src/monitoring/monitoring.service.ts | 30 ++---------------------- src/utils/serverVersion.ts | 34 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 src/utils/serverVersion.ts diff --git a/src/monitoring/monitoring.service.ts b/src/monitoring/monitoring.service.ts index a410004a1..190aef2ed 100644 --- a/src/monitoring/monitoring.service.ts +++ b/src/monitoring/monitoring.service.ts @@ -5,34 +5,8 @@ */ import { Injectable } from '@nestjs/common'; -import { promises as fs } from 'fs'; -import { join as joinPath } from 'path'; -import { ServerStatusDto, ServerVersion } from './server-status.dto'; - -let versionCache: ServerVersion; - -async function getServerVersionFromPackageJson(): Promise { - if (versionCache === null) { - const rawFileContent: string = await fs.readFile( - joinPath(__dirname, '../../package.json'), - { encoding: 'utf8' }, - ); - // TODO: Should this be validated in more detail? - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - const packageInfo: { version: string } = JSON.parse(rawFileContent); - const versionParts: number[] = packageInfo.version - .split('.') - .map((x) => parseInt(x, 10)); - versionCache = { - major: versionParts[0], - minor: versionParts[1], - patch: versionParts[2], - preRelease: 'dev', // TODO: Replace this? - }; - } - - return versionCache; -} +import { ServerStatusDto } from './server-status.dto'; +import { getServerVersionFromPackageJson } from '../utils/serverVersion'; @Injectable() export class MonitoringService { diff --git a/src/utils/serverVersion.ts b/src/utils/serverVersion.ts new file mode 100644 index 000000000..21a553cc2 --- /dev/null +++ b/src/utils/serverVersion.ts @@ -0,0 +1,34 @@ +/* + * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { ServerVersion } from '../monitoring/server-status.dto'; +import { promises as fs } from 'fs'; +import { join as joinPath } from 'path'; + +let versionCache: ServerVersion; + +export async function getServerVersionFromPackageJson(): Promise { + if (versionCache === null) { + const rawFileContent: string = await fs.readFile( + joinPath(__dirname, '../../package.json'), + { encoding: 'utf8' }, + ); + // TODO: Should this be validated in more detail? + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const packageInfo: { version: string } = JSON.parse(rawFileContent); + const versionParts: number[] = packageInfo.version + .split('.') + .map((x) => parseInt(x, 10)); + versionCache = { + major: versionParts[0], + minor: versionParts[1], + patch: versionParts[2], + preRelease: 'dev', // TODO: Replace this? + }; + } + + return versionCache; +}