openclaw/scripts/lib/state-schema-inline-plugin.mts
Vincent Koc 8e5a3f3638
chore(test): migrate to stable Vitest 5 (#138264)
* build(test): migrate to stable Vitest 5

Co-authored-by: Peter Steinberger <steipete@gmail.com>

* docs(test): document Vitest 5 contracts

* test(test): prove Vitest cache invalidation

* test(test): stabilize report config load proof

* test(test): restore Vitest 5 compatibility

Co-authored-by: Peter Steinberger <steipete@gmail.com>

* test(test): isolate pnpm cache fixture registry

* fix(test): keep pnpm 12 env lock portable

* fix(test): preserve explicit Vitest project roots

* fix(test): preserve nested Vitest project identity

* test(test): expect captured Vitest name prefix

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-09-06 04:17:59 +09:00

53 lines
1.8 KiB
TypeScript

import fs from "node:fs";
import path from "node:path";
export const STATE_SCHEMA_INLINE_PLUGIN_NAME = "openclaw:inline-state-schemas";
const STATE_SCHEMA_MODULES = [
{
modulePath: "src/state/openclaw-state-schema.ts",
schemaPath: "src/state/openclaw-state-schema.sql",
exportName: "OPENCLAW_STATE_SCHEMA_SQL",
},
{
modulePath: "src/state/openclaw-agent-schema.ts",
schemaPath: "src/state/openclaw-agent-schema.sql",
exportName: "OPENCLAW_AGENT_SCHEMA_SQL",
},
] as const;
export const STATE_SCHEMA_GENERATOR_INPUTS = STATE_SCHEMA_MODULES.map(
({ schemaPath }) => schemaPath,
);
/** Inline canonical schema bytes so bundled consumers need no SQL asset. */
export function createStateSchemaInlinePlugin(rootDir = process.cwd()) {
const schemasByModulePath = new Map(
STATE_SCHEMA_MODULES.map((schema) => [path.resolve(rootDir, schema.modulePath), schema]),
);
const cacheKeyForSchema = ({ id }: { id: string }) => {
const schema = schemasByModulePath.get(path.resolve(id));
return schema ? fs.readFileSync(path.resolve(rootDir, schema.schemaPath), "utf8") : undefined;
};
return {
name: STATE_SCHEMA_INLINE_PLUGIN_NAME,
configureVitest(context: {
defineCacheKeyGenerator(callback: typeof cacheKeyForSchema): void;
}) {
context.defineCacheKeyGenerator(cacheKeyForSchema);
},
load(this: { addWatchFile(id: string): void }, id: string) {
const schema = schemasByModulePath.get(path.resolve(id));
if (!schema) {
return null;
}
const schemaPath = path.resolve(rootDir, schema.schemaPath);
this.addWatchFile(schemaPath);
return {
code: `export const ${schema.exportName} = ${JSON.stringify(fs.readFileSync(schemaPath, "utf8"))};\n`,
moduleType: "js" as const,
};
},
};
}