Skip to content

Commit

Permalink
Add semver gte wrapper and revert handling of beta versions again
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Nov 4, 2020
1 parent 171a592 commit 12ff04e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

"comma-dangle": ["error", "always-multiline"],
"dot-notation": "error",
"eqeqeq": "error",
"eqeqeq": ["error", "always", {"null": "ignore"}],
"curly": ["error", "all"],
"brace-style": ["error"],

Expand Down
32 changes: 19 additions & 13 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { EventEmitter } from "events";
import * as hapNodeJs from "hap-nodejs";
import { Controller, Service } from "hap-nodejs";
import { getBetaRevision, getServerVersion } from "./version";
import getVersion from "./version";
import { PlatformAccessory } from "./platformAccessory";
import { User } from "./user";
import { Logger, Logging } from "./logger";
import { AccessoryConfig, PlatformConfig } from "./server";
import { PluginManager } from "./pluginManager";
import semver from "semver";

const log = Logger.internal;

Expand Down Expand Up @@ -173,18 +174,8 @@ export interface API {
readonly version: number;
/**
* The current homebridge semver version.
*
* Note: for beta versions like "1.3.0-beta.4" this property will always return the full release string "1.3.0".
* To check if current release is a beta version refer to {@link betaRevision}.
*/
readonly serverVersion: string;
/**
* If current running version is a beta version this property will reflect the revision of that specific release.
* For example for the beta version 1.3.0-beta.4 {@link serverVersion} will return the current version "1.3.0"
* and the property {@link betaRevision} will return the revision number 4.
* If the current running version is a stable release the property will be null.
*/
readonly betaRevision: null | number

// ------------------ LEGACY EXPORTS FOR PRE TYPESCRIPT ------------------
readonly user: typeof User;
Expand Down Expand Up @@ -242,8 +233,7 @@ export declare interface HomebridgeAPI {
export class HomebridgeAPI extends EventEmitter implements API {

public readonly version = 2.7; // homebridge API version
public readonly serverVersion = getServerVersion(); // homebridge node module version
public readonly betaRevision: null | number = getBetaRevision();
public readonly serverVersion = getVersion(); // homebridge node module version

// ------------------ LEGACY EXPORTS FOR PRE TYPESCRIPT ------------------
readonly user = User;
Expand All @@ -256,6 +246,22 @@ export class HomebridgeAPI extends EventEmitter implements API {
super();
}

/**
* Returns true if the current running homebridge version is greater or equal to the
* passed version string.
*
* Example:
* We assume the homebridge version 1.3.0-beta.12 and the following example calls below
* versionGreaterOrEqual("1.2.0"); will return true
* versionGreaterOrEqual("1.3.0"); will return false (the RELEASE version 1.3.0 is bigger than the BETA version 1.3.0-beta.12)
* versionGreaterOrEqual("1.3.0-beta.8); will return true
*
* @param version
*/
public versionGreaterOrEqual(version: string): boolean {
return semver.gte(this.serverVersion, version);
}

public static isDynamicPlatformPlugin(platformPlugin: PlatformPlugin): platformPlugin is DynamicPlatformPlugin {
return "configureAccessory" in platformPlugin;
}
Expand Down
31 changes: 0 additions & 31 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from "fs";
import path from "path";
import semver from "semver";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function loadPackageJson(): any {
Expand All @@ -12,36 +11,6 @@ export default function getVersion(): string {
return loadPackageJson().version;
}

/**
* Returns the versions string set to the API object.
*/
export function getServerVersion(): string {
const version = getVersion();

const prerelease = semver.prerelease(version);
if (prerelease && prerelease[0] === "beta") {
return semver.inc(version, "patch")!; // 1.y.x-beta.z turns into 1.y.x
}

return version;
}

/**
* If current version is a beta release this method returns the revision.
* For example for the version 1.3.0-beta.4 the method returns the number 4.
* For a non beta release lik 1.3.0 it will return null.
*/
export function getBetaRevision(): number | null {
const version = getVersion();

const prerelease = semver.prerelease(version);
if (prerelease && prerelease[0] === "beta") {
return parseInt(prerelease[1]);
}

return null;
}

export function getRequiredNodeVersion(): string {
return loadPackageJson().engines.node;
}

0 comments on commit 12ff04e

Please sign in to comment.