REC-032 Soul finding: dist/contracts.js required "../../../contracts/cultnet/...",
escaping the cultnet-ts package tree entirely. That breaks every installed
consumer: the two-runtime sample script (packs and installs cultnet-ts into a
clean consumer), test-typescript-package-closure.mjs, and Heimdall's pnpm copy
of cultnet-ts (honours `files`, would 404 on its next vendor/CultLib bump).
Only Stonks (file: symlink into the live checkout) happened to still work.
Fix: keep CultLib/contracts/cultnet as the only tracked source, but have
tools/generate-swarm-contracts.mjs wipe and re-copy it into a package-local,
gitignored packages/cultnet-ts/contracts/cultnet/ every time it runs (build,
typecheck, test, test:interop, prepack, the two-runtime script, and Heimdral's
`npm run build` all already invoke it). src/contracts.ts and the swarm-contracts
manifest now import that package-local copy ("../contracts/cultnet/...") instead
of reaching three directories up. package.json gets back `files: ["dist",
"contracts"]` and `prepack` so a packed tarball carries its own schema copy.
Also revert fa00f3f's tsconfig.test.json rootDir widen ("." -> "../..") and the
matching test-script/dist-test path changes and __dirname-relative recomputation
in cultnet-interop.test.ts and runtime-presence-health.test.ts: that widening
was working around dist-test not receiving a copy of the (now out-of-package)
contracts, but nothing in this repo actually needs the wider rootDir once the
schemas are package-local again, and the widened paths never matched what CI's
interop lanes and package.json's own dist-test paths expect --
packages/cultnet-ts/dist-test/test/interop/cultnet-interop.test.js, not
.../dist-test/packages/cultnet-ts/test/interop/.... PR #3 CI run 34766892424 on
7d3a3dc failed the first interop lane on exactly this missing-file defect.
cultnet-interop.test.ts's interopSchemaPath already resolved correctly once
cultNetTsRoot was reverted (it was never touched by fa00f3f) and continues to
point at the canonical CultLib/contracts/cultnet/interop copy, which is fine
since it is only read by the spawned non-TS peer processes, not required() by
this package. The cultmesh-ts shard-catalog $id lookup fix from fa00f3f
(cultnet.shard_catalog_request.v0 -> the CultLib contracts URL id) is unrelated
to the rootDir defect and is left as is.
REC-032, Soul redirect on branch claude/rec-032-cultnet-contracts.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
98 lines
3.9 KiB
JavaScript
98 lines
3.9 KiB
JavaScript
import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { compile } from "json-schema-to-typescript";
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
const packageRoot = path.resolve(__dirname, "..");
|
|
const cultLibRoot = path.resolve(packageRoot, "..", "..");
|
|
const manifestPath = path.join(packageRoot, "tools", "swarm-contracts.manifest.json");
|
|
const outputPath = path.join(packageRoot, "src", "generated", "swarm-contracts.generated.ts");
|
|
|
|
// CultLib/contracts/cultnet is the single source of CultNet schema contracts
|
|
// (REC-032). cultnet-ts is a standalone npm package (published or vendored by
|
|
// Heimdall independent of the CultLib checkout), so it cannot import schemas
|
|
// that live outside its own package tree. Mirror the canonical contracts into
|
|
// a package-local, gitignored copy every time this generator runs, so `dist`
|
|
// and `contracts` both ship self-contained. Wipe first: a stale local file
|
|
// left behind after a schema is deleted upstream must not survive as drift.
|
|
const sourceContractsDir = path.join(cultLibRoot, "contracts", "cultnet");
|
|
const localContractsDir = path.join(packageRoot, "contracts", "cultnet");
|
|
await rm(localContractsDir, { recursive: true, force: true });
|
|
await mkdir(path.dirname(localContractsDir), { recursive: true });
|
|
await cp(sourceContractsDir, localContractsDir, { recursive: true });
|
|
|
|
/**
|
|
* @typedef {{
|
|
* schemaPath: string;
|
|
* typeName: string;
|
|
* contractExportName: string;
|
|
* validateExportName: string;
|
|
* parseExportName: string;
|
|
* }} ContractManifestEntry
|
|
*/
|
|
|
|
/** @type {{ contracts: ContractManifestEntry[] }} */
|
|
const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
|
|
|
|
const importLines = [
|
|
'import { defineJsonSchemaContract } from "../json-schema-contract";',
|
|
];
|
|
const sections = [
|
|
"// Generated by tools/generate-swarm-contracts.mjs. Do not hand-edit this file unless you enjoy losing arguments with a machine.",
|
|
];
|
|
|
|
for (const entry of manifest.contracts) {
|
|
const absoluteSchemaPath = path.resolve(path.dirname(manifestPath), entry.schemaPath);
|
|
const schema = JSON.parse(await readFile(absoluteSchemaPath, "utf8"));
|
|
const schemaForTypes = structuredClone(schema);
|
|
delete schemaForTypes.title;
|
|
const schemaImportName = toIdentifier(`${entry.contractExportName}Schema`);
|
|
const relativeImportPath = path.relative(path.dirname(outputPath), absoluteSchemaPath).replace(/\\/g, "/");
|
|
importLines.push(`import ${schemaImportName} from "${ensureRelativeModule(relativeImportPath)}";`);
|
|
|
|
const rawTypeSource = await compile(schemaForTypes, entry.typeName, {
|
|
bannerComment: "",
|
|
style: {
|
|
singleQuote: true,
|
|
semi: true,
|
|
},
|
|
});
|
|
const typeSource = rewriteRootTypeName(rawTypeSource, entry.typeName);
|
|
|
|
sections.push(typeSource.trim());
|
|
sections.push(`
|
|
export const ${entry.contractExportName} = defineJsonSchemaContract<${entry.typeName}>({
|
|
schemaId: ${JSON.stringify(schema.$id ?? entry.typeName)},
|
|
schema: ${schemaImportName},
|
|
});
|
|
|
|
export function ${entry.validateExportName}(value: unknown): value is ${entry.typeName} {
|
|
return ${entry.contractExportName}.validate(value);
|
|
}
|
|
|
|
export function ${entry.parseExportName}(value: unknown): ${entry.typeName} {
|
|
return ${entry.contractExportName}.parse(value);
|
|
}`.trim());
|
|
}
|
|
|
|
const output = `${importLines.join("\n")}\n\n${sections.join("\n\n")}\n`;
|
|
await mkdir(path.dirname(outputPath), { recursive: true });
|
|
await writeFile(outputPath, output, "utf8");
|
|
|
|
function ensureRelativeModule(value) {
|
|
return value.startsWith(".") ? value : `./${value}`;
|
|
}
|
|
|
|
function toIdentifier(value) {
|
|
return value.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
}
|
|
|
|
function rewriteRootTypeName(typeSource, desiredTypeName) {
|
|
return typeSource.replace(
|
|
/export\s+(interface|type)\s+([A-Za-z0-9_$]+)/u,
|
|
(_match, kind) => `export ${kind} ${desiredTypeName}`,
|
|
);
|
|
}
|