|
| 1 | +import { Command } from 'commander'; |
| 2 | +import { YavyApiClient } from '@/api/client'; |
| 3 | +import { needsInteractiveMode, runInteractiveFlow } from '@/prompts/project-create'; |
| 4 | +import { error, formatProjectCreated } from '@/utils'; |
| 5 | +import { buildCreateProjectPayload } from '@/commands/project/build-request'; |
| 6 | +import { resolveOrg } from '@/commands/project/resolve-org'; |
| 7 | +import type { CreateProjectOptions } from '@/commands/project/types'; |
| 8 | + |
| 9 | +export function createProjectCommand(): Command { |
| 10 | + return new Command('create') |
| 11 | + .description('Create a new documentation project') |
| 12 | + .option('--url <url>', 'Documentation URL (WebCrawl source)') |
| 13 | + .option('--github <owner/repo>', 'GitHub repository (e.g. laravel/docs)') |
| 14 | + .option('--org <slug>', 'Organization slug') |
| 15 | + .option('--name <name>', 'Project name (auto-generated if omitted)') |
| 16 | + .option('--public', 'Make project public (default)') |
| 17 | + .option('--private', 'Make project private') |
| 18 | + .option('--branch <branch>', 'GitHub branch override') |
| 19 | + .option('--docs-path <path>', 'GitHub docs path') |
| 20 | + .option('--no-sync', 'Skip initial auto-sync') |
| 21 | + .action(async (options: CreateProjectOptions) => { |
| 22 | + try { |
| 23 | + await executeCreateProject(options); |
| 24 | + } catch (err) { |
| 25 | + if (err instanceof Error && err.message === 'cancelled') { |
| 26 | + console.log('\nProject creation cancelled.'); |
| 27 | + return; |
| 28 | + } |
| 29 | + error(err instanceof Error ? err.message : String(err)); |
| 30 | + process.exit(1); |
| 31 | + } |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +export async function executeCreateProject(options: CreateProjectOptions): Promise<void> { |
| 36 | + const client = await YavyApiClient.create(); |
| 37 | + |
| 38 | + let resolvedOptions = options; |
| 39 | + |
| 40 | + if (needsInteractiveMode(options)) { |
| 41 | + resolvedOptions = await runInteractiveFlow(client, options); |
| 42 | + } |
| 43 | + |
| 44 | + if (!resolvedOptions.url && !resolvedOptions.github) { |
| 45 | + throw new Error('Either --url or --github is required.'); |
| 46 | + } |
| 47 | + |
| 48 | + if (resolvedOptions.url && resolvedOptions.github) { |
| 49 | + throw new Error('Provide either --url or --github, not both.'); |
| 50 | + } |
| 51 | + |
| 52 | + const projects = await client.listProjects(); |
| 53 | + const { slug: orgSlug, orgs } = await resolveOrg(projects, resolvedOptions.org); |
| 54 | + |
| 55 | + if (!orgSlug) { |
| 56 | + const slugList = orgs.map((o) => ` - ${o.slug} (${o.name})`).join('\n'); |
| 57 | + throw new Error(`Multiple organizations found. Please specify one with --org <slug>:\n${slugList}`); |
| 58 | + } |
| 59 | + |
| 60 | + const payload = buildCreateProjectPayload(resolvedOptions); |
| 61 | + const response = await client.createProject(orgSlug, payload); |
| 62 | + |
| 63 | + console.log(formatProjectCreated(response.data)); |
| 64 | +} |
0 commit comments