Skip to content

Commit

Permalink
chore(cli): bump template validator (#8115)
Browse files Browse the repository at this point in the history
  • Loading branch information
RostiMelk authored and bjoerge committed Dec 20, 2024
1 parent b7a68b3 commit 5521f87
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 135 deletions.
2 changes: 1 addition & 1 deletion packages/@sanity/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@sanity/client": "^6.24.1",
"@sanity/codegen": "3.68.2",
"@sanity/telemetry": "^0.7.7",
"@sanity/template-validator": "^1.0.2",
"@sanity/template-validator": "^1.2.1",
"@sanity/util": "3.68.2",
"chalk": "^4.1.2",
"debug": "^4.3.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {mkdir} from 'node:fs/promises'
import {join} from 'node:path'

import {getMonoRepo, GitHubFileReader, validateTemplate} from '@sanity/template-validator'
import {type Framework, frameworks} from '@vercel/frameworks'
import {detectFrameworkRecord, LocalFileSystemDetector} from '@vercel/fs-detectors'

Expand All @@ -12,11 +13,10 @@ import {
checkNeedsReadToken,
downloadAndExtractRepo,
generateSanityApiReadToken,
getPackages,
getGitHubRawContentUrl,
type RepoInfo,
setCorsOrigin,
tryApplyPackageName,
validateRemoteTemplate,
} from '../../util/remoteTemplate'
import {type GenerateConfigOptions} from './createStudioConfig'
import {tryGitInit} from './git'
Expand All @@ -39,11 +39,20 @@ export async function bootstrapRemoteTemplate(
const {outputPath, repoInfo, bearerToken, variables, packageName} = opts
const {output, apiClient} = context
const name = [repoInfo.username, repoInfo.name, repoInfo.filePath].filter(Boolean).join('/')
const contentsUrl = getGitHubRawContentUrl(repoInfo)
const headers: Record<string, string> = {}
if (bearerToken) {
headers.Authorization = `Bearer ${bearerToken}`
}
const spinner = output.spinner(`Bootstrapping files from template "${name}"`).start()

debug('Validating remote template')
const packages = await getPackages(repoInfo, bearerToken)
await validateRemoteTemplate(repoInfo, packages, bearerToken)
const fileReader = new GitHubFileReader(contentsUrl, headers)
const packages = await getMonoRepo(fileReader)
const validation = await validateTemplate(fileReader, packages)
if (!validation.isValid) {
throw new Error(validation.errors.join('\n'))
}

debug('Create new directory "%s"', outputPath)
await mkdir(outputPath, {recursive: true})
Expand Down
40 changes: 2 additions & 38 deletions packages/@sanity/cli/src/util/remoteTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import {Readable} from 'node:stream'
import {pipeline} from 'node:stream/promises'
import {type ReadableStream} from 'node:stream/web'

import {
ENV_TEMPLATE_FILES,
getMonoRepo,
REQUIRED_ENV_VAR,
validateSanityTemplate,
} from '@sanity/template-validator'
import {ENV_TEMPLATE_FILES, REQUIRED_ENV_VAR} from '@sanity/template-validator'
import {x} from 'tar'

import {debug} from '../debug'
Expand Down Expand Up @@ -42,7 +37,7 @@ export type RepoInfo = {
filePath: string
}

function getGitHubRawContentUrl(repoInfo: RepoInfo): string {
export function getGitHubRawContentUrl(repoInfo: RepoInfo): string {
const {username, name, branch, filePath} = repoInfo
return `https://raw.githubusercontent.com/${username}/${name}/${branch}/${filePath}`
}
Expand Down Expand Up @@ -196,37 +191,6 @@ export async function downloadAndExtractRepo(
)
}

/**
* Checks if a GitHub repository is a monorepo by examining common monorepo configuration files.
* Supports pnpm workspaces, Lerna, Rush, and npm workspaces (package.json).
* @returns Promise that resolves to an array of package paths/names if monorepo is detected, undefined otherwise
*/
export async function getPackages(
repoInfo: RepoInfo,
bearerToken?: string,
): Promise<string[] | undefined> {
const headers: Record<string, string> = {}
if (bearerToken) {
headers.Authorization = `Bearer ${bearerToken}`
}
return getMonoRepo(getGitHubRawContentUrl(repoInfo), headers)
}

export async function validateRemoteTemplate(
repoInfo: RepoInfo,
packages: string[] = [''],
bearerToken?: string,
): Promise<void> {
const headers: Record<string, string> = {}
if (bearerToken) {
headers.Authorization = `Bearer ${bearerToken}`
}
const result = await validateSanityTemplate(getGitHubRawContentUrl(repoInfo), packages, headers)
if (!result.isValid) {
throw new Error(result.errors.join('\n'))
}
}

export async function checkNeedsReadToken(root: string): Promise<boolean> {
try {
const templatePath = await Promise.any(
Expand Down
Loading

0 comments on commit 5521f87

Please sign in to comment.