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

48 lines (47 loc) 1.97 kB
import path from "path"; import readPkgUp from "read-pkg-up"; import { createSchema } from "sanity"; import { isMainThread, Worker } from "worker_threads"; async function getGraphQLAPIs(cliContext) { if (!isModernCliConfig(cliContext)) throw new Error("Expected Sanity studio of version 3 or above"); if (!isMainThread) throw new Error("getGraphQLAPIs() must be called from the main thread"); const defaultTypes = createSchema({ name: "default", types: [] }).getTypeNames(), isCustomType = (type) => !defaultTypes.includes(type.name); return (await getApisWithSchemaTypes(cliContext)).map( ({ schemaTypes, ...api }) => ({ schema: createSchema({ name: "default", types: schemaTypes.filter(isCustomType) }), ...api }) ); } function getApisWithSchemaTypes(cliContext) { return new Promise((resolve, reject) => { var _a; const { cliConfig, cliConfigPath, workDir } = cliContext, 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 rootDir = path.dirname(rootPkgPath), workerPath = path.join(rootDir, "lib", "_internal", "cli", "threads", "getGraphQLAPIs.js"), worker = new Worker(workerPath, { workerData: { cliConfig: serialize(cliConfig || {}), cliConfigPath, workDir }, // eslint-disable-next-line no-process-env env: process.env }); worker.on("message", resolve), worker.on("error", reject), worker.on("exit", (code) => { code !== 0 && reject(new Error(`Worker stopped with exit code ${code}`)); }); }); } function isModernCliConfig(config) { return config.sanityMajorVersion >= 3; } function serialize(obj) { try { return JSON.parse(JSON.stringify(obj)); } catch (cause) { throw new Error("Failed to serialize CLI configuration", { cause }); } } export { getGraphQLAPIs as g }; //# sourceMappingURL=getGraphQLAPIs.js.map