Skip to content

Commit e39c5b2

Browse files
RostiMelkbinoy14
authored andcommittedDec 20, 2024
chore(cli): bump template validator (#8115)
1 parent 1d23092 commit e39c5b2

File tree

4 files changed

+90
-135
lines changed

4 files changed

+90
-135
lines changed
 

‎packages/@sanity/cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"@sanity/client": "^6.24.1",
6161
"@sanity/codegen": "3.68.3",
6262
"@sanity/telemetry": "^0.7.7",
63-
"@sanity/template-validator": "^1.0.2",
63+
"@sanity/template-validator": "^1.2.1",
6464
"@sanity/util": "3.68.3",
6565
"chalk": "^4.1.2",
6666
"debug": "^4.3.4",

‎packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {mkdir} from 'node:fs/promises'
22
import {join} from 'node:path'
33

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

@@ -12,11 +13,10 @@ import {
1213
checkNeedsReadToken,
1314
downloadAndExtractRepo,
1415
generateSanityApiReadToken,
15-
getPackages,
16+
getGitHubRawContentUrl,
1617
type RepoInfo,
1718
setCorsOrigin,
1819
tryApplyPackageName,
19-
validateRemoteTemplate,
2020
} from '../../util/remoteTemplate'
2121
import {type GenerateConfigOptions} from './createStudioConfig'
2222
import {tryGitInit} from './git'
@@ -39,11 +39,20 @@ export async function bootstrapRemoteTemplate(
3939
const {outputPath, repoInfo, bearerToken, variables, packageName} = opts
4040
const {output, apiClient} = context
4141
const name = [repoInfo.username, repoInfo.name, repoInfo.filePath].filter(Boolean).join('/')
42+
const contentsUrl = getGitHubRawContentUrl(repoInfo)
43+
const headers: Record<string, string> = {}
44+
if (bearerToken) {
45+
headers.Authorization = `Bearer ${bearerToken}`
46+
}
4247
const spinner = output.spinner(`Bootstrapping files from template "${name}"`).start()
4348

4449
debug('Validating remote template')
45-
const packages = await getPackages(repoInfo, bearerToken)
46-
await validateRemoteTemplate(repoInfo, packages, bearerToken)
50+
const fileReader = new GitHubFileReader(contentsUrl, headers)
51+
const packages = await getMonoRepo(fileReader)
52+
const validation = await validateTemplate(fileReader, packages)
53+
if (!validation.isValid) {
54+
throw new Error(validation.errors.join('\n'))
55+
}
4756

4857
debug('Create new directory "%s"', outputPath)
4958
await mkdir(outputPath, {recursive: true})

‎packages/@sanity/cli/src/util/remoteTemplate.ts

+2-38
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import {Readable} from 'node:stream'
44
import {pipeline} from 'node:stream/promises'
55
import {type ReadableStream} from 'node:stream/web'
66

7-
import {
8-
ENV_TEMPLATE_FILES,
9-
getMonoRepo,
10-
REQUIRED_ENV_VAR,
11-
validateSanityTemplate,
12-
} from '@sanity/template-validator'
7+
import {ENV_TEMPLATE_FILES, REQUIRED_ENV_VAR} from '@sanity/template-validator'
138
import {x} from 'tar'
149

1510
import {debug} from '../debug'
@@ -42,7 +37,7 @@ export type RepoInfo = {
4237
filePath: string
4338
}
4439

45-
function getGitHubRawContentUrl(repoInfo: RepoInfo): string {
40+
export function getGitHubRawContentUrl(repoInfo: RepoInfo): string {
4641
const {username, name, branch, filePath} = repoInfo
4742
return `https://raw.githubusercontent.com/${username}/${name}/${branch}/${filePath}`
4843
}
@@ -196,37 +191,6 @@ export async function downloadAndExtractRepo(
196191
)
197192
}
198193

199-
/**
200-
* Checks if a GitHub repository is a monorepo by examining common monorepo configuration files.
201-
* Supports pnpm workspaces, Lerna, Rush, and npm workspaces (package.json).
202-
* @returns Promise that resolves to an array of package paths/names if monorepo is detected, undefined otherwise
203-
*/
204-
export async function getPackages(
205-
repoInfo: RepoInfo,
206-
bearerToken?: string,
207-
): Promise<string[] | undefined> {
208-
const headers: Record<string, string> = {}
209-
if (bearerToken) {
210-
headers.Authorization = `Bearer ${bearerToken}`
211-
}
212-
return getMonoRepo(getGitHubRawContentUrl(repoInfo), headers)
213-
}
214-
215-
export async function validateRemoteTemplate(
216-
repoInfo: RepoInfo,
217-
packages: string[] = [''],
218-
bearerToken?: string,
219-
): Promise<void> {
220-
const headers: Record<string, string> = {}
221-
if (bearerToken) {
222-
headers.Authorization = `Bearer ${bearerToken}`
223-
}
224-
const result = await validateSanityTemplate(getGitHubRawContentUrl(repoInfo), packages, headers)
225-
if (!result.isValid) {
226-
throw new Error(result.errors.join('\n'))
227-
}
228-
}
229-
230194
export async function checkNeedsReadToken(root: string): Promise<boolean> {
231195
try {
232196
const templatePath = await Promise.any(

‎pnpm-lock.yaml

+74-92
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.