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
58 lines (57 loc) • 2.22 kB
JavaScript
import { writeFile } from "node:fs/promises";
import { join, dirname } from "node:path";
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"
});
async function extractAction(args, { workDir, output, telemetry }) {
var _a;
const flags = args.extOptions, formatFlag = flags.format || "groq-type-nodes", enforceRequiredFields = flags["enforce-required-fields"] || !1, rootPkgPath = (_a = readPkgUp.sync({ cwd: __dirname })) == null ? void 0 : _a.path;
if (!rootPkgPath)
throw new Error("Could not find root directory for `sanity` package");
const workerPath = join(
dirname(rootPkgPath),
"lib",
"_internal",
"cli",
"threads",
"extractSchema.js"
), 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
},
// eslint-disable-next-line no-process-env
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("Extracted schema");
} catch (err) {
throw trace.error(err), spinner.fail("Failed to extract schema"), err;
}
}
export {
extractAction as default
};
//# sourceMappingURL=extractAction.js.map