move dependency version stuff from EnvService to DependencyService
This commit is contained in:
parent
f103d98099
commit
8059515db4
2 changed files with 43 additions and 34 deletions
41
packages/backend/src/global/DependencyService.ts
Normal file
41
packages/backend/src/global/DependencyService.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
import nodePath from 'node:path';
|
||||||
|
import nodeFs from 'node:fs';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { CacheManagementService, type ManagedMemoryKVCache } from '@/global/CacheManagementService.js';
|
||||||
|
import { bindThis } from '@/decorators.js';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class DependencyService {
|
||||||
|
protected readonly dependencyVersionCache: ManagedMemoryKVCache<string | null>;
|
||||||
|
|
||||||
|
constructor(cacheManagementService: CacheManagementService) {
|
||||||
|
this.dependencyVersionCache = cacheManagementService.createMemoryKVCache<string | null>(Infinity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the installed version of a given dependency, or null if not installed.
|
||||||
|
*/
|
||||||
|
@bindThis
|
||||||
|
public async getDependencyVersion(dependency: string): Promise<string | null> {
|
||||||
|
return await this.dependencyVersionCache.fetch(dependency, async () => {
|
||||||
|
const packageJsonPath = nodePath.join(import.meta.dirname, '../../package.json');
|
||||||
|
const packageJsonText = nodeFs.readFileSync(packageJsonPath, 'utf8');
|
||||||
|
|
||||||
|
// No "dependencies" section -> infer not installed.
|
||||||
|
const packageJson = JSON.parse(packageJsonText) as { dependencies?: Partial<Record<string, string>> };
|
||||||
|
if (packageJson.dependencies == null) return null;
|
||||||
|
|
||||||
|
// Not listed -> not installed.
|
||||||
|
const version = packageJson.dependencies['mfm-js'];
|
||||||
|
if (version == null) return null;
|
||||||
|
|
||||||
|
// Just in case some other value is there
|
||||||
|
return String(version);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,25 +3,15 @@
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import nodePath from 'node:path';
|
|
||||||
import nodeFs from 'node:fs';
|
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { bindThis } from '@/decorators.js';
|
import { createEnvOptions, type EnvOption } from '@/env.js';
|
||||||
import { type ManagedMemoryKVCache, CacheManagementService } from '@/global/CacheManagementService.js';
|
|
||||||
import { type EnvOption, createEnvOptions } from '@/env.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides structured, mockable access to runtime/environment details.
|
* Provides structured, mockable access to runtime/environment details.
|
||||||
*/
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class EnvService {
|
export class EnvService {
|
||||||
protected readonly dependencyVersionCache: ManagedMemoryKVCache<string | null>;
|
protected readonly envOptions: EnvOption = createEnvOptions(() => this.env);
|
||||||
protected readonly envOptions: EnvOption;
|
|
||||||
|
|
||||||
constructor(cacheManagementService: CacheManagementService) {
|
|
||||||
this.dependencyVersionCache = cacheManagementService.createMemoryKVCache<string | null>(Infinity);
|
|
||||||
this.envOptions = createEnvOptions(() => this.env);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the environment variables of the process.
|
* Returns the environment variables of the process.
|
||||||
|
|
@ -38,26 +28,4 @@ export class EnvService {
|
||||||
public get options(): EnvOption {
|
public get options(): EnvOption {
|
||||||
return this.envOptions;
|
return this.envOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the installed version of a given dependency, or null if not installed.
|
|
||||||
*/
|
|
||||||
@bindThis
|
|
||||||
public async getDependencyVersion(dependency: string): Promise<string | null> {
|
|
||||||
return await this.dependencyVersionCache.fetch(dependency, async () => {
|
|
||||||
const packageJsonPath = nodePath.join(import.meta.dirname, '../../package.json');
|
|
||||||
const packageJsonText = nodeFs.readFileSync(packageJsonPath, 'utf8');
|
|
||||||
|
|
||||||
// No "dependencies" section -> infer not installed.
|
|
||||||
const packageJson = JSON.parse(packageJsonText) as { dependencies?: Partial<Record<string, string>> };
|
|
||||||
if (packageJson.dependencies == null) return null;
|
|
||||||
|
|
||||||
// Not listed -> not installed.
|
|
||||||
const version = packageJson.dependencies['mfm-js'];
|
|
||||||
if (version == null) return null;
|
|
||||||
|
|
||||||
// Just in case some other value is there
|
|
||||||
return String(version);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue