Factor out helm chart normalization

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2021-10-18 10:23:54 -04:00
1 parent 9551e0c926
commit 8fd0b6f4c1
2 files changed
+31 -30

No files matched your search

+1 -1
View File
@@ -83,5 +83,5 @@ export function sortCharts(charts: RawHelmChart[]) {
return chartsWithVersion return chartsWithVersion
.sort(sortCompareChartVersions) .sort(sortCompareChartVersions)
.map(chart => (delete chart.__version, chart)); .map(chart => (delete chart.__version, chart as RawHelmChart));
} }
+30 -29
View File
@@ -95,37 +95,11 @@ export class HelmChartManager {
const cacheFileStats = await fs.promises.stat(this.repo.cacheFilePath); const cacheFileStats = await fs.promises.stat(this.repo.cacheFilePath);
const data = yaml.load(cacheFile) as string | number | HelmCacheFile; const data = yaml.load(cacheFile) as string | number | HelmCacheFile;
if (typeof data !== "object" || !data) { if (!data || typeof data !== "object" || typeof data.entries !== "object") {
return; throw Object.assign(new TypeError("Helm Cache file does not parse correctly"), { file: this.repo.cacheFilePath, data });
} }
const { entries } = data; const normalized = normalizeHelmCharts(this.repo.name, data.entries);
/**
* Do some initial preprocessing on the data, so as to avoid needing to do it later
* 1. Set the repo name
* 2. Normalize the created date
* 3. Filter out deprecated items
*/
const normalized = Object.fromEntries(
iter.filter(
iter.map(
Object.entries(entries),
([name, charts]) => [
name,
sortCharts(
charts.map(chart => ({
...chart,
created: Date.parse(chart.created).toString(),
repo: this.repo.name,
})),
),
] as const
),
([, charts]) => !charts.every(chart => chart.deprecated),
)
);
HelmChartManager.#cache.set(this.repo.name, { HelmChartManager.#cache.set(this.repo.name, {
data: v8.serialize(normalized), data: v8.serialize(normalized),
@@ -148,3 +122,30 @@ export class HelmChartManager {
return v8.deserialize(HelmChartManager.#cache.get(this.repo.name).data); return v8.deserialize(HelmChartManager.#cache.get(this.repo.name).data);
} }
} }
/**
* Do some initial preprocessing on the data, so as to avoid needing to do it later
* 1. Set the repo name
* 2. Normalize the created date
* 3. Filter out charts that only have deprecated entries
*/
function normalizeHelmCharts(repoName: string, entries: RepoHelmChartList): RepoHelmChartList {
return Object.fromEntries(
iter.filter(
iter.map(
Object.entries(entries),
([name, charts]) => [
name,
sortCharts(
charts.map(chart => ({
...chart,
created: Date.parse(chart.created).toString(),
repo: repoName,
})),
),
] as const
),
([, charts]) => !charts.every(chart => chart.deprecated),
)
);
}