From e09880003b4b05a4e274aca1f1eb86b7d1593252 Mon Sep 17 00:00:00 2001 From: Binoy Patel Date: Thu, 3 Oct 2024 12:13:16 -0400 Subject: [PATCH] fix(cli): use studioHost from CLI config for intent link (#7570) ### Description Updates the validate command to use studioHost configured in the CLI config instead of relying on the projects endpoint studioHost for this information. ### What to review Changes makes sense ### Testing Tests Updates ### Notes for release N/A (does not seem it is important to mention in release notes) --- .../cli/actions/validation/validateAction.ts | 10 ++++++++-- .../cli/actions/validation/validateDocuments.ts | 2 ++ .../threads/__tests__/validateDocuments.test.ts | 11 ++--------- .../_internal/cli/threads/validateDocuments.ts | 17 ++++------------- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/packages/sanity/src/_internal/cli/actions/validation/validateAction.ts b/packages/sanity/src/_internal/cli/actions/validation/validateAction.ts index 175ae43c73b..6f343b9ab01 100644 --- a/packages/sanity/src/_internal/cli/actions/validation/validateAction.ts +++ b/packages/sanity/src/_internal/cli/actions/validation/validateAction.ts @@ -1,7 +1,12 @@ import fs from 'node:fs' import path from 'node:path' -import {type CliCommandArguments, type CliCommandContext, type CliOutputter} from '@sanity/cli' +import { + type CliCommandArguments, + type CliCommandContext, + type CliConfig, + type CliOutputter, +} from '@sanity/cli' import {type ClientConfig} from '@sanity/client' import chalk from 'chalk' import logSymbols from 'log-symbols' @@ -31,7 +36,7 @@ export type BuiltInValidationReporter = (options: { export default async function validateAction( args: CliCommandArguments, - {apiClient, workDir, output, prompt}: CliCommandContext, + {apiClient, workDir, output, cliConfig, prompt}: CliCommandContext, ): Promise { const flags = args.extOptions const unattendedMode = Boolean(flags.yes || flags.y) @@ -160,6 +165,7 @@ export default async function validateAction( return reporter({output, worker, flags}) }, + studioHost: (cliConfig as CliConfig)?.studioHost, }) process.exitCode = overallLevel === 'error' ? 1 : 0 diff --git a/packages/sanity/src/_internal/cli/actions/validation/validateDocuments.ts b/packages/sanity/src/_internal/cli/actions/validation/validateDocuments.ts index aa2be78083d..4447c5b7d30 100644 --- a/packages/sanity/src/_internal/cli/actions/validation/validateDocuments.ts +++ b/packages/sanity/src/_internal/cli/actions/validation/validateDocuments.ts @@ -23,6 +23,7 @@ export interface ValidateDocumentsOptions { maxCustomValidationConcurrency?: number maxFetchConcurrency?: number reporter?: (worker: WorkerChannelReceiver) => TReturn + studioHost?: string } export interface DocumentValidationResult { @@ -102,6 +103,7 @@ export function validateDocuments(options: ValidateDocumentsOptions): unknown { ndjsonFilePath, maxCustomValidationConcurrency, maxFetchConcurrency, + studioHost: options.studioHost, } satisfies ValidateDocumentsWorkerData, // eslint-disable-next-line no-process-env env: process.env, diff --git a/packages/sanity/src/_internal/cli/threads/__tests__/validateDocuments.test.ts b/packages/sanity/src/_internal/cli/threads/__tests__/validateDocuments.test.ts index dcfac4c9950..c82178e4383 100644 --- a/packages/sanity/src/_internal/cli/threads/__tests__/validateDocuments.test.ts +++ b/packages/sanity/src/_internal/cli/threads/__tests__/validateDocuments.test.ts @@ -3,7 +3,7 @@ import path from 'node:path' import {Worker} from 'node:worker_threads' import {afterAll, beforeAll, describe, expect, it, jest} from '@jest/globals' -import {type SanityDocument, type SanityProject} from '@sanity/client' +import {type SanityDocument} from '@sanity/client' import {evaluate, parse} from 'groq-js' import {getMonorepoAliases} from '../../server/sanityMonorepo' @@ -125,14 +125,6 @@ describe('validateDocuments', () => { } switch (resource) { - case 'projects': { - json({ - studioHost: 'https://example.sanity.studio', - metadata: {externalStudioHost: localhost}, - } satisfies Partial) - return - } - case 'data': { const [method] = rest switch (method) { @@ -195,6 +187,7 @@ describe('validateDocuments', () => { useCdn: true, useProjectHostname: false, }, + studioHost: localhost, } const filepath = require.resolve('../validateDocuments') diff --git a/packages/sanity/src/_internal/cli/threads/validateDocuments.ts b/packages/sanity/src/_internal/cli/threads/validateDocuments.ts index 41716d260fd..cefeae6dca1 100644 --- a/packages/sanity/src/_internal/cli/threads/validateDocuments.ts +++ b/packages/sanity/src/_internal/cli/threads/validateDocuments.ts @@ -44,6 +44,7 @@ export interface ValidateDocumentsWorkerData { level?: ValidationMarker['level'] maxCustomValidationConcurrency?: number maxFetchConcurrency?: number + studioHost?: string } /** @internal */ @@ -52,7 +53,6 @@ export type ValidationWorkerChannel = WorkerChannel<{ name: string projectId: string dataset: string - studioHost: string | null basePath: string }> loadedDocumentCount: WorkerChannelEvent<{documentCount: number}> @@ -81,6 +81,7 @@ const { level, maxCustomValidationConcurrency, maxFetchConcurrency, + studioHost, } = _workerData as ValidateDocumentsWorkerData if (isMainThread || !parentPort) { @@ -160,24 +161,14 @@ async function loadWorkspace() { requestTagPrefix: 'sanity.cli.validate', }).config({apiVersion: 'v2021-03-25'}) - let studioHost - try { - const project = await client.projects.getById(projectId || workspace.projectId) - studioHost = project.metadata.externalStudioHost || project.studioHost - } catch { - // no big deal if we fail to get the studio host - studioHost = null - } - report.event.loadedWorkspace({ projectId: workspace.projectId, dataset: workspace.dataset, name: workspace.name, - studioHost, basePath: workspace.basePath, }) - return {workspace, client, studioHost} + return {workspace, client} } async function downloadFromExport(client: SanityClient) { @@ -321,7 +312,7 @@ async function validateDocuments() { let cleanupDownloadedDocuments: (() => Promise) | undefined try { - const {client, workspace, studioHost} = await loadWorkspace() + const {client, workspace} = await loadWorkspace() const {documentIds, referencedIds, getDocuments, cleanup} = ndjsonFilePath ? await downloadFromFile(ndjsonFilePath) : await downloadFromExport(client)