@@ -6,10 +6,24 @@ import { Env, getEnv } from "../environment";
66
77import 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