-
Notifications
You must be signed in to change notification settings - Fork 0
feat: PR-7 add business logic handlers and CLI entry point #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0a15270
feat: add business logic handlers and CLI entry point
mynameistito d452418
fix: resolve typecheck errors in AppState, ProgressCallback, and SUBC…
mynameistito c8c2ab8
style: apply lint formatting fixes
mynameistito d753a4e
fix: address CodeRabbit review findings
mynameistito 1fe25fa
fix: inspect export Results, mkdir before preset export, propagate se…
mynameistito aa6d48a
fix: inspect deletePreset/savePreset Results, remove unsafe token cas…
mynameistito 4423c4d
fix issues
mynameistito 244d253
fix: inspect savePreset Result and separate load-error handling in ha…
mynameistito 90d81a3
fix: handle export errors in bulk preset run and validate export format
mynameistito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { mkdir } from "node:fs/promises"; | ||
| import { select, spinner, text } from "@clack/prompts"; | ||
| import type { Result } from "better-result"; | ||
| import { handleCancel } from "@/cli/prompts.ts"; | ||
| import type { collateResults } from "@/collate.ts"; | ||
| import type { ExportError } from "@/errors.ts"; | ||
| import { | ||
| exportEmbedsCsv, | ||
| exportFieldsCsv, | ||
| exportJson, | ||
| exportMessagesCsv, | ||
| } from "@/export.ts"; | ||
| import { OUTPUT_DIR } from "@/paths.ts"; | ||
|
|
||
| const VALID_EXPORT_FORMATS = new Set([ | ||
| "json", | ||
| "csv-messages", | ||
| "csv-embeds", | ||
| "csv-fields", | ||
| "all", | ||
| ]); | ||
|
|
||
| export const exportNonInteractive = async ( | ||
| data: ReturnType<typeof collateResults>, | ||
| guildId: string, | ||
| format: string, | ||
| outputDir?: string | ||
| ): Promise<string> => { | ||
| if (!VALID_EXPORT_FORMATS.has(format)) { | ||
| throw new Error( | ||
| `Invalid export format: "${format}". Valid formats: ${[...VALID_EXPORT_FORMATS].join(", ")}` | ||
| ); | ||
| } | ||
|
|
||
| const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); | ||
| const dir = outputDir ?? `${OUTPUT_DIR}/${guildId}-${timestamp}`; | ||
|
|
||
| await mkdir(dir, { recursive: true }); | ||
|
|
||
| const exports: Promise<Result<void, ExportError>>[] = []; | ||
|
|
||
| if (format === "json" || format === "all") { | ||
| exports.push(exportJson(data, `${dir}/data.json`)); | ||
| } | ||
| if (format === "csv-messages" || format === "all") { | ||
| exports.push(exportMessagesCsv(data, `${dir}/messages.csv`)); | ||
| } | ||
| if (format === "csv-embeds" || format === "all") { | ||
| exports.push(exportEmbedsCsv(data, `${dir}/embeds.csv`)); | ||
| } | ||
| if (format === "csv-fields" || format === "all") { | ||
| exports.push(exportFieldsCsv(data, `${dir}/fields.csv`)); | ||
| } | ||
|
|
||
| const results = await Promise.all(exports); | ||
| const failed = results.find((r) => r.isErr()); | ||
| if (failed) { | ||
| throw failed.error; | ||
| } | ||
|
|
||
| return dir; | ||
| }; | ||
|
|
||
| export const handleExport = async ( | ||
| data: ReturnType<typeof collateResults>, | ||
| guildId: string | ||
| ): Promise<void> => { | ||
| const format = await select({ | ||
| message: "Export format:", | ||
| options: [ | ||
| { value: "json", label: "JSON (full data)" }, | ||
| { value: "csv-messages", label: "CSV - Messages" }, | ||
| { value: "csv-embeds", label: "CSV - Embeds" }, | ||
| { value: "csv-fields", label: "CSV - Extracted Fields" }, | ||
| { value: "all", label: "All of the above" }, | ||
| { value: "none", label: "None (just view summary)" }, | ||
| ], | ||
| }); | ||
| handleCancel(format); | ||
|
|
||
| if (format === "none") { | ||
| return; | ||
| } | ||
|
|
||
| const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); | ||
| const defaultDir = `${OUTPUT_DIR}/${guildId}-${timestamp}`; | ||
|
|
||
| const outputDir = await text({ | ||
|
mynameistito marked this conversation as resolved.
mynameistito marked this conversation as resolved.
|
||
| message: "Output directory:", | ||
| initialValue: defaultDir, | ||
| }); | ||
| handleCancel(outputDir); | ||
|
|
||
| const dir = (outputDir as string).trim(); | ||
|
|
||
| const s = spinner(); | ||
| s.start("Exporting..."); | ||
|
|
||
| // Ensure directory exists | ||
| await mkdir(dir, { recursive: true }); | ||
|
|
||
| const exports: Promise<Result<void, ExportError>>[] = []; | ||
|
|
||
| if (format === "json" || format === "all") { | ||
| exports.push(exportJson(data, `${dir}/data.json`)); | ||
| } | ||
| if (format === "csv-messages" || format === "all") { | ||
| exports.push(exportMessagesCsv(data, `${dir}/messages.csv`)); | ||
| } | ||
| if (format === "csv-embeds" || format === "all") { | ||
| exports.push(exportEmbedsCsv(data, `${dir}/embeds.csv`)); | ||
| } | ||
| if (format === "csv-fields" || format === "all") { | ||
| exports.push(exportFieldsCsv(data, `${dir}/fields.csv`)); | ||
| } | ||
|
|
||
| const results = await Promise.all(exports); | ||
| const failed = results.find((r) => r.isErr()); | ||
| if (failed) { | ||
| s.stop(`Export failed: ${failed.error.message}`); | ||
| return; | ||
| } | ||
|
|
||
| s.stop(`Exported to ${dir}/`); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.