{"version":3,"file":"extractSchema.cjs","sources":["../../../../src/_internal/cli/threads/extractSchema.ts"],"sourcesContent":["import {isMainThread, parentPort, workerData as _workerData} from 'node:worker_threads'\n\nimport {extractSchema} from '@sanity/schema/_internal'\nimport {type SchemaValidationProblemGroup} from '@sanity/types'\nimport {SchemaError, type Workspace} from 'sanity'\n\nimport {getStudioWorkspaces} from '../util/getStudioWorkspaces'\nimport {mockBrowserEnvironment} from '../util/mockBrowserEnvironment'\n\n/** @internal */\nexport interface ExtractSchemaWorkerData {\n  workDir: string\n  workspaceName?: string\n  enforceRequiredFields?: boolean\n  format: 'groq-type-nodes' | string\n}\n\n/** @internal */\nexport interface ExtractSchemaWorkerResult {\n  type: 'success'\n  schema: ReturnType<typeof extractSchema>\n}\n\n/** @internal */\nexport interface ExtractSchemaWorkerError {\n  type: 'error'\n  error: string\n  validation?: SchemaValidationProblemGroup[]\n}\n\n/** @internal */\nexport type ExtractSchemaWorkerMessage = ExtractSchemaWorkerResult | ExtractSchemaWorkerError\n\nasync function main() {\n  if (isMainThread || !parentPort) {\n    throw new Error('This module must be run as a worker thread')\n  }\n\n  const opts = _workerData as ExtractSchemaWorkerData\n  const cleanup = mockBrowserEnvironment(opts.workDir)\n\n  try {\n    if (opts.format !== 'groq-type-nodes') {\n      throw new Error(`Unsupported format: \"${opts.format}\"`)\n    }\n\n    const workspaces = await getStudioWorkspaces({basePath: opts.workDir})\n\n    const workspace = getWorkspace({workspaces, workspaceName: opts.workspaceName})\n\n    const schema = extractSchema(workspace.schema, {\n      enforceRequiredFields: opts.enforceRequiredFields,\n    })\n\n    parentPort?.postMessage({\n      type: 'success',\n      schema,\n    } satisfies ExtractSchemaWorkerResult)\n  } catch (err) {\n    const validation = extractValidationFromSchemaError(err)\n    parentPort?.postMessage({\n      type: 'error',\n      error: err instanceof Error ? err.message : String(err),\n      validation,\n    } satisfies ExtractSchemaWorkerError)\n  } finally {\n    cleanup()\n  }\n}\n\n/**\n * Extracts validation problem groups from a SchemaError.\n */\nfunction extractValidationFromSchemaError(\n  error: unknown,\n): SchemaValidationProblemGroup[] | undefined {\n  if (error instanceof SchemaError) {\n    return error.schema._validation\n  }\n\n  return undefined\n}\n\nvoid main().then(() => process.exit())\n\nfunction getWorkspace({\n  workspaces,\n  workspaceName,\n}: {\n  workspaces: Workspace[]\n  workspaceName?: string\n}): Workspace {\n  if (workspaces.length === 0) {\n    throw new Error('No studio configuration found')\n  }\n\n  if (workspaces.length === 1) {\n    return workspaces[0]\n  }\n\n  if (workspaceName === undefined) {\n    throw new Error(\n      `Multiple workspaces found. Please specify which workspace to use with '--workspace' or with the schemaExtraction.workspace config prop. Available workspaces: ${workspaces.map((w) => w.name).join(', ')}`,\n    )\n  }\n  const workspace = workspaces.find((w) => w.name === workspaceName)\n  if (!workspace) {\n    throw new Error(\n      `Could not find \"${workspaceName}\" workspace. Available workspaces: ${workspaces.map((w) => w.name).join(', ')}`,\n    )\n  }\n  return workspace\n}\n"],"names":["main","isMainThread","parentPort","Error","opts","_workerData","cleanup","mockBrowserEnvironment","workDir","format","workspaces","getStudioWorkspaces","basePath","workspace","getWorkspace","workspaceName","schema","extractSchema","enforceRequiredFields","postMessage","type","err","validation","extractValidationFromSchemaError","error","message","String","SchemaError","_validation","then","process","exit","length","undefined","map","w","name","join","find"],"mappings":";;AAiCA,eAAeA,OAAO;AACpB,MAAIC,oBAAAA,gBAAgB,CAACC,oBAAAA;AACnB,UAAM,IAAIC,MAAM,4CAA4C;AAG9D,QAAMC,OAAOC,oBAAAA,YACPC,UAAUC,uBAAAA,uBAAuBH,KAAKI,OAAO;AAEnD,MAAI;AACF,QAAIJ,KAAKK,WAAW;AAClB,YAAM,IAAIN,MAAM,wBAAwBC,KAAKK,MAAM,GAAG;AAGxD,UAAMC,aAAa,MAAMC,wCAAoB;AAAA,MAACC,UAAUR,KAAKI;AAAAA,IAAAA,CAAQ,GAE/DK,YAAYC,aAAa;AAAA,MAACJ;AAAAA,MAAYK,eAAeX,KAAKW;AAAAA,I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