UNPKG

revit-cli

Version:

A scalable CLI tool for Revit communication and data manipulation

212 lines 7.75 kB
"use strict"; /** * List rooms command implementation * Handles room listing with various display options */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createListRoomsCommand = createListRoomsCommand; const commander_1 = require("commander"); const chalk_1 = __importDefault(require("chalk")); const room_service_js_1 = require("../../services/room-service.js"); /** * Creates the list-rooms command * @param getState - Function to get CLI state * @returns Commander command instance */ function createListRoomsCommand(getState) { const command = new commander_1.Command('rooms') .description('List all rooms in the Revit project') .option('-p, --extract-params', 'Extract and display additional parameters') .option('-s, --sort <field>', 'Sort by field (name, number, level, area, volume)', 'name') .option('--reverse', 'Reverse sort order') .option('--format <format>', 'Output format (table, json, csv)', 'table') .option('--limit <count>', 'Limit number of results', parseInt) .action(async (options) => { try { const state = await getState(); const roomService = new room_service_js_1.RoomService(state.revitConnector, state.logger); const rooms = await roomService.getAllRooms(); // Sort rooms let sortedRooms = sortRooms(rooms, options.sort, options.reverse); // Apply limit if specified if (options.limit && options.limit > 0) { sortedRooms = sortedRooms.slice(0, options.limit); state.logger.info(`Limited results to ${sortedRooms.length} rooms`); } // Display results based on format switch (options.format?.toLowerCase()) { case 'json': displayAsJson(sortedRooms); break; case 'csv': displayAsCsv(sortedRooms, options.extractParams); break; case 'table': default: displayAsTable(sortedRooms, options.extractParams); break; } } catch (error) { const state = await getState(); state.logger.error('List rooms command failed:', error); console.log(chalk_1.default.red('❌ Failed to list rooms')); process.exit(1); } }); return command; } /** * Sorts rooms by the specified field * @param rooms - Array of room data * @param sortField - Field to sort by * @param reverse - Whether to reverse the sort order * @returns Sorted array of rooms */ function sortRooms(rooms, sortField, reverse = false) { const sorted = [...rooms].sort((a, b) => { let aValue; let bValue; switch (sortField?.toLowerCase()) { case 'number': aValue = a.number; bValue = b.number; break; case 'level': aValue = a.level; bValue = b.level; break; case 'area': aValue = a.area || 0; bValue = b.area || 0; return reverse ? bValue - aValue : aValue - bValue; case 'volume': aValue = a.volume || 0; bValue = b.volume || 0; return reverse ? bValue - aValue : aValue - bValue; case 'department': aValue = a.department || ''; bValue = b.department || ''; break; case 'occupancy': aValue = a.occupancy || ''; bValue = b.occupancy || ''; break; case 'name': default: aValue = a.name; bValue = b.name; break; } // String comparison if (typeof aValue === 'string' && typeof bValue === 'string') { const result = aValue.localeCompare(bValue); return reverse ? -result : result; } // Fallback comparison if (aValue < bValue) return reverse ? 1 : -1; if (aValue > bValue) return reverse ? -1 : 1; return 0; }); return sorted; } /** * Displays rooms in table format * @param rooms - Array of room data * @param extractParams - Whether to include additional parameters */ function displayAsTable(rooms, extractParams) { console.log(`\nRooms (${rooms.length} total):\n`); rooms.forEach((room, index) => { console.log(`${index + 1}. ${room.name} (${room.number})`); console.log(` Level: ${room.level}`); if (room.area) console.log(` Area: ${room.area}`); if (room.volume) console.log(` Volume: ${room.volume}`); if (room.department) console.log(` Department: ${room.department}`); if (room.occupancy) console.log(` Occupancy: ${room.occupancy}`); if (room.comments) console.log(` Comments: ${room.comments}`); if (extractParams && room.parameters && Object.keys(room.parameters).length > 0) { console.log(` Additional Parameters:`); Object.entries(room.parameters).forEach(([key, value]) => { if (value !== null && value !== undefined && value !== '') { console.log(` ${key}: ${value}`); } }); } console.log(); // Empty line between rooms }); } /** * Displays rooms in JSON format * @param rooms - Array of room data */ function displayAsJson(rooms) { console.log(JSON.stringify(rooms, null, 2)); } /** * Displays rooms in CSV format * @param rooms - Array of room data * @param extractParams - Whether to include additional parameters */ function displayAsCsv(rooms, extractParams) { // Basic headers const headers = ['Name', 'Number', 'Level', 'Area', 'Volume', 'Department', 'Occupancy', 'Comments']; // Add parameter headers if extracting params const paramKeys = new Set(); if (extractParams) { rooms.forEach(room => { if (room.parameters) { Object.keys(room.parameters).forEach(key => paramKeys.add(key)); } }); headers.push(...Array.from(paramKeys)); } // Output headers console.log(headers.join(',')); // Output data rows rooms.forEach(room => { const row = [ escapeCsvValue(room.name), escapeCsvValue(room.number), escapeCsvValue(room.level), room.area || '', room.volume || '', escapeCsvValue(room.department || ''), escapeCsvValue(room.occupancy || ''), escapeCsvValue(room.comments || '') ]; // Add parameter values if (extractParams) { paramKeys.forEach(key => { const value = room.parameters?.[key]; row.push(escapeCsvValue(value?.toString() || '')); }); } console.log(row.join(',')); }); } /** * Escapes a value for CSV output * @param value - Value to escape * @returns Escaped CSV value */ function escapeCsvValue(value) { if (!value) return ''; // If value contains comma, quote, or newline, wrap in quotes and escape quotes if (value.includes(',') || value.includes('"') || value.includes('\n')) { return `"${value.replace(/"/g, '""')}"`; } return value; } //# sourceMappingURL=list-rooms.js.map