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

1 lines 9.6 kB
{"version":3,"file":"validateSchema.cjs","sources":["../../../../src/_internal/cli/threads/validateSchema.ts"],"sourcesContent":["import {isMainThread, parentPort, workerData as _workerData} from 'node:worker_threads'\n\nimport {\n type EncodableObject,\n type EncodableValue,\n type SetSynchronization,\n} from '@sanity/descriptors'\nimport {DescriptorConverter} from '@sanity/schema/_internal'\nimport {type SchemaValidationProblem, type SchemaValidationProblemGroup} from '@sanity/types'\n\nimport {getStudioWorkspaces} from '../util/getStudioWorkspaces'\nimport {mockBrowserEnvironment} from '../util/mockBrowserEnvironment'\n\n/** @internal */\nexport interface ValidateSchemaWorkerData {\n workDir: string\n workspace?: string\n level?: SchemaValidationProblem['severity']\n debugSerialize?: boolean\n}\n\n/** @internal */\nexport interface ValidateSchemaWorkerResult {\n validation: SchemaValidationProblemGroup[]\n serializedDebug?: SeralizedSchemaDebug\n}\n\n/**\n * Contains debug information about the serialized schema.\n *\n * @internal\n **/\nexport type SeralizedSchemaDebug = {\n size: number\n parent?: SeralizedSchemaDebug\n types: Record<string, SerializedTypeDebug>\n hoisted: Record<string, SerializedTypeDebug>\n}\n\n/**\n * Contains debug information about a serialized type.\n *\n * @internal\n **/\nexport type SerializedTypeDebug = {\n size: number\n extends: string\n fields?: Record<string, SerializedTypeDebug>\n of?: Record<string, SerializedTypeDebug>\n}\n\nconst {\n workDir,\n workspace: workspaceName,\n level = 'warning',\n debugSerialize,\n} = _workerData as ValidateSchemaWorkerData\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 cleanup = mockBrowserEnvironment(workDir)\n\n try {\n const workspaces = await getStudioWorkspaces({basePath: workDir})\n\n if (!workspaces.length) {\n throw new Error(`Configuration did not return any workspaces.`)\n }\n\n let workspace\n if (workspaceName) {\n workspace = workspaces.find((w) => w.name === workspaceName)\n if (!workspace) {\n throw new Error(`Could not find any workspaces with name \\`${workspaceName}\\``)\n }\n } else {\n if (workspaces.length !== 1) {\n throw new Error(\n \"Multiple workspaces found. Please specify which workspace to use with '--workspace'.\",\n )\n }\n workspace = workspaces[0]\n }\n\n const schema = workspace.schema\n const validation = schema._validation!\n\n let serializedDebug: ValidateSchemaWorkerResult['serializedDebug']\n\n if (debugSerialize) {\n const conv = new DescriptorConverter()\n const set = await conv.get(schema)\n serializedDebug = getSeralizedSchemaDebug(set)\n }\n\n const result: ValidateSchemaWorkerResult = {\n validation: validation\n .map((group) => ({\n ...group,\n problems: group.problems.filter((problem) =>\n level === 'error' ? problem.severity === 'error' : true,\n ),\n }))\n .filter((group) => group.problems.length),\n serializedDebug,\n }\n\n parentPort?.postMessage(result)\n } catch (err) {\n console.error(err)\n console.error(err.stack)\n throw err\n } finally {\n cleanup()\n }\n}\n\nfunction getSeralizedSchemaDebug(set: SetSynchronization<string>): SeralizedSchemaDebug {\n let size = 0\n const types: Record<string, SerializedTypeDebug> = {}\n const hoisted: Record<string, SerializedTypeDebug> = {}\n\n for (const [id, value] of Object.entries(set.objectValues)) {\n const descType = typeof value.type === 'string' ? value.type : '<unknown>'\n switch (descType) {\n case 'sanity.schema.namedType': {\n const typeName = typeof value.name === 'string' ? value.name : id\n if (isEncodableObject(value.typeDef)) {\n const debug = getSerializedTypeDebug(value.typeDef)\n types[typeName] = debug\n size += debug.size\n }\n break\n }\n case 'sanity.schema.hoisted': {\n const key = typeof value.key === 'string' ? value.key : id\n // The `hoisted` can technically hoist _anything_,\n // but we detect the common case of field + array element.\n if (isEncodableObject(value.value) && isEncodableObject(value.value.typeDef)) {\n const debug = getSerializedTypeDebug(value.value.typeDef)\n hoisted[key] = debug\n size += debug.size\n }\n break\n }\n default:\n }\n size += JSON.stringify(value).length\n }\n\n return {\n size,\n types,\n hoisted,\n }\n}\n\nfunction isEncodableObject(val: EncodableValue | undefined): val is EncodableObject {\n return typeof val === 'object' && val !== null && !Array.isArray(val)\n}\n\nfunction getSerializedTypeDebug(typeDef: EncodableObject): SerializedTypeDebug {\n const ext = typeof typeDef.extends === 'string' ? typeDef.extends : '<unknown>'\n let fields: SerializedTypeDebug['fields']\n let of: SerializedTypeDebug['of']\n\n if (Array.isArray(typeDef.fields)) {\n fields = {}\n\n for (const field of typeDef.fields) {\n if (!isEncodableObject(field)) continue\n const name = field.name\n const fieldTypeDef = field.typeDef\n if (typeof name !== 'string' || !isEncodableObject(fieldTypeDef)) continue\n\n fields[name] = getSerializedTypeDebug(fieldTypeDef)\n }\n }\n\n if (Array.isArray(typeDef.of)) {\n of = {}\n\n for (const field of typeDef.of) {\n if (!isEncodableObject(field)) continue\n const name = field.name\n const arrayTypeDef = field.typeDef\n if (typeof name !== 'string' || !isEncodableObject(arrayTypeDef)) continue\n\n of[name] = getSerializedTypeDebug(arrayTypeDef)\n }\n }\n\n return {\n size: JSON.stringify(typeDef).length,\n extends: ext,\n fields,\n of,\n }\n}\n\nvoid main().then(() => process.exit())\n"],"names":["workDir","workspace","workspaceName","level","debugSerialize","_workerData","main","isMainThread","parentPort","Error","cleanup","mockBrowserEnvironment","workspaces","getStudioWorkspaces","basePath","length","find","w","name","schema","validation","_validation","serializedDebug","set","DescriptorConverter","get","getSeralizedSchemaDebug","result","map","group","problems","filter","problem","severity","postMessage","err","console","error","stack","size","types","hoisted","id","value","Object","entries","objectValues","type","typeName","isEncodableObject","typeDef","debug","getSerializedTypeDebug","key","JSON","stringify","val","Array","isArray","ext","extends","fields","of","field","fieldTypeDef","arrayTypeDef","then","process","exit"],"mappings":";;AAmDA,MAAM;AAAA,EACJA;AAAAA,EACAC,WAAWC;AAAAA,EACXC,QAAQ;AAAA,EACRC;AACF,IAAIC,oBAAAA;AAEJ,eAAeC,OAAO;AACpB,MAAIC,oBAAAA,gBAAgB,CAACC,oBAAAA;AACnB,UAAM,IAAIC,MAAM,4CAA4C;AAG9D,QAAMC,UAAUC,uBAAAA,uBAAuBX,OAAO;AAE9C,MAAI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