• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

streetsidesoftware / cspell / 16363970229

18 Jul 2025 06:37AM UTC coverage: 92.434%. First build
16363970229

Pull #7636

github

web-flow
Merge 17b9abb8d into c0957a82e
Pull Request #7636: fix: Remove `flat-cache` dependency

13114 of 15508 branches covered (84.56%)

90 of 110 new or added lines in 2 files covered. (81.82%)

16114 of 17433 relevant lines covered (92.43%)

29585.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.3
/packages/cspell/src/util/cache/file-entry-cache/flatCache.ts
1
import fs from 'node:fs/promises';
2
import path from 'node:path';
3

4
import { parse, stringify } from 'flatted';
5

6
export class FlatCache<T> {
7
    #cache: Map<string, T>;
8

9
    constructor(readonly cacheFilename: string) {
30✔
10
        this.#cache = new Map<string, T>();
30✔
11
    }
12

13
    keys(): MapIterator<string> {
14
        return this.#cache.keys();
59✔
15
    }
16

17
    set(key: string, value: T): this {
18
        this.#cache.set(key, value);
42✔
19
        return this;
42✔
20
    }
21

22
    removeKey(key: string): void {
NEW
23
        this.#cache.delete(key);
×
24
    }
25

26
    get(key: string): T | undefined {
27
        return this.#cache.get(key);
92✔
28
    }
29

30
    async load(ifFound: boolean = true): Promise<this> {
30✔
31
        this.#cache.clear();
30✔
32
        try {
30✔
33
            const content = await fs.readFile(this.cacheFilename, 'utf8');
30✔
34
            this.#cache = new Map<string, T>(Object.entries(parse(content)));
18✔
35
        } catch (error) {
36
            if (!ifFound) {
12!
NEW
37
                throw error;
×
38
            }
39
        }
40
        return this;
30✔
41
    }
42

43
    async save(): Promise<void> {
44
        const dir = path.dirname(this.cacheFilename);
29✔
45
        await fs.mkdir(dir, { recursive: true });
29✔
46
        const content = stringify(Object.fromEntries(this.#cache.entries()));
29✔
47
        await fs.writeFile(this.cacheFilename, content, 'utf8');
29✔
48
    }
49

50
    /**
51
     * Clear the cache and remove the cache file from disk.
52
     */
53
    async destroy(): Promise<void> {
54
        this.#cache.clear();
4✔
55
        try {
4✔
56
            await fs.unlink(this.cacheFilename);
4✔
57
        } catch {
58
            // Ignore errors when deleting the cache file.
59
            // It may not exist or may not be writable.
60
        }
61
    }
62
}
63

64
export function loadCacheFile<T>(file: string): Promise<FlatCache<T>> {
65
    const cache = new FlatCache<T>(file);
30✔
66
    return cache.load();
30✔
67
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc