Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): use studioHost from CLI config for intent link #7570

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -31,7 +36,7 @@ export type BuiltInValidationReporter = (options: {

export default async function validateAction(
args: CliCommandArguments<ValidateFlags>,
{apiClient, workDir, output, prompt}: CliCommandContext,
{apiClient, workDir, output, cliConfig, prompt}: CliCommandContext,
): Promise<void> {
const flags = args.extOptions
const unattendedMode = Boolean(flags.yes || flags.y)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ValidateDocumentsOptions<TReturn = unknown> {
maxCustomValidationConcurrency?: number
maxFetchConcurrency?: number
reporter?: (worker: WorkerChannelReceiver<ValidationWorkerChannel>) => TReturn
studioHost?: string
}

export interface DocumentValidationResult {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -125,14 +125,6 @@ describe('validateDocuments', () => {
}

switch (resource) {
case 'projects': {
json({
studioHost: 'https://example.sanity.studio',
metadata: {externalStudioHost: localhost},
} satisfies Partial<SanityProject>)
return
}

case 'data': {
const [method] = rest
switch (method) {
Expand Down Expand Up @@ -195,6 +187,7 @@ describe('validateDocuments', () => {
useCdn: true,
useProjectHostname: false,
},
studioHost: localhost,
}

const filepath = require.resolve('../validateDocuments')
Expand Down
17 changes: 4 additions & 13 deletions packages/sanity/src/_internal/cli/threads/validateDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface ValidateDocumentsWorkerData {
level?: ValidationMarker['level']
maxCustomValidationConcurrency?: number
maxFetchConcurrency?: number
studioHost?: string
}

/** @internal */
Expand All @@ -52,7 +53,6 @@ export type ValidationWorkerChannel = WorkerChannel<{
name: string
projectId: string
dataset: string
studioHost: string | null
basePath: string
}>
loadedDocumentCount: WorkerChannelEvent<{documentCount: number}>
Expand Down Expand Up @@ -81,6 +81,7 @@ const {
level,
maxCustomValidationConcurrency,
maxFetchConcurrency,
studioHost,
} = _workerData as ValidateDocumentsWorkerData

if (isMainThread || !parentPort) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -321,7 +312,7 @@ async function validateDocuments() {
let cleanupDownloadedDocuments: (() => Promise<void>) | 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)
Expand Down
Loading