Skip to content

Commit b222c3a

Browse files
committed
Generalize file cache data structure
1 parent 11569df commit b222c3a

3 files changed

Lines changed: 30 additions & 11 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/cli/output-cache.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test.serial(
2020
cacheFile,
2121
JSON.stringify({
2222
cmd: "/path/to/codeql",
23-
version: { version: "2.20.0" },
23+
entries: { version: { version: "2.20.0" } },
2424
}),
2525
"utf8",
2626
);

‎src/cli/output-cache.ts‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,24 @@ import { Env, getEnv } from "../environment";
66

77
import type { VersionInfo } from "./types";
88

9+
/**
10+
* The keys of the command cache. Each key corresponds to a command whose output we cache.
11+
*/
12+
enum CommandCacheKey {
13+
Version = "version",
14+
}
15+
16+
/**
17+
* The mapping of CLI commands to the types of the output of each command that we cache.
18+
*/
19+
type CommandCacheKeyOutputMap = {
20+
[CommandCacheKey.Version]: VersionInfo;
21+
};
22+
923
/** The persisted version together with the CLI path it was obtained from. */
10-
interface PersistedVersionInfo {
24+
interface PersistedVersionInfo<K extends CommandCacheKey> {
1125
cmd: string;
12-
version: VersionInfo;
26+
entries: Map<K, CommandCacheKeyOutputMap[K]>;
1327
}
1428

1529
/**
@@ -60,7 +74,7 @@ export function cacheCodeQlVersion(
6074
// doesn't pick up a stale version.
6175
fs.writeFileSync(
6276
getCommandCacheFilePath(env),
63-
JSON.stringify({ cmd, version }),
77+
JSON.stringify({ cmd, entries: { [CommandCacheKey.Version]: version } }),
6478
"utf8",
6579
);
6680
}
@@ -100,7 +114,7 @@ export function getCachedCodeQlVersion(
100114
}
101115
// Memoize the parsed value so that subsequent calls in this process don't
102116
// re-parse the environment variable.
103-
cachedCodeQlVersion = persisted.version;
117+
cachedCodeQlVersion = persisted.entries[CommandCacheKey.Version];
104118
return cachedCodeQlVersion;
105119
}
106120

@@ -126,12 +140,17 @@ function isVersionInfo(x: unknown): x is VersionInfo {
126140
* Determines whether a value is a `PersistedVersionInfo` object.
127141
* @param x The value to test
128142
*/
129-
function isPersistedVersionInfo(x: unknown): x is PersistedVersionInfo {
130-
const candidate = x as Partial<PersistedVersionInfo> | null;
143+
function isPersistedVersionInfo(
144+
x: unknown,
145+
): x is PersistedVersionInfo<CommandCacheKey.Version> {
146+
const candidate = x as Partial<
147+
PersistedVersionInfo<CommandCacheKey.Version>
148+
> | null;
131149
return (
132150
typeof candidate === "object" &&
133151
candidate !== null &&
134152
typeof candidate.cmd === "string" &&
135-
isVersionInfo(candidate.version)
153+
candidate.entries !== undefined &&
154+
isVersionInfo(candidate.entries[CommandCacheKey.Version])
136155
);
137156
}

0 commit comments

Comments
 (0)