Refactor isVersionInfo with json module

This commit is contained in:
Mario Campos
2026-08-14 11:28:36 -05:00
parent d7d901a925
commit c8ba2d2a05
3 changed files with 46 additions and 25 deletions

26
lib/entry-points.js generated
View File

@@ -146727,6 +146727,23 @@ function wrapApiConfigurationError(e) {
// src/cli/output-cache.ts
var fs3 = __toESM(require("fs"));
var import_path = __toESM(require("path"));
// src/cli/types.ts
var versionInfoBaseSchema = {
version: string,
features: optional(object({})),
/**
* The overlay version helps deal with backward incompatible changes for
* overlay analysis. When a precompiled query pack reports the same overlay
* version as the CodeQL CLI, we can use the CodeQL CLI to perform overlay
* analysis with that pack. Otherwise, if the overlay versions are different,
* or if either the pack or the CLI does not report an overlay version,
* we need to revert to non-overlay analysis.
*/
overlayVersion: optional(number)
};
// src/cli/output-cache.ts
var COMMAND_CACHE_FILENAME = "codeql-action-command-cache.json";
var cachedCodeQlVersion = void 0;
function getCommandCacheFilePath(env) {
@@ -146774,14 +146791,7 @@ function getCachedCodeQlVersion(logger, env, cmd) {
return cachedCodeQlVersion;
}
function isVersionInfo(x) {
return isObject(x) && validateSchema(
{
version: string,
features: optional(object({})),
overlayVersion: optional(number)
},
x
);
return isObject(x) && validateSchema(versionInfoBaseSchema, x);
}
function isOutputCache(x) {
return isObject(x) && validateSchema(

View File

@@ -6,7 +6,7 @@ import { Env } from "../environment";
import * as json from "../json";
import { Logger } from "../logging";
import type { VersionInfo } from "./types";
import { VersionInfo, versionInfoBaseSchema } from "./types";
/**
* The keys of the command cache. Each key corresponds to a command whose output we cache.
@@ -128,17 +128,7 @@ export function getCachedCodeQlVersion(
* @param x The value to test
*/
function isVersionInfo(x: unknown): x is VersionInfo {
return (
json.isObject(x) &&
json.validateSchema(
{
version: json.string,
features: json.optional(json.object({})),
overlayVersion: json.optional(json.number),
} as const satisfies json.Schema,
x,
)
);
return json.isObject(x) && json.validateSchema(versionInfoBaseSchema, x);
}
/**

View File

@@ -1,6 +1,11 @@
export interface VersionInfo {
version: string;
features?: { [name: string]: boolean };
import * as json from "../json";
/**
* The JSON schema of the expected output of the `codeql version` command.
*/
export const versionInfoBaseSchema = {
version: json.string,
features: json.optional(json.object({})),
/**
* The overlay version helps deal with backward incompatible changes for
* overlay analysis. When a precompiled query pack reports the same overlay
@@ -9,5 +14,21 @@ export interface VersionInfo {
* or if either the pack or the CLI does not report an overlay version,
* we need to revert to non-overlay analysis.
*/
overlayVersion?: number;
}
overlayVersion: json.optional(json.number),
} as const satisfies json.Schema;
/**
* The base type that describes the expected output of the `codeql version` command.
* This type is partially derived from {@link versionInfoBaseSchema}.
*/
export type VersionInfoBase = json.FromSchema<typeof versionInfoBaseSchema>;
/**
* The full type that describes the expected output of the `codeql version` command.
* This type is partially derived from {@link VersionInfoBase}.
*/
export type VersionInfo = VersionInfoBase & {
// `features` remains optional, but the more specific type takes precedence
// over the `any` type derived by `FromSchema`.
features?: { [name: string]: boolean };
};