forked from backstage/backstage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request backstage#27215 from backstage/rugvip/add-cache
cli: refactor success cache to be additive
- Loading branch information
Showing
4 changed files
with
111 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@backstage/cli': patch | ||
--- | ||
|
||
The `--successCache` option for the `repo test` and `repo lint` commands now use an additive store that keeps old entries around for a week before they are cleaned up automatically. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2024 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import fs from 'fs-extra'; | ||
import { resolve as resolvePath } from 'node:path'; | ||
|
||
const DEFAULT_CACHE_BASE_PATH = 'node_modules/.cache/backstage-cli'; | ||
|
||
const CACHE_MAX_AGE_MS = 7 * 24 * 3600_000; | ||
|
||
export class SuccessCache { | ||
readonly #path: string; | ||
|
||
constructor(name: string, basePath?: string) { | ||
this.#path = resolvePath(basePath ?? DEFAULT_CACHE_BASE_PATH, name); | ||
} | ||
|
||
async read(): Promise<Set<string>> { | ||
try { | ||
const stat = await fs.stat(this.#path); | ||
if (!stat.isDirectory()) { | ||
await fs.rm(this.#path); | ||
return new Set(); | ||
} | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { | ||
return new Set(); | ||
} | ||
throw error; | ||
} | ||
|
||
const items = await fs.readdir(this.#path); | ||
|
||
const returned = new Set<string>(); | ||
const removed = new Set<string>(); | ||
|
||
const now = Date.now(); | ||
|
||
for (const item of items) { | ||
const split = item.split('_'); | ||
if (split.length !== 2) { | ||
removed.add(item); | ||
continue; | ||
} | ||
const createdAt = parseInt(split[0], 10); | ||
if (Number.isNaN(createdAt) || now - createdAt > CACHE_MAX_AGE_MS) { | ||
removed.add(item); | ||
} else { | ||
returned.add(split[1]); | ||
} | ||
} | ||
|
||
for (const item of removed) { | ||
await fs.unlink(resolvePath(this.#path, item)); | ||
} | ||
|
||
return returned; | ||
} | ||
|
||
async write(newEntries: Iterable<string>): Promise<void> { | ||
const now = Date.now(); | ||
|
||
await fs.ensureDir(this.#path); | ||
|
||
const existingItems = await fs.readdir(this.#path); | ||
|
||
const empty = Buffer.alloc(0); | ||
for (const key of newEntries) { | ||
// Remove any existing items with the key we're about to add | ||
const trimmedItems = existingItems.filter(item => | ||
item.endsWith(`_${key}`), | ||
); | ||
for (const trimmedItem of trimmedItems) { | ||
await fs.unlink(resolvePath(this.#path, trimmedItem)); | ||
} | ||
|
||
await fs.writeFile(resolvePath(this.#path, `${now}_${key}`), empty); | ||
} | ||
} | ||
} |