Skip to content

Commit

Permalink
feat: Unified Version and added semver checking
Browse files Browse the repository at this point in the history
  • Loading branch information
CEbbinghaus committed Dec 8, 2023
1 parent c9eae63 commit f3360f4
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ charset = utf-8
insert_final_newline = true
end_of_line = lf

[*.{js,ts,tsx,rs}]
[*.{json,js,ts,tsx,rs}]
indent_size = 4
indent_style = tab
5 changes: 1 addition & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ jobs:
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
run: |
echo "`jq \".version+=\\\"-$(git rev-parse --short HEAD)\\\"\" package.json`" > package.json && \
echo "-$(git rev-parse --short HEAD)" >> version && \
pnpm install && \
pnpm build && \
pnpm publish --no-git-checks --tag prerelease


1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enable-pre-post-scripts=true
2 changes: 1 addition & 1 deletion backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "backend"
version = "0.9.7"
version = "0.0.0"
edition = "2021"
license = "GPL-2.0"
authors = ["Christopher-Robin Ebbinghaus <[email protected]>"]
Expand Down
2 changes: 1 addition & 1 deletion backend/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::Path;
pub const PORT: u16 = 12412; // TODO replace with something unique

pub const PACKAGE_NAME: &'static str = env!("CARGO_PKG_NAME");
pub const PACKAGE_VERSION: &'static str = env!("CARGO_PKG_VERSION");
pub const PACKAGE_VERSION: &'static str = include_str!("../../version");
pub const PACKAGE_AUTHORS: &'static str = env!("CARGO_PKG_AUTHORS");

const TEMPDIR: &'static str = "/tmp/MicroSDeck";
Expand Down
1 change: 1 addition & 0 deletions lib/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enable-pre-post-scripts=true
60 changes: 32 additions & 28 deletions lib/package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
{
"name": "@cebbinghaus/microsdeck",
"version": "0.9.7",
"description": "",
"keywords": [],
"author": "CEbbinghaus",
"license": "GPLv2",

"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsc",
"watch": "tsc -w"
},
"repository": {
"type": "git",
"url": "https://github.com/CEbbinghaus/MicroSDeck.git",
"directory": "src/lib"
},
"devDependencies": {
"@types/react": "16.14.0",
"lipe": "^0.3.3"
},
"dependencies": {
"typescript": "^4.9.5"
}
"name": "@cebbinghaus/microsdeck",
"version": "0.0.0",
"description": "",
"keywords": [],
"author": "CEbbinghaus",
"license": "GPLv2",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"prebuild": "node ../util/updateVersion.js",
"build": "tsc",
"postbuild": "node ../util/resetVersion.js",
"watch": "tsc -w",
"prepack": "node ../util/updateVersion.js"
},
"repository": {
"type": "git",
"url": "https://github.com/CEbbinghaus/MicroSDeck.git",
"directory": "src/lib"
},
"devDependencies": {
"@types/react": "16.14.0",
"@types/semver": "^7.5.6",
"lipe": "^0.3.3",
"typescript": "^4.9.5"
},
"dependencies": {
"semver": "^7.5.4"
}
}
25 changes: 16 additions & 9 deletions lib/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 54 additions & 5 deletions lib/src/MicoSDeck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { Event as BackendEvent, EventType, fetchCardsAndGames, fetchCardsForGame
import Logger from "lipe";
import { CardAndGames, CardsAndGames, MicroSDCard } from "./types.js"

import semverParse from "semver/functions/parse"
import semverEq from "semver/functions/eq.js"

import { version } from "../package.json"

function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(() => resolve(), ms));
}
Expand All @@ -20,6 +25,36 @@ export type EventBus<K extends string, T extends Event> = {
removeEventListener(type: K, callback: EventBusListener<T> | EventBusListenerObject<T> | null, options?: boolean | EventListenerOptions | undefined): void;
}

export { version };

/**
* Matches the current MicroSDeck version against the version provided.
* Returns true if the semver matches and compatibility is guaranteed.
* @export
* @param {string} version1 his should be in valid semver notation of x.y.z
* @param {string} version2 This should be in valid semver notation of x.y.z
*/
export function IsMatchingSemver(version1: string, version2: string, strict: boolean = false): boolean {
const semVersion1 = semverParse(version1) ?? (() => {throw new Error("Current Version is not a valid Semver. (How did we get here?)")})();
const semVersion2 = semverParse(version2) ?? (() => {throw new Error("provided Version is not a valid Semver.")})();;

// the two major versions do not match. Not valid
if(semVersion1.major != semVersion2.major && semVersion1.major != 0) {
return false;
}

// The minor version can introduce breaking changes while <1.0
if(semVersion1.minor != semVersion2.minor && semVersion1.major == 0) {
return false;
}

if(!strict) {
return true;
}

return semverEq(semVersion1, semVersion2);
}

export class MicroSDeck {
private abortController = new AbortController();

Expand All @@ -28,11 +63,20 @@ export class MicroSDeck {

public eventBus = new EventTarget() as unknown as EventBus<EventType, Event | CustomEvent<BackendEvent>>;

public get Version() {
return version;
}

private enabled: boolean = false;
public get Enabled() {
return this.enabled;
}
private version: string | undefined;

private backendVersion: string | undefined;
public get BackendVersion() {
return this.backendVersion;
}

private currentCardAndGames: CardAndGames | undefined;
public get CurrentCardAndGames() {
return this.currentCardAndGames;
Expand Down Expand Up @@ -62,18 +106,23 @@ export class MicroSDeck {
destruct() {
this.logger?.Debug("Deconstruct Called");
if (this.isDestructed) return;
this.isDestructed = true;
this.logger?.Log("Deinitializing MicroSDeck");
this.abortController.abort("destruct");
this.isDestructed = true;
}

async fetch() {
this.enabled = await fetchHealth(this.fetchProps);
this.version = await fetchVersion(this.fetchProps);
if(!await fetchHealth(this.fetchProps)) {
this.enabled = false;
this.logger?.Log("Fetch failed. Server is offline...");
return;
}
this.backendVersion = await fetchVersion(this.fetchProps);

await this.fetchCurrent();
await this.fetchCardsAndGames();
this.eventBus.dispatchEvent(new Event("update"));
this.enabled = true;
}

async fetchCurrent() {
Expand All @@ -86,7 +135,7 @@ export class MicroSDeck {
getProps() {
return {
enabled: this.enabled,
version: this.version,
version: this.backendVersion,
cardsAndGames: this.cardsAndGames,
currentCardAndGames: this.currentCardAndGames
}
Expand Down
4 changes: 3 additions & 1 deletion lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"jsx": "react",
"jsxFactory": "window.SP_REACT.createElement",
"jsxFragmentFactory": "React.Fragment",
"allowJs": true,
"declaration": true,
"moduleResolution": "Node",
"noUnusedLocals": true,
Expand All @@ -18,7 +19,8 @@
"ignoreDeprecations": "5.0",
"suppressImplicitAnyIndexErrors": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["src"],
"exclude": ["node_modules"]
Expand Down
Loading

0 comments on commit f3360f4

Please sign in to comment.