UNPKG

@softr/sdk

Version:

The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.

39 lines (37 loc) • 2.2 kB
import { requireDefined } from "../assertions.js"; import { DOC_NOTE, renderDatasourceDoc, renderDatasourceIndex } from "./datasource-docs.js"; import { renderDatasourceSchema } from "./datasource-schema.js"; import { docFileNames } from "./doc-file-names.js"; import { writeIfChanged } from "./write-if-changed.js"; import { existsSync, readFileSync, readdirSync, rmSync } from "node:fs"; import path from "node:path"; //#region src/codegen/write-datasource-docs.ts /** * Writes the index, and one doc and one schema JSON per datasource, and removes the doc and schema * of a datasource no longer registered. It removes only a doc that carries the generated note: the * caller resolves `docsDir` against `process.cwd()`, which can be any directory a developer ran the * bin from. `changed` is true when any file it wrote now reads differently, or it removed one. */ function writeDatasourceDocs(datasources, docsDir) { const context = { datasources: new Map(datasources.map((datasource) => [datasource.id, datasource])), fileNames: docFileNames(datasources) }; const current = new Set(context.fileNames.values()); const stale = (existsSync(docsDir) ? readdirSync(docsDir, { withFileTypes: true }) : []).filter((entry) => entry.isFile() && entry.name.endsWith(".md")).map((entry) => entry.name).filter((name) => !current.has(name) && readFileSync(path.join(docsDir, name), "utf8").includes(DOC_NOTE)); for (const name of stale) { rmSync(path.join(docsDir, name)); rmSync(path.join(docsDir, name.replace(/\.md$/, ".json")), { force: true }); } const writes = [ writeIfChanged(path.join(docsDir, "README.md"), renderDatasourceIndex(datasources, context)), ...datasources.map((datasource) => writeIfChanged(path.join(docsDir, requireDefined(context.fileNames.get(datasource.id))), renderDatasourceDoc(datasource, context))), ...datasources.map((datasource) => writeIfChanged(path.join(docsDir, requireDefined(context.fileNames.get(datasource.id)).replace(/\.md$/, ".json")), renderDatasourceSchema(datasource, context.datasources))) ]; return { fileNames: context.fileNames, changed: stale.length > 0 || writes.includes(true) }; } //#endregion export { writeDatasourceDocs };