mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-22 00:11:02 -05:00
Made an interface for custom Forge payload
This commit is contained in:
parent
89873779da
commit
47e35aa836
1 changed files with 123 additions and 0 deletions
123
src/loaders/forge/forge-metadata-custom-payload.ts
Normal file
123
src/loaders/forge/forge-metadata-custom-payload.ts
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
import { ACTION_NAME } from "@/action";
|
||||||
|
import { Dependency, createDependency } from "@/dependencies";
|
||||||
|
import { LoaderType } from "@/loaders/loader-type";
|
||||||
|
import { PlatformType } from "@/platforms";
|
||||||
|
import { PartialRecord } from "@/utils/types";
|
||||||
|
import { deprecate } from "node:util";
|
||||||
|
import { RawForgeMetadata } from "./raw-forge-metadata";
|
||||||
|
import { asString } from "@/utils/string-utils";
|
||||||
|
|
||||||
|
// TODO: Remove the deprecated stuff in v4.0.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom payload for Forge metadata.
|
||||||
|
*/
|
||||||
|
export type ForgeMetadataCustomPayload = {
|
||||||
|
/**
|
||||||
|
* A list of supported mod loaders.
|
||||||
|
*/
|
||||||
|
loaders?: string[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of mod dependencies.
|
||||||
|
*/
|
||||||
|
dependencies?: string[];
|
||||||
|
}
|
||||||
|
& PartialRecord<PlatformType, string>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the custom payload from the Forge metadata.
|
||||||
|
*
|
||||||
|
* @param metadata - The raw Forge metadata.
|
||||||
|
*
|
||||||
|
* @returns The custom payload attached to the given metadata.
|
||||||
|
*/
|
||||||
|
export function getForgeMetadataCustomPayload(metadata: RawForgeMetadata): ForgeMetadataCustomPayload {
|
||||||
|
return containsLegacyCustomPayloadDefinition(metadata)
|
||||||
|
? getLegacyForgeMetadataCustomPayload(metadata)
|
||||||
|
: (metadata?.[ACTION_NAME] || {});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the metadata contains a legacy custom payload definition.
|
||||||
|
*
|
||||||
|
* @param metadata - The raw Forge metadata.
|
||||||
|
*
|
||||||
|
* @returns A boolean indicating if the legacy custom payload definition is present.
|
||||||
|
*/
|
||||||
|
function containsLegacyCustomPayloadDefinition(metadata: RawForgeMetadata): boolean {
|
||||||
|
return !!metadata?.custom?.[ACTION_NAME] || !!metadata?.custom?.projects || !!metadata?.projects;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the legacy custom payload from the Forge metadata.
|
||||||
|
*
|
||||||
|
* @param metadata - The raw Forge metadata.
|
||||||
|
*
|
||||||
|
* @returns The custom payload object.
|
||||||
|
*/
|
||||||
|
function _getLegacyForgeMetadataCustomPayload(metadata: RawForgeMetadata): ForgeMetadataCustomPayload {
|
||||||
|
const legacyPayload = { ...metadata?.projects, ...metadata?.custom?.projects, ...metadata?.custom?.[ACTION_NAME] };
|
||||||
|
const basePayload = metadata?.[ACTION_NAME];
|
||||||
|
return { ...legacyPayload, ...basePayload };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the legacy custom payload from the Forge metadata.
|
||||||
|
*
|
||||||
|
* @param metadata - The raw Forge metadata.
|
||||||
|
*
|
||||||
|
* @returns The custom payload object.
|
||||||
|
*
|
||||||
|
* @deprecated
|
||||||
|
*
|
||||||
|
* Use top-level `mc-publish` field in your mod metadata.
|
||||||
|
*/
|
||||||
|
const getLegacyForgeMetadataCustomPayload = deprecate(
|
||||||
|
_getLegacyForgeMetadataCustomPayload,
|
||||||
|
"Use top-level `mc-publish` field in your mods.toml.",
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of default mod loaders associated with the Forge loader.
|
||||||
|
*/
|
||||||
|
const DEFAULT_LOADERS = [LoaderType.FORGE] as const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an array of supported mod loaders from the custom payload attached to the Forge metadata.
|
||||||
|
*
|
||||||
|
* @param payload - The custom payload object.
|
||||||
|
*
|
||||||
|
* @returns An array of supported mod loaders.
|
||||||
|
*/
|
||||||
|
export function getLoadersFromForgeMetadataCustomPayload(payload: ForgeMetadataCustomPayload): string[] {
|
||||||
|
return payload?.loaders || [...DEFAULT_LOADERS];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the dependencies from the custom payload attached to the Forge metadata.
|
||||||
|
*
|
||||||
|
* @param payload - The custom payload object.
|
||||||
|
*
|
||||||
|
* @returns An array of dependencies included into the custom payload.
|
||||||
|
*/
|
||||||
|
export function getDependenciesFromForgeMetadataCustomPayload(payload: ForgeMetadataCustomPayload): Dependency[] {
|
||||||
|
if (!Array.isArray(payload?.dependencies)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return payload?.dependencies?.map(x => createDependency(x)).filter(x => x) || [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the project ID from the custom payload attached to the Forge metadata based on the given platform.
|
||||||
|
*
|
||||||
|
* @param payload - The custom payload object.
|
||||||
|
* @param platform - The platform for which the project ID is required.
|
||||||
|
*
|
||||||
|
* @returns The project ID as a string, or `undefined` if not found.
|
||||||
|
*/
|
||||||
|
export function getProjectIdFromForgeMetadataCustomPayload(payload: ForgeMetadataCustomPayload, platform: PlatformType): string | undefined {
|
||||||
|
const id = payload?.[platform];
|
||||||
|
return id ? asString(id) : undefined;
|
||||||
|
}
|
Loading…
Reference in a new issue