Skip to content

Commit

Permalink
feat: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
RostiMelk committed Jan 15, 2025
1 parent b7c170a commit 32557c5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ export async function bootstrapRemoteTemplate(

debug('Applying environment variables')
const readToken = needsReadToken
? await generateSanityApiToken(READ_TOKEN_LABEL, 'viewer', variables.projectId, apiClient)
? await generateSanityApiToken(READ_TOKEN_LABEL, 'read', variables.projectId, apiClient)
: undefined
const writeToken = needsWriteToken
? await generateSanityApiToken(WRITE_TOKEN_LABEL, 'editor', variables.projectId, apiClient)
? await generateSanityApiToken(WRITE_TOKEN_LABEL, 'write', variables.projectId, apiClient)
: undefined

for (const pkg of packages ?? ['']) {
Expand Down
13 changes: 6 additions & 7 deletions packages/@sanity/cli/src/util/remoteTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const ENV_VAR = {
WRITE_TOKEN: 'SANITY_API_WRITE_TOKEN',
} as const

const API_READ_TOKEN_ROLE = 'viewer'
const API_WRITE_TOKEN_ROLE = 'editor'

type EnvData = {
projectId: string
dataset: string
Expand Down Expand Up @@ -202,11 +205,7 @@ export async function checkIfNeedsApiToken(root: string, type: 'read' | 'write')
}),
)
const templateContent = await readFile(join(root, templatePath), 'utf8')
const tokenEnvMap = {
read: ENV_VAR.READ_TOKEN,
write: ENV_VAR.WRITE_TOKEN,
}
return templateContent.includes(tokenEnvMap[type])
return templateContent.includes(type === 'read' ? ENV_VAR.READ_TOKEN : ENV_VAR.WRITE_TOKEN)
} catch {
return false
}
Expand Down Expand Up @@ -285,7 +284,7 @@ export async function tryApplyPackageName(root: string, name: string): Promise<v

export async function generateSanityApiToken(
label: string,
roleName: 'viewer' | 'editor',
type: 'read' | 'write',
projectId: string,
apiClient: CliApiClient,
): Promise<string> {
Expand All @@ -296,7 +295,7 @@ export async function generateSanityApiToken(
method: 'POST',
body: {
label: `${label} (${Date.now()})`,
roleName,
roleName: type === 'read' ? API_READ_TOKEN_ROLE : API_WRITE_TOKEN_ROLE,
},
})
return response.key
Expand Down

0 comments on commit 32557c5

Please sign in to comment.