Skip to content

Commit

Permalink
feat: support for studio alt
Browse files Browse the repository at this point in the history
  • Loading branch information
RostiMelk committed Nov 26, 2024
1 parent ce43f61 commit 7b065c4
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions packages/@sanity/cli/src/util/remoteTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {x} from 'tar'
import {parse as parseYaml} from 'yaml'

const ENV_VAR = {
PROJECT_ID: 'SANITY_PROJECT_ID',
DATASET: 'SANITY_DATASET',
API_VERSION: 'SANITY_API_VERSION',
PROJECT_ID: /SANITY(?:_STUDIO)?_PROJECT_ID/, // Matches SANITY_PROJECT_ID and SANITY_STUDIO_PROJECT_ID
DATASET: /SANITY(?:_STUDIO)?_DATASET/, // Matches SANITY_DATASET and SANITY_STUDIO_DATASET
READ_TOKEN: 'SANITY_API_READ_TOKEN',
API_VERSION: /SANITY(?:_STUDIO)?_API_VERSION/, // Matches SANITY_API_VERSION and SANITY_STUDIO_API_VERSION
} as const

const ENV_FILE = {
Expand Down Expand Up @@ -325,16 +326,16 @@ async function validatePackage(
)
if (envFile) {
const envContent = envFile.content
const hasProjectId = envContent.includes(ENV_VAR.PROJECT_ID)
const hasDataset = envContent.includes(ENV_VAR.DATASET)
const hasProjectId = envContent.match(ENV_VAR.PROJECT_ID)
const hasDataset = envContent.match(ENV_VAR.DATASET)

if (!hasProjectId || !hasDataset) {
const missing = []
if (!hasProjectId) missing.push(ENV_VAR.PROJECT_ID)
if (!hasDataset) missing.push(ENV_VAR.DATASET)
if (!hasProjectId) missing.push('SANITY_PROJECT_ID or SANITY_STUDIO_PROJECT_ID')
if (!hasDataset) missing.push('SANITY_DATASET or SANITY_STUDIO_DATASET')
throw new Error(
`Environment template in ${
packagePath || 'root'
packagePath || 'repo'
} must include the following variables: ${missing.join(', ')}`,
)
}
Expand Down Expand Up @@ -364,8 +365,8 @@ async function validatePackage(
* - Must have .env.template, .env.example, or .env.local.example
*
* Environment files must include:
* - SANITY_PROJECT_ID variable
* - SANITY_DATASET variable
* - SANITY_PROJECT_ID or SANITY_STUDIO_PROJECT_ID variable
* - SANITY_DATASET or SANITY_STUDIO_DATASET variable
*
* @param repoInfo - Repository information including username, name, branch, and file path
* @param bearerToken - Optional GitHub API bearer token for private repositories
Expand Down Expand Up @@ -441,28 +442,28 @@ export async function applyEnvVariables(
const templateContent = await readFile(join(root, templatePath), 'utf8')
const {projectId, dataset, apiVersion = 'vX'} = envData

const replaceVariable = (
content: string,
varName: string,
value: string,
match: RegExpMatchArray | null,
) => {
const findAndReplaceVariable = (content: string, varRegex: RegExp | string, value: string) => {
const pattern = varRegex instanceof RegExp ? varRegex : new RegExp(`${varRegex}=.*$`, 'm')
const match = content.match(pattern)
if (!match) return content

const varName = match[0].split('=')[0]
const hasQuotes = match[0].includes('"')
return content.replace(
new RegExp(`${varName}=.*$`, 'm'),
`${varName}=${match?.[1].includes('"') ? `"${value}"` : value}`,
`${varName}=${hasQuotes ? `"${value}"` : value}`,
)
}

let envContent = templateContent
const vars = [
{name: ENV_VAR.PROJECT_ID, value: projectId},
{name: ENV_VAR.DATASET, value: dataset},
{name: ENV_VAR.API_VERSION, value: apiVersion},
{pattern: ENV_VAR.PROJECT_ID, value: projectId},
{pattern: ENV_VAR.DATASET, value: dataset},
{pattern: ENV_VAR.API_VERSION, value: apiVersion},
]

for (const {name, value} of vars) {
const match = templateContent.match(new RegExp(`${name}=(.*)$`, 'm'))
envContent = replaceVariable(envContent, name, value, match)
for (const {pattern, value} of vars) {
envContent = findAndReplaceVariable(envContent, pattern, value)
}

await writeFile(join(root, targetName), envContent)
Expand All @@ -482,3 +483,7 @@ export async function applyPackageName(root: string, name: string): Promise<void
throw new Error('Failed to update package.json')
}
}

export function generateSanityApiReadToken() {
throw new Error('Not implemented')
}

0 comments on commit 7b065c4

Please sign in to comment.