|
| 1 | +import { log } from "@clack/prompts"; |
| 2 | +import { Command } from "commander"; |
| 3 | +import JSON5 from "json5"; |
| 4 | +import type { CLIContext } from "@/cli/types.js"; |
| 5 | +import { runCommand, runTask } from "@/cli/utils/index.js"; |
| 6 | +import type { RunCommandResult } from "@/cli/utils/runCommand.js"; |
| 7 | +import { InvalidInputError } from "@/core/errors.js"; |
| 8 | +import { createRecord } from "@/core/resources/entity/index.js"; |
| 9 | +import { readTextFile } from "@/core/utils/fs.js"; |
| 10 | + |
| 11 | +interface CreateRecordCommandOptions { |
| 12 | + data?: string; |
| 13 | + file?: string; |
| 14 | +} |
| 15 | + |
| 16 | +async function parseRecordData( |
| 17 | + options: CreateRecordCommandOptions, |
| 18 | +): Promise<Record<string, unknown>> { |
| 19 | + if (options.data) { |
| 20 | + try { |
| 21 | + return JSON5.parse(options.data); |
| 22 | + } catch { |
| 23 | + throw new InvalidInputError( |
| 24 | + "Invalid JSON in --data flag. Provide valid JSON.", |
| 25 | + { |
| 26 | + hints: [ |
| 27 | + { |
| 28 | + message: |
| 29 | + 'Example: --data \'{"name": "John", "email": "john@example.com"}\'', |
| 30 | + }, |
| 31 | + ], |
| 32 | + }, |
| 33 | + ); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if (options.file) { |
| 38 | + const content = await readTextFile(options.file); |
| 39 | + try { |
| 40 | + return JSON5.parse(content); |
| 41 | + } catch { |
| 42 | + throw new InvalidInputError( |
| 43 | + `Invalid JSON in file ${options.file}. Provide a valid JSON/JSONC file.`, |
| 44 | + ); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + throw new InvalidInputError( |
| 49 | + "Provide record data with --data or --file flag", |
| 50 | + { |
| 51 | + hints: [ |
| 52 | + { |
| 53 | + message: |
| 54 | + 'Example: --data \'{"name": "John"}\' or --file record.json', |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +async function createRecordAction( |
| 62 | + entityName: string, |
| 63 | + options: CreateRecordCommandOptions, |
| 64 | +): Promise<RunCommandResult> { |
| 65 | + const data = await parseRecordData(options); |
| 66 | + |
| 67 | + const record = await runTask( |
| 68 | + `Creating ${entityName} record...`, |
| 69 | + async () => { |
| 70 | + return await createRecord(entityName, data); |
| 71 | + }, |
| 72 | + { |
| 73 | + successMessage: `Created ${entityName} record`, |
| 74 | + errorMessage: `Failed to create ${entityName} record`, |
| 75 | + }, |
| 76 | + ); |
| 77 | + |
| 78 | + log.success(`Record created with ID: ${record.id}`); |
| 79 | + console.log(JSON.stringify(record, null, 2)); |
| 80 | + |
| 81 | + return {}; |
| 82 | +} |
| 83 | + |
| 84 | +export function getRecordsCreateCommand(context: CLIContext): Command { |
| 85 | + return new Command("create") |
| 86 | + .description("Create a new entity record") |
| 87 | + .argument("<entity-name>", "Name of the entity (e.g. Users, Products)") |
| 88 | + .option("-d, --data <json>", "JSON object with record data") |
| 89 | + .option("--file <path>", "Read record data from a JSON/JSONC file") |
| 90 | + .action( |
| 91 | + async (entityName: string, options: CreateRecordCommandOptions) => { |
| 92 | + await runCommand( |
| 93 | + () => createRecordAction(entityName, options), |
| 94 | + { requireAuth: true }, |
| 95 | + context, |
| 96 | + ); |
| 97 | + }, |
| 98 | + ); |
| 99 | +} |
0 commit comments