|
| 1 | +import process from "node:process"; |
| 2 | +import semver, {SemVer} from "semver"; |
| 3 | +import data from "./data.json" with {type: "json"}; |
| 4 | + |
| 5 | +/** |
| 6 | + * Represents a GitHub release. |
| 7 | + */ |
| 8 | +export class Release { |
| 9 | + |
| 10 | + /** |
| 11 | + * The latest release. |
| 12 | + */ |
| 13 | + static get latest(): Release|null { |
| 14 | + return this.#data.at(0) ?? null; |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * The base URL of the releases. |
| 19 | + */ |
| 20 | + static readonly #baseUrl: URL = new URL("https://github.com/HaxeFoundation/hashlink/"); |
| 21 | + |
| 22 | + /** |
| 23 | + * The list of all releases. |
| 24 | + */ |
| 25 | + static readonly #data: Release[] = data.map(release => new this(release.version, release.assets as ReleaseAsset[])); |
| 26 | + |
| 27 | + /** |
| 28 | + * The associated assets. |
| 29 | + */ |
| 30 | + assets: ReleaseAsset[]; |
| 31 | + |
| 32 | + /** |
| 33 | + * The version number. |
| 34 | + */ |
| 35 | + version: string; |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates a new release. |
| 39 | + * @param version The version number. |
| 40 | + * @param assets The associated assets. |
| 41 | + */ |
| 42 | + constructor(version: string, assets: ReleaseAsset[] = []) { |
| 43 | + this.assets = assets; |
| 44 | + this.version = version; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Value indicating whether this release exists. |
| 49 | + */ |
| 50 | + get exists(): boolean { |
| 51 | + return Release.#data.some(release => release.version == this.version); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Value indicating whether this release is provided as source code. |
| 56 | + */ |
| 57 | + get isSource(): boolean { |
| 58 | + return !this.getAsset(process.platform); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * The associated Git tag. |
| 63 | + */ |
| 64 | + get tag(): string { |
| 65 | + const {major, minor, patch} = new SemVer(this.version); |
| 66 | + return patch > 0 ? `${major}.${minor}.${patch}` : `${major}.${minor}`; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * The download URL. |
| 71 | + */ |
| 72 | + get url(): URL { |
| 73 | + const asset = this.getAsset(process.platform); |
| 74 | + return new URL(asset ? `releases/download/${this.tag}/${asset.file}` : `archive/refs/tags/${this.tag}.zip`, Release.#baseUrl); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Finds a release that matches the specified version constraint. |
| 79 | + * @param constraint The version constraint. |
| 80 | + * @returns The release corresponding to the specified constraint, or `null` if not found. |
| 81 | + */ |
| 82 | + static find(constraint: string): Release|null { |
| 83 | + return this.#data.find(release => semver.satisfies(release.version, constraint)) ?? null; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Gets the release corresponding to the specified version. |
| 88 | + * @param version The version number of a release. |
| 89 | + * @returns The release corresponding to the specified version, or `null` if not found. |
| 90 | + */ |
| 91 | + static get(version: string): Release|null { |
| 92 | + return this.#data.find(release => release.version == version) ?? null; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Gets the asset corresponding to the specified platform. |
| 97 | + * @param platform The target platform. |
| 98 | + * @returns The asset corresponding to the specified platform, or `null` if not found. |
| 99 | + */ |
| 100 | + getAsset(platform: NodeJS.Platform): ReleaseAsset|null { |
| 101 | + return this.assets.find(asset => asset.platform == platform) ?? null; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Represents an asset of a GitHub release. |
| 107 | + */ |
| 108 | +export type ReleaseAsset = { |
| 109 | + |
| 110 | + /** |
| 111 | + * The target file. |
| 112 | + */ |
| 113 | + file: string; |
| 114 | + |
| 115 | + /** |
| 116 | + * The target platform. |
| 117 | + */ |
| 118 | + platform: NodeJS.Platform; |
| 119 | +} |
0 commit comments