Skip to content

Commit

Permalink
feat(cli): select org and add to sanity.cli.ts on initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
cngonzalez committed Feb 10, 2025
1 parent d517632 commit 34c7aec
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ export async function bootstrapLocalTemplate(

// ...and a CLI config (`sanity.cli.[ts|js]`)
const cliConfig = isCoreAppTemplate
? createCoreAppCliConfig({appLocation: template.appLocation!})
? createCoreAppCliConfig({
appLocation: template.appLocation!,
organizationId: variables.organizationId,
})
: createCliConfig({
projectId: variables.projectId,
dataset: variables.dataset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const defaultCoreAppTemplate = `
import {defineCliConfig} from 'sanity/cli'
export default defineCliConfig({
api: {
organizationId: '%organizationId%'
},
__experimental_coreAppConfiguration: {
appLocation: '%appLocation%'
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface GenerateConfigOptions {
projectName?: string
sourceName?: string
sourceTitle?: string
organizationId?: string
}
}

Expand Down
33 changes: 21 additions & 12 deletions packages/@sanity/cli/src/actions/init-project/initProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ export default async function initSanity(
// skip project / dataset prompting
const isCoreAppTemplate = cliFlags.template ? determineCoreAppTemplate(cliFlags.template) : false // Default to false

// We're authenticated, now lets select or create a project
const {projectId, displayName, isFirstProject, datasetName, schemaUrl} = await getProjectDetails()
// We're authenticated, now lets select or create a project (for studios) or org (for core apps)
const {projectId, displayName, isFirstProject, datasetName, schemaUrl, organizationId} =
await getProjectDetails()

const sluggedName = deburr(displayName.toLowerCase())
.replace(/\s+/g, '-')
Expand Down Expand Up @@ -715,6 +716,7 @@ export default async function initSanity(
displayName: string
isFirstProject: boolean
schemaUrl?: string
organizationId?: string
}> {
// If we're doing a quickstart, we don't need to prompt for project details
if (flags.quickstart) {
Expand All @@ -731,11 +733,17 @@ export default async function initSanity(
}

if (isCoreAppTemplate) {
const client = apiClient({requireUser: true, requireProject: false})
const organizations = await client.request({uri: '/organizations'})

const coreAppOrganizationId = await getOrganizationId(organizations)

return {
projectId: '',
displayName: '',
isFirstProject: false,
datasetName: '',
isFirstProject: false,
organizationId: coreAppOrganizationId,
}
}

Expand Down Expand Up @@ -1091,6 +1099,7 @@ export default async function initSanity(
dataset: datasetName,
projectId,
projectName: displayName || answers.projectName,
organizationId,
}

if (remoteTemplateInfo) {
Expand Down Expand Up @@ -1230,12 +1239,12 @@ export default async function initSanity(
}

async function getOrganizationId(organizations: ProjectOrganization[]) {
let organizationId = flags.organization
let orgId = flags.organization
if (unattended) {
return organizationId || undefined
return orgId || undefined
}

const shouldPrompt = organizations.length > 0 && !organizationId
const shouldPrompt = organizations.length > 0 && !orgId
if (shouldPrompt) {
debug(`User has ${organizations.length} organization(s), checking attach access`)
const withGrant = await getOrganizationsWithAttachGrant(organizations)
Expand All @@ -1261,26 +1270,26 @@ export default async function initSanity(
})

if (chosenOrg && chosenOrg !== 'none') {
organizationId = chosenOrg
orgId = chosenOrg
}
} else if (organizationId) {
debug(`User has defined organization flag explicitly (%s)`, organizationId)
} else if (orgId) {
debug(`User has defined organization flag explicitly (%s)`, orgId)
} else if (organizations.length === 0) {
debug('User has no organizations, skipping selection prompt')
}

return organizationId || undefined
return orgId || undefined
}

async function hasProjectAttachGrant(organizationId: string) {
async function hasProjectAttachGrant(orgId: string) {
const requiredGrantGroup = 'sanity.organization.projects'
const requiredGrant = 'attach'

const client = apiClient({requireProject: false, requireUser: true})
.clone()
.config({apiVersion: 'v2021-06-07'})

const grants = await client.request({uri: `organizations/${organizationId}/grants`})
const grants = await client.request({uri: `organizations/${orgId}/grants`})
const group: {grants: {name: string}[]}[] = grants[requiredGrantGroup] || []
return group.some(
(resource) =>
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export type CliStubbedYarn = (args: string[], options?: CliYarnOptions) => Promi

export interface CliApiConfig {
projectId?: string
organizationId?: string
dataset?: string
}

Expand Down

0 comments on commit 34c7aec

Please sign in to comment.