Skip to content

Commit 31cfe3d

Browse files
committed
Properly convert release metadata from json
1 parent c8986b2 commit 31cfe3d

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/hlsBinaries.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ async function toolInstalled(
726726
*
727727
* consult [ghcup metadata repo](https://github.com/haskell/ghcup-metadata/) for details.
728728
*/
729-
export type ReleaseMetadata = Map<string, Map<string, Map<string, string[]>>>;
729+
export type ReleaseMetadata = Map<string, Map<string, Map<string, string[]>>>;
730730

731731
/**
732732
* Compute Map of supported HLS versions for this platform.
@@ -829,13 +829,29 @@ async function getReleaseMetadata(
829829

830830
const offlineCache = path.join(storagePath, 'ghcupReleases.cache.json');
831831

832+
/**
833+
* Convert a json value to ReleaseMetadata.
834+
* Assumes the json is well-formed and a valid Release-Metadata.
835+
* @param obj Release Metadata without any typing information but well-formed.
836+
* @returns Typed ReleaseMetadata.
837+
*/
838+
const objectToMetadata = (obj: any): ReleaseMetadata => {
839+
const hlsMetaEntries = Object.entries(obj).map(([hlsVersion, archMap]) => {
840+
const archMetaEntries = Object.entries(archMap as any).map(([arch, supportedGhcVersionsPerOs]) => {
841+
return [arch, new Map(Object.entries(supportedGhcVersionsPerOs as any))] as [string, Map<string, string[]>];
842+
});
843+
return [hlsVersion, new Map(archMetaEntries)] as [string, Map<string, Map<string, string[]>>];
844+
});
845+
return new Map(hlsMetaEntries);
846+
};
847+
832848
async function readCachedReleaseData(): Promise<ReleaseMetadata | null> {
833849
try {
834850
logger.info(`Reading cached release data at ${offlineCache}`);
835851
const cachedInfo = await promisify(fs.readFile)(offlineCache, { encoding: 'utf-8' });
836852
// export type ReleaseMetadata = Map<string, Map<string, Map<string, string[]>>>;
837-
const value: ReleaseMetadata = JSON.parse(cachedInfo);
838-
return value;
853+
const value: any = JSON.parse(cachedInfo);
854+
return objectToMetadata(value);
839855
} catch (err: any) {
840856
// If file doesn't exist, return null, otherwise consider it a failure
841857
if (err.code === 'ENOENT') {
@@ -852,7 +868,7 @@ async function getReleaseMetadata(
852868

853869
// Cache the latest successfully fetched release information
854870
await promisify(fs.writeFile)(offlineCache, JSON.stringify(releaseInfoParsed), { encoding: 'utf-8' });
855-
return releaseInfoParsed;
871+
return objectToMetadata(releaseInfoParsed);
856872
} catch (githubError: any) {
857873
// Attempt to read from the latest cached file
858874
try {

0 commit comments

Comments
 (0)