Files
orca/src/cli/worktree-project-target.ts
T
2026-06-11 12:33:58 -07:00

71 lines
2.4 KiB
TypeScript

import type { ProjectHostSetup } from '../shared/types'
import type { RuntimeClient } from './runtime-client'
import { RuntimeClientError } from './runtime-client'
function getPresentStringFlag(
flags: Map<string, string | boolean>,
name: string
): string | undefined {
if (!flags.has(name)) {
return undefined
}
const value = flags.get(name)
if (typeof value === 'string' && value.length > 0) {
return value
}
throw new RuntimeClientError('invalid_argument', `Missing value for --${name}`)
}
export function hasWorkspaceProjectTarget(flags: Map<string, string | boolean>): boolean {
return flags.has('project') || flags.has('host') || flags.has('project-host-setup')
}
export function assertWorkspaceTargetFlagsCompatible(flags: Map<string, string | boolean>): void {
const hasProjectTarget = hasWorkspaceProjectTarget(flags)
if (flags.has('repo') && hasProjectTarget) {
throw new RuntimeClientError(
'invalid_argument',
'Choose either --repo or project target flags, not both.'
)
}
if (flags.has('host') && !flags.has('project') && !flags.has('project-host-setup')) {
throw new RuntimeClientError(
'invalid_argument',
'--host requires --project unless --project-host-setup is provided.'
)
}
}
export async function resolveProjectCreateRepoSelector(
flags: Map<string, string | boolean>,
client: RuntimeClient
): Promise<string | undefined> {
const projectHostSetupId = getPresentStringFlag(flags, 'project-host-setup')
const projectId = getPresentStringFlag(flags, 'project')
const hostId = getPresentStringFlag(flags, 'host')
if (!projectHostSetupId && !projectId && !hostId) {
return undefined
}
const result = await client.call<{ setups: ProjectHostSetup[] }>('projectHostSetup.list')
const setup = result.result.setups.find((candidate) => {
if (candidate.setupState !== 'ready') {
return false
}
if (projectHostSetupId) {
return candidate.id === projectHostSetupId
}
return (
candidate.projectId === projectId && (hostId === undefined || candidate.hostId === hostId)
)
})
if (!setup) {
throw new RuntimeClientError(
'invalid_argument',
projectHostSetupId
? `Project host setup is not ready or was not found: ${projectHostSetupId}`
: `Project is not set up on the selected host: ${projectId}${hostId ? ` on ${hostId}` : ''}`
)
}
return `id:${setup.repoId}`
}