From 7b065c41647533e9dfe5fb2ce91f485f2d1549ee Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Tue, 26 Nov 2024 13:58:02 +0100 Subject: [PATCH] feat: support for studio alt --- .../@sanity/cli/src/util/remoteTemplate.ts | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/packages/@sanity/cli/src/util/remoteTemplate.ts b/packages/@sanity/cli/src/util/remoteTemplate.ts index b856bac18ee..647f2f7074d 100644 --- a/packages/@sanity/cli/src/util/remoteTemplate.ts +++ b/packages/@sanity/cli/src/util/remoteTemplate.ts @@ -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 = { @@ -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(', ')}`, ) } @@ -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 @@ -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) @@ -482,3 +483,7 @@ export async function applyPackageName(root: string, name: string): Promise