Files
lincodex/frontend/scripts/check-orval-compat.mjs
T

85 lines
3.4 KiB
JavaScript

// Generates the OpenAPI 3.2 compatibility fixture with the production Orval
// settings and the mock pattern post-step into a temporary directory,
// type-checks the output with the frontend compiler settings, and checks that
// the API conventions reached the generated code. The directory is removed
// afterwards.
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { dirname, join, relative, resolve } from "node:path";
import { env, execPath, stdout } from "node:process";
import { fileURLToPath } from "node:url";
const frontendRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const bin = (name) => resolve(frontendRoot, "node_modules/.bin", name);
const fixture = resolve(frontendRoot, "../api/testdata/openapi-3.2.0.yml");
// Text that each fixture convention leaves in the generated TypeScript.
const expectedFragments = {
IdempotencyKey: '"Idempotency-Key": zod.string().min(1).max(',
IfMatch: '"If-Match": zod.string().min(',
PageLimit: '"limit": zod.int().min(1).max(',
PageCursor: '"cursor": zod.string().min(1).max(',
PageInfo: '"next_cursor": zod.string().min(1).max(',
Timestamp: "export const Timestamp = zod.iso.datetime(",
Problem: "export const Problem = zod.object(",
ProblemErrorType: "TError = ErrorType<Problem>",
PageCursorMock: 'fakeFromPattern("^[A-Za-z0-9_-]+$", {minLength: 1, maxLength: 512})',
HeaderArgument: "headers: CreateProbeItemHeaders",
OptionalHeaderArgument: "headers: UpdateProbeItemHeaders",
};
function run(command, args, extraEnv = {}) {
const result = spawnSync(command, args, {
cwd: frontendRoot,
encoding: "utf8",
env: { ...env, ...extraEnv },
});
assert.equal(
result.status,
0,
`${relative(frontendRoot, command)} failed:\n${result.stdout}\n${result.stderr}`,
);
}
function typeScriptFiles(directory) {
return readdirSync(directory, { recursive: true, encoding: "utf8" })
.filter((name) => name.endsWith(".ts"))
.map((name) => join(directory, name));
}
const outputDirectory = mkdtempSync(join(frontendRoot, ".orval-compat-"));
try {
run(bin("orval"), ["--config", "./codegen/orval.config.ts"], {
ORVAL_COMPAT_OUTPUT: outputDirectory,
});
// The same post-step as `pnpm run generate`, see codegen/mock-patterns.ts.
run(execPath, [
"./codegen/mock-patterns.ts",
"--spec",
fixture,
"--endpoints",
join(outputDirectory, "endpoints"),
]);
const files = typeScriptFiles(outputDirectory);
assert.ok(files.length > 0, "Orval generated no files for the compatibility fixture.");
writeFileSync(
join(outputDirectory, "tsconfig.json"),
JSON.stringify({
extends: "../tsconfig.json",
files: files.map((file) => relative(outputDirectory, file)),
}),
);
run(bin("tsc"), ["--noEmit", "-p", outputDirectory]);
const generated = files.map((file) => readFileSync(file, "utf8")).join("\n");
for (const [component, fragment] of Object.entries(expectedFragments)) {
assert.ok(generated.includes(fragment), `Generated code lacks ${component}: ${fragment}`);
}
assert.ok(!generated.includes("fromRegExp"), "Generated mocks still call faker fromRegExp.");
stdout.write(`Orval compatibility fixture: ${files.length} files type-checked.\n`);
} finally {
rmSync(outputDirectory, { recursive: true, force: true });
}