mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 07:24:53 +09:00
Migrate the usage of CommonJS specific code and imports to ESM. Roughly summarised this: - Changes all require() calls to import statement or expressions. - Changes usage of module.exports to export statements. - Replaces require.resolve with import.meta.resolve. - Replaces conditional imports with conditional subpath imports. - Replaces module === require.main with import.meta.main. - Adds the full filename with extention to all relative imports. - Changes all usage of __dirname and __filename to their import.meta equivalents. - Sets "type": "module" in package.json files. - Removes usage of "use strict". - Adds .ts an an alias of .js for the webpack build. Due to differences in how require and import resolves files the plugin entrypoints will have to include the filename and extension after this migration in order to work.
63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { Static, Type } from "@sinclair/typebox";
|
|
import { FullVersion, FullVersionSchema } from "../data/version.js";
|
|
|
|
const LATEST_RELEASES_URL = "https://factorio.com/api/latest-releases";
|
|
|
|
/**
|
|
* The set of builds (e.g. alpha, demo, headless, expansion) and their version
|
|
* for a single release channel.
|
|
*/
|
|
export const LatestReleaseBuildsSchema = Type.Record(Type.String(), FullVersionSchema);
|
|
|
|
/**
|
|
* The latest released version of each build, keyed by release channel name
|
|
* (e.g. "stable" and "experimental").
|
|
*/
|
|
export const LatestReleasesSchema = Type.Record(Type.String(), LatestReleaseBuildsSchema);
|
|
|
|
export type LatestReleases = Static<typeof LatestReleasesSchema>
|
|
|
|
/**
|
|
* Fetch the latest stable and experimental factorio releases
|
|
*
|
|
* @returns The latest version of each build keyed by release channel
|
|
* @throws Network errors and non-ok status responses
|
|
*/
|
|
export async function fetchLatestReleases(): Promise<LatestReleases> {
|
|
let res;
|
|
|
|
try {
|
|
res = await fetch(LATEST_RELEASES_URL);
|
|
} catch (err) {
|
|
throw new Error(`Failed to fetch Factorio releases from ${LATEST_RELEASES_URL}: ${err}`);
|
|
}
|
|
|
|
if (!res.ok) {
|
|
throw new Error(
|
|
`Failed to fetch Factorio releases from ${LATEST_RELEASES_URL}: HTTP ${res.status} ${res.statusText}`
|
|
);
|
|
}
|
|
|
|
return await res.json() as LatestReleases;
|
|
}
|
|
|
|
/**
|
|
* Resolve a release channel to a concrete factorio version
|
|
*
|
|
* @param releases Releases data as returned by {@link fetchLatestReleases}
|
|
* @param channel Release channel name, e.g. "stable" or "experimental"
|
|
* @param build Build to read the version of, defaults to the headless build
|
|
* @returns The version for the channel, or undefined if it cannot be resolved
|
|
*/
|
|
export function resolveReleaseChannel(
|
|
releases: LatestReleases,
|
|
channel: string,
|
|
build = "headless",
|
|
): FullVersion | undefined {
|
|
const builds = releases[channel];
|
|
if (!builds) {
|
|
return undefined;
|
|
}
|
|
return (builds[build] ?? Object.values(builds)[0]) as FullVersion | undefined;
|
|
}
|