import * as p from '@clack/prompts'; import { ResponseError } from '@fbrc/sdk'; import { addDummyFiles } from './files'; import { createWorkspaces, deleteWorkspace, listWorkspaces, } from './workspaces'; const commands: Record< string, { label: string; description: string; action: () => Promise } > = { createWorkspaces: { label: 'Create workspaces', description: 'Will create 1 workspace with a random slug', action: createWorkspaces, }, listWorkspaces: { label: 'List workspaces', description: 'Will list all workspaces', action: listWorkspaces, }, deleteWorkspace: { label: 'Delete workspace', description: 'Will delete the first workspace', action: deleteWorkspace, }, addDummyFiles:{ label: 'Add dummy files', description: 'Will add dummy files to a random delegated workspace', action: addDummyFiles, }, exit: { label: 'Exit', description: 'Exit the program', action: () => { p.outro('Exiting...'); process.exit(0); }, }, }; async function main() { while (true) { p.intro('Fabric API Example'); const selectOptions = Object.entries(commands).map(([key, command]) => ({ label: command.label, value: key, hint: command.description, })); const selectedKey = await p.select({ message: 'Select a command:', options: selectOptions, }); if (!selectedKey) { p.outro('No command selected. Exiting...'); process.exit(0); } const selectedCommand = commands[selectedKey as keyof typeof commands]; if (!selectedCommand) { p.outro('Invalid command. Exiting...'); process.exit(1); } try { await selectedCommand.action(); } catch (error) { if (error instanceof ResponseError) { p.log.error( error.response.status + ' ' + error.response.statusText + '\n' + JSON.stringify(await error.response.json(), null, 2), ); } else { p.log.error( (error instanceof Error ? error.message : 'Unknown error') + '\n' + JSON.stringify(error, null, 2), ); } process.exit(1); } } } main();