fix: drop cache for new majors (#1419)

currently when new majors are introduced we need to shift around branches / older versions need to be rebuild, so we invalidate the cache here when a new major is introduced

Reggi committed Dec 18, 2024 at 12:01 UTC 6fcf10c1af627e48a564454baf2202fe87331b1b
2 files changed +19
cli/lib/build.js
+6
@@ -113,6 +113,12 @@ const main = async ({loglevel, releases: rawReleases, useCurrent, navPath, conte
113 }
114 })
115
116 + /**
117 + * this voids the cache when a new version is added to release.json / not in the cli-cache.json
118 + * this is done so that the previous major versions nav can be reset to legacy and pages can be droped from its variant
119 + */
120 + cache?.voidOnNewKey(releases.map(v => v.id))
121 +
122 const updates = await Promise.all(
123 releases.map(r => extractRelease(r, {cache, contentPath, baseNav: navData, prerelease})),
124 ).then(r => r.filter(Boolean))
cli/lib/cache.js
+13
@@ -2,6 +2,8 @@ const fs = require('fs/promises')
2
3 /** cache npm cli version shas to NOT pull down changes we already have */
4 class CacheVersionSha {
5 + shouldVoid = false
6 +
7 constructor(cache, path) {
8 this.cache = cache
9 this.path = path
@@ -11,6 +13,16 @@ class CacheVersionSha {
13 return new CacheVersionSha(JSON.parse(await fs.readFile(path, 'utf-8')), path)
14 }
15
16 + get keys() {
17 + return Object.keys(this.cache)
18 + }
19 +
20 + voidOnNewKey(keys) {
21 + if (keys.length !== this.keys.length || !keys.every(key => this.keys.includes(key))) {
22 + this.shouldVoid = true
23 + }
24 + }
25 +
26 async save() {
27 const sortedCache = {}
28 Object.keys(this.cache)
@@ -33,6 +45,7 @@ class CacheVersionSha {
45 }
46
47 same(id, value) {
48 + if (this.shouldVoid) return false
49 return this.cache[id] === value
50 }
51 }