|
| 1 | +import { |
| 2 | + CliOptions, |
| 3 | + createService, |
| 4 | + findRootTask, |
| 5 | + formatCliError, |
| 6 | +} from "./utils.js"; |
| 7 | +import { colors } from "./colors.js"; |
| 8 | +import { getBooleanFlag, parseArgs } from "./args.js"; |
| 9 | +import { |
| 10 | + createGitHubSyncServiceOrThrow, |
| 11 | + getGitHubIssueNumber, |
| 12 | + GitHubSyncService, |
| 13 | +} from "../core/github/index.js"; |
| 14 | +import { loadConfig } from "../core/config.js"; |
| 15 | + |
| 16 | +export async function exportCommand( |
| 17 | + args: string[], |
| 18 | + options: CliOptions, |
| 19 | +): Promise<void> { |
| 20 | + const { positional, flags } = parseArgs( |
| 21 | + args, |
| 22 | + { |
| 23 | + "dry-run": { hasValue: false }, |
| 24 | + help: { short: "h", hasValue: false }, |
| 25 | + }, |
| 26 | + "export", |
| 27 | + ); |
| 28 | + |
| 29 | + if (getBooleanFlag(flags, "help")) { |
| 30 | + console.log(`${colors.bold}dex export${colors.reset} - Export tasks to GitHub Issues (one-way, no sync) |
| 31 | +
|
| 32 | +${colors.bold}USAGE:${colors.reset} |
| 33 | + dex export <task-id>... # Export one or more tasks |
| 34 | + dex export --dry-run # Preview without creating issues |
| 35 | +
|
| 36 | +${colors.bold}ARGUMENTS:${colors.reset} |
| 37 | + <task-id>... One or more task IDs to export (required) |
| 38 | +
|
| 39 | +${colors.bold}OPTIONS:${colors.reset} |
| 40 | + --dry-run Show what would be exported without making changes |
| 41 | + -h, --help Show this help message |
| 42 | +
|
| 43 | +${colors.bold}DIFFERENCE FROM SYNC:${colors.reset} |
| 44 | + Unlike 'dex sync', export creates GitHub issues without saving |
| 45 | + the issue metadata back to the task. This is useful for sharing |
| 46 | + tasks externally without enabling bidirectional sync. |
| 47 | +
|
| 48 | +${colors.bold}REQUIREMENTS:${colors.reset} |
| 49 | + - Git repository with GitHub remote |
| 50 | + - GITHUB_TOKEN environment variable |
| 51 | +
|
| 52 | +${colors.bold}EXAMPLE:${colors.reset} |
| 53 | + dex export abc123 # Export single task |
| 54 | + dex export abc123 def456 # Export multiple tasks |
| 55 | + dex export abc123 --dry-run # Preview export |
| 56 | +`); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const taskIds = positional; |
| 61 | + const dryRun = getBooleanFlag(flags, "dry-run"); |
| 62 | + |
| 63 | + if (taskIds.length === 0) { |
| 64 | + console.error( |
| 65 | + `${colors.red}Error:${colors.reset} At least one task ID is required`, |
| 66 | + ); |
| 67 | + console.error(`Usage: dex export <task-id>...`); |
| 68 | + process.exit(1); |
| 69 | + } |
| 70 | + |
| 71 | + const config = loadConfig({ storagePath: options.storage.getIdentifier() }); |
| 72 | + |
| 73 | + let syncService: GitHubSyncService; |
| 74 | + try { |
| 75 | + syncService = createGitHubSyncServiceOrThrow(config.sync?.github); |
| 76 | + } catch (err) { |
| 77 | + console.error(formatCliError(err)); |
| 78 | + process.exit(1); |
| 79 | + } |
| 80 | + |
| 81 | + const service = createService(options); |
| 82 | + const repo = syncService.getRepo(); |
| 83 | + const store = await options.storage.readAsync(); |
| 84 | + |
| 85 | + let exportedCount = 0; |
| 86 | + let skippedCount = 0; |
| 87 | + |
| 88 | + for (const taskId of taskIds) { |
| 89 | + try { |
| 90 | + const task = await service.get(taskId); |
| 91 | + if (!task) { |
| 92 | + console.error( |
| 93 | + `${colors.red}Error:${colors.reset} Task ${taskId} not found`, |
| 94 | + ); |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + // Find root task if this is a subtask |
| 99 | + const rootTask = await findRootTask(service, task); |
| 100 | + |
| 101 | + // Check if already synced |
| 102 | + if (getGitHubIssueNumber(rootTask)) { |
| 103 | + console.log( |
| 104 | + `${colors.yellow}Skipped${colors.reset} ${colors.bold}${rootTask.id}${colors.reset}: already synced to GitHub`, |
| 105 | + ); |
| 106 | + skippedCount++; |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + if (dryRun) { |
| 111 | + console.log( |
| 112 | + `Would export to ${colors.cyan}${repo.owner}/${repo.repo}${colors.reset}:`, |
| 113 | + ); |
| 114 | + console.log( |
| 115 | + ` [create] ${colors.bold}${rootTask.id}${colors.reset}: ${rootTask.name}`, |
| 116 | + ); |
| 117 | + exportedCount++; |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + // Export the task (create issue but don't save metadata) |
| 122 | + const result = await syncService.syncTask(rootTask, store); |
| 123 | + |
| 124 | + if (result) { |
| 125 | + console.log( |
| 126 | + `${colors.green}Exported${colors.reset} task ${colors.bold}${rootTask.id}${colors.reset} to ${colors.cyan}${repo.owner}/${repo.repo}${colors.reset}`, |
| 127 | + ); |
| 128 | + console.log(` ${colors.dim}${result.github.issueUrl}${colors.reset}`); |
| 129 | + exportedCount++; |
| 130 | + } |
| 131 | + } catch (err) { |
| 132 | + console.error( |
| 133 | + `${colors.red}Error${colors.reset} exporting ${taskId}: ${formatCliError(err)}`, |
| 134 | + ); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Summary for multiple tasks |
| 139 | + if (taskIds.length > 1) { |
| 140 | + const parts = []; |
| 141 | + if (exportedCount > 0) { |
| 142 | + parts.push( |
| 143 | + `${exportedCount} ${dryRun ? "would be exported" : "exported"}`, |
| 144 | + ); |
| 145 | + } |
| 146 | + if (skippedCount > 0) { |
| 147 | + parts.push(`${skippedCount} skipped`); |
| 148 | + } |
| 149 | + if (parts.length > 0) { |
| 150 | + console.log(`\n${parts.join(", ")}`); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments