From 8fd0b6f4c111ab0e5d624ebac46dea546e0d49eb Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 18 Oct 2021 09:52:35 -0400 Subject: [PATCH] Factor out helm chart normalization Signed-off-by: Sebastian Malton --- src/common/utils/sort-compare.ts | 2 +- src/main/helm/helm-chart-manager.ts | 59 +++++++++++++++-------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/common/utils/sort-compare.ts b/src/common/utils/sort-compare.ts index 041a239bb5..d8292aab83 100644 --- a/src/common/utils/sort-compare.ts +++ b/src/common/utils/sort-compare.ts @@ -83,5 +83,5 @@ export function sortCharts(charts: RawHelmChart[]) { return chartsWithVersion .sort(sortCompareChartVersions) - .map(chart => (delete chart.__version, chart)); + .map(chart => (delete chart.__version, chart as RawHelmChart)); } diff --git a/src/main/helm/helm-chart-manager.ts b/src/main/helm/helm-chart-manager.ts index 2a3547e145..b7afb6ff3d 100644 --- a/src/main/helm/helm-chart-manager.ts +++ b/src/main/helm/helm-chart-manager.ts @@ -95,37 +95,11 @@ export class HelmChartManager { const cacheFileStats = await fs.promises.stat(this.repo.cacheFilePath); const data = yaml.load(cacheFile) as string | number | HelmCacheFile; - if (typeof data !== "object" || !data) { - return; + if (!data || typeof data !== "object" || typeof data.entries !== "object") { + throw Object.assign(new TypeError("Helm Cache file does not parse correctly"), { file: this.repo.cacheFilePath, data }); } - const { entries } = 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 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), - ) - ); + const normalized = normalizeHelmCharts(this.repo.name, data.entries); HelmChartManager.#cache.set(this.repo.name, { data: v8.serialize(normalized), @@ -148,3 +122,30 @@ export class HelmChartManager { 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), + ) + ); +}