Refactor to use version object #59

Enable writing safer version aware logic.
This commit is contained in:
undergroundwires
2022-02-26 17:15:30 +01:00
parent d6bc33ec86
commit eeb1d5b0c4
14 changed files with 198 additions and 35 deletions

View File

@@ -1,8 +1,9 @@
import { OperatingSystem } from './OperatingSystem';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { Version } from '@/domain/Version';
export interface IProjectInformation {
readonly name: string;
readonly version: string;
readonly version: Version;
readonly repositoryUrl: string;
readonly homepage: string;
readonly feedbackUrl: string;

View File

@@ -1,21 +1,22 @@
import { assertInRange } from '@/application/Common/Enum';
import { IProjectInformation } from './IProjectInformation';
import { OperatingSystem } from './OperatingSystem';
import { Version } from './Version';
export class ProjectInformation implements IProjectInformation {
public readonly repositoryWebUrl: string;
constructor(
public readonly name: string,
public readonly version: string,
public readonly version: Version,
public readonly repositoryUrl: string,
public readonly homepage: string,
) {
if (!name) {
throw new Error('name is undefined');
}
if (!version || +version <= 0) {
throw new Error('version should be higher than zero');
if (!version) {
throw new Error('undefined version');
}
if (!repositoryUrl) {
throw new Error('repositoryUrl is undefined');
@@ -27,7 +28,8 @@ export class ProjectInformation implements IProjectInformation {
}
public getDownloadUrl(os: OperatingSystem): string {
return `${this.repositoryWebUrl}/releases/download/${this.version}/${getFileName(os, this.version)}`;
const fileName = getFileName(os, this.version.toString());
return `${this.repositoryWebUrl}/releases/download/${this.version}/${fileName}`;
}
public get feedbackUrl(): string {

24
src/domain/Version.ts Normal file
View File

@@ -0,0 +1,24 @@
export class Version {
public readonly major: number;
public readonly minor: number;
public readonly patch: number;
public constructor(semanticVersion: string) {
if (!semanticVersion) {
throw new Error('empty version');
}
if (!semanticVersion.match(/^\d+\.\d+\.\d+$/g)) {
throw new Error(`invalid version: ${semanticVersion}`);
}
const [major, minor, patch] = semanticVersion.split('.');
this.major = parseInt(major, 10);
this.minor = parseInt(minor, 10);
this.patch = parseInt(patch, 10);
}
public toString(): string {
return `${this.major}.${this.minor}.${this.patch}`;
}
}