diff --git a/packages/sanity/src/_internal/cli/threads/extractManifest.ts b/packages/sanity/src/_internal/cli/threads/extractManifest.ts index e3080dce09f..3efa233607e 100644 --- a/packages/sanity/src/_internal/cli/threads/extractManifest.ts +++ b/packages/sanity/src/_internal/cli/threads/extractManifest.ts @@ -9,15 +9,15 @@ export interface ExtractManifestWorkerData { workDir: string } -if (isMainThread || !parentPort) { - throw new Error('This module must be run as a worker thread') -} +async function main() { + if (isMainThread || !parentPort) { + throw new Error('This module must be run as a worker thread') + } -const opts = _workerData as ExtractManifestWorkerData + const opts = _workerData as ExtractManifestWorkerData -const cleanup = mockBrowserEnvironment(opts.workDir) + const cleanup = mockBrowserEnvironment(opts.workDir) -async function main() { try { const workspaces = await getStudioWorkspaces({basePath: opts.workDir}) @@ -30,4 +30,4 @@ async function main() { } } -main() +main().then(() => process.exit()) diff --git a/packages/sanity/src/_internal/cli/threads/extractSchema.ts b/packages/sanity/src/_internal/cli/threads/extractSchema.ts index 8fb02fc00df..1c0f250bf44 100644 --- a/packages/sanity/src/_internal/cli/threads/extractSchema.ts +++ b/packages/sanity/src/_internal/cli/threads/extractSchema.ts @@ -19,14 +19,14 @@ export interface ExtractSchemaWorkerResult { schema: ReturnType } -if (isMainThread || !parentPort) { - throw new Error('This module must be run as a worker thread') -} +async function main() { + if (isMainThread || !parentPort) { + throw new Error('This module must be run as a worker thread') + } -const opts = _workerData as ExtractSchemaWorkerData -const cleanup = mockBrowserEnvironment(opts.workDir) + const opts = _workerData as ExtractSchemaWorkerData + const cleanup = mockBrowserEnvironment(opts.workDir) -async function main() { try { if (opts.format !== 'groq-type-nodes') { throw new Error(`Unsupported format: "${opts.format}"`) @@ -48,7 +48,7 @@ async function main() { } } -main() +main().then(() => process.exit()) function getWorkspace({ workspaces, diff --git a/packages/sanity/src/_internal/cli/threads/getGraphQLAPIs.ts b/packages/sanity/src/_internal/cli/threads/getGraphQLAPIs.ts index c0555e5f907..5330202c3cd 100644 --- a/packages/sanity/src/_internal/cli/threads/getGraphQLAPIs.ts +++ b/packages/sanity/src/_internal/cli/threads/getGraphQLAPIs.ts @@ -9,11 +9,15 @@ import {type Workspace} from 'sanity' import {type SchemaDefinitionish, type TypeResolvedGraphQLAPI} from '../actions/graphql/types' import {getStudioWorkspaces} from '../util/getStudioWorkspaces' -if (isMainThread || !parentPort) { - throw new Error('This module must be run as a worker thread') +async function main() { + if (isMainThread || !parentPort) { + throw new Error('This module must be run as a worker thread') + } + + await getGraphQLAPIsForked(parentPort) } -getGraphQLAPIsForked(parentPort) +main().then(() => process.exit()) async function getGraphQLAPIsForked(parent: MessagePort): Promise { const {cliConfig, cliConfigPath, workDir} = workerData diff --git a/packages/sanity/src/_internal/cli/threads/validateDocuments.ts b/packages/sanity/src/_internal/cli/threads/validateDocuments.ts index cefeae6dca1..3c917cd397f 100644 --- a/packages/sanity/src/_internal/cli/threads/validateDocuments.ts +++ b/packages/sanity/src/_internal/cli/threads/validateDocuments.ts @@ -129,7 +129,7 @@ async function* readerToGenerator(reader: ReadableStreamDefaultReader process.exit()) async function loadWorkspace() { const workspaces = await getStudioWorkspaces({basePath: workDir, configPath}) @@ -302,7 +302,7 @@ async function checkReferenceExistence({ return {existingIds} } -async function validateDocuments() { +async function main() { // note: this is dynamically imported because this module is ESM only and this // file gets compiled to CJS at this time const {default: pMap} = await import('p-map') diff --git a/packages/sanity/src/_internal/cli/threads/validateSchema.ts b/packages/sanity/src/_internal/cli/threads/validateSchema.ts index ae4990ce922..2045d03ae20 100644 --- a/packages/sanity/src/_internal/cli/threads/validateSchema.ts +++ b/packages/sanity/src/_internal/cli/threads/validateSchema.ts @@ -25,53 +25,57 @@ const { level = 'warning', } = _workerData as ValidateSchemaWorkerData -if (isMainThread || !parentPort) { - throw new Error('This module must be run as a worker thread') -} +async function main() { + if (isMainThread || !parentPort) { + throw new Error('This module must be run as a worker thread') + } -const cleanup = mockBrowserEnvironment(workDir) + const cleanup = mockBrowserEnvironment(workDir) -try { - const workspaces = getStudioConfig({basePath: workDir}) + try { + const workspaces = getStudioConfig({basePath: workDir}) - if (!workspaces.length) { - throw new Error(`Configuration did not return any workspaces.`) - } - - let workspace - if (workspaceName) { - workspace = workspaces.find((w) => w.name === workspaceName) - if (!workspace) { - throw new Error(`Could not find any workspaces with name \`${workspaceName}\``) + if (!workspaces.length) { + throw new Error(`Configuration did not return any workspaces.`) } - } else { - if (workspaces.length !== 1) { - throw new Error( - "Multiple workspaces found. Please specify which workspace to use with '--workspace'.", - ) + + let workspace + if (workspaceName) { + workspace = workspaces.find((w) => w.name === workspaceName) + if (!workspace) { + throw new Error(`Could not find any workspaces with name \`${workspaceName}\``) + } + } else { + if (workspaces.length !== 1) { + throw new Error( + "Multiple workspaces found. Please specify which workspace to use with '--workspace'.", + ) + } + workspace = workspaces[0] } - workspace = workspaces[0] - } - const schemaTypes = resolveSchemaTypes({ - config: workspace, - context: {dataset: workspace.dataset, projectId: workspace.projectId}, - }) + const schemaTypes = resolveSchemaTypes({ + config: workspace, + context: {dataset: workspace.dataset, projectId: workspace.projectId}, + }) - const validation = groupProblems(validateSchema(schemaTypes).getTypes()) + const validation = groupProblems(validateSchema(schemaTypes).getTypes()) - const result: ValidateSchemaWorkerResult = { - validation: validation - .map((group) => ({ - ...group, - problems: group.problems.filter((problem) => - level === 'error' ? problem.severity === 'error' : true, - ), - })) - .filter((group) => group.problems.length), - } + const result: ValidateSchemaWorkerResult = { + validation: validation + .map((group) => ({ + ...group, + problems: group.problems.filter((problem) => + level === 'error' ? problem.severity === 'error' : true, + ), + })) + .filter((group) => group.problems.length), + } - parentPort?.postMessage(result) -} finally { - cleanup() + parentPort?.postMessage(result) + } finally { + cleanup() + } } + +main().then(() => process.exit())