UNPKG

sanity

Version:

Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches

57 lines (56 loc) 2.41 kB
import { writeFile } from "node:fs/promises"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { Worker } from "node:worker_threads"; import readPkgUp from "read-pkg-up"; import { defineTrace } from "@sanity/telemetry"; const SchemaExtractedTrace = defineTrace({ name: "Schema Extracted", version: 0, description: "Trace emitted when extracting schema" }), __dirname$1 = dirname(fileURLToPath(import.meta.url)); async function extractAction(args, { workDir, output, telemetry }) { const flags = args.extOptions, formatFlag = flags.format || "groq-type-nodes", enforceRequiredFields = flags["enforce-required-fields"] || !1, rootPkgPath = readPkgUp.sync({ cwd: __dirname$1 })?.path; if (!rootPkgPath) throw new Error("Could not find root directory for `sanity` package"); const workerPath = join(dirname(rootPkgPath), "lib", "_internal", "cli", "threads", "extractSchema.cjs"), spinner = output.spinner({}).start(enforceRequiredFields ? "Extracting schema, with enforced required fields" : "Extracting schema"), trace = telemetry.trace(SchemaExtractedTrace); trace.start(); const worker = new Worker(workerPath, { workerData: { workDir, workspaceName: flags.workspace, enforceRequiredFields, format: formatFlag }, env: process.env }); try { const { schema } = await new Promise((resolve, reject) => { worker.addListener("message", resolve), worker.addListener("error", reject); }); trace.log({ schemaAllTypesCount: schema.length, schemaDocumentTypesCount: schema.filter((type) => type.type === "document").length, schemaTypesCount: schema.filter((type) => type.type === "type").length, enforceRequiredFields, schemaFormat: formatFlag }); const path = flags.path || join(process.cwd(), "schema.json"); spinner.text = `Writing schema to ${path}`, await writeFile(path, `${JSON.stringify(schema, null, 2)} `), trace.complete(), spinner.succeed(enforceRequiredFields ? `Extracted schema to ${path} with enforced required fields` : `Extracted schema to ${path}`); } catch (err) { throw trace.error(err), spinner.fail(enforceRequiredFields ? "Failed to extract schema, with enforced required fields" : "Failed to extract schema"), err; } } export { extractAction as default }; //# sourceMappingURL=extractAction.js.map