projects.ts•2.17 kB
/**
* Projects command
*/
import chalk from 'chalk';
import { WhatapClient } from '../../core/client/WhatapClient';
import { requireAuth } from '../utils/session';
import { formatProjectsList, formatOutput, printSuccess, printError, printInfo } from '../utils/formatters';
interface ProjectsOptions {
format?: 'table' | 'json' | 'csv';
filter?: string;
}
/**
* Projects command handler
*/
export async function projectsCommand(options: ProjectsOptions): Promise<void> {
try {
printInfo('Fetching projects...');
// Require authentication
const authManager = await requireAuth();
// Create client
const client = new WhatapClient(authManager);
// Get projects
const projects = await client.getProjects();
// Filter by product type if specified
let filteredProjects = projects;
if (options.filter) {
const filterType = options.filter.toUpperCase();
filteredProjects = projects.filter(p =>
p.productType.toUpperCase() === filterType
);
if (filteredProjects.length === 0) {
console.log('');
console.log(chalk.yellow('⚠'), `No projects found with type: ${options.filter}`);
console.log('');
console.log(chalk.gray('Available types:'), [...new Set(projects.map(p => p.productType))].join(', '));
return;
}
}
console.log('');
printSuccess(`Found ${filteredProjects.length} project(s)`);
console.log('');
// Format and display
const format = (options.format || 'table') as 'table' | 'json' | 'csv';
if (format === 'table') {
console.log(formatProjectsList(filteredProjects, format));
} else {
console.log(formatOutput(filteredProjects, format));
}
console.log('');
// Show usage hint
if (format === 'table' && filteredProjects.length > 0) {
console.log(chalk.gray('To execute a query:'));
console.log(chalk.white(' whatap-mxql query'), chalk.cyan(filteredProjects[0].projectCode), chalk.gray('"<mxql>"'));
console.log('');
}
} catch (error: any) {
console.log('');
printError('Failed to fetch projects', error);
process.exit(1);
}
}