Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

2.2 KB
Raw Blame
import { writeFile } from "node:fs/promises";
import { createHash } from "node:crypto";
import { fileURLToPath } from "node:url";
import { createSchemaOrgData } from "./schema-org-data-lib";

const schemaOrgBase = "https://schema.org/";
const schemaOrgVersion = "30.0";
const sourceBase = `${schemaOrgBase}version/${schemaOrgVersion}/schemaorg-current-https`;
const sourceSha256 = {
  types: "1ae97702e7bd9b91a90b78df5133b8897a0f025a6f94669491a55c0bf1867626",
  properties:
    "3a6319bd496d2d3dc276e3a8af9b0ff6415993e7d1f6b466072fcbdec1e0d257",
} as const;

const fetchCsv = async (name: "types" | "properties") => {
  const response = await fetch(`${sourceBase}-${name}.csv`);
  if (response.ok === false) {
    throw Error(`Unable to download Schema.org ${name}: ${response.status}`);
  }
  const csv = await response.text();
  const actualSha256 = createHash("sha256").update(csv).digest("hex");
  if (actualSha256 !== sourceSha256[name]) {
    throw Error(
      `Schema.org ${name} checksum mismatch: expected ${sourceSha256[name]}, received ${actualSha256}`
    );
  }
  return csv;
};

const [typesCsv, propertiesCsv] = await Promise.all([
  fetchCsv("types"),
  fetchCsv("properties"),
]);
const { typeRows, propertyRows } = createSchemaOrgData({
  typesCsv,
  propertiesCsv,
});

const output = `// Generated by pnpm --filter @webstudio-is/sdk build:schema-org-data. Do not edit manually.
// Source: ${sourceBase}-{types,properties}.csv
// SHA-256: types=${sourceSha256.types} properties=${sourceSha256.properties}

export const schemaOrgDataVersion = ${JSON.stringify(schemaOrgVersion)};

export const schemaOrgDataSourceSha256 = ${JSON.stringify(sourceSha256)} as const;

export type SchemaOrgTypeRow = readonly [
  name: string,
  parentTypes: readonly string[],
  supersededBy: string,
];

export type SchemaOrgPropertyRow = readonly [
  name: string,
  domains: readonly string[],
  ranges: readonly string[],
  supersededBy: string,
];

export const schemaOrgTypeRows: readonly SchemaOrgTypeRow[] = ${JSON.stringify(typeRows)};

export const schemaOrgPropertyRows: readonly SchemaOrgPropertyRow[] = ${JSON.stringify(propertyRows)};
`;

await writeFile(
  fileURLToPath(
    new URL("../src/__generated__/schema-org-data.ts", import.meta.url)
  ),
  output
);