/** * Profile Helper Utility * * Standardized profile loading for all numerology tools. * Import this to add profile support with just a few lines. * * Usage: * import { parseProfileOrDirect } from './profile-helper'; * * const { name, birthdate } = parseProfileOrDirect(args, { * toolName: 'optimal-days', * requiresBirthdate: true, * requiresName: false * }); */ import { loadProfile } from './profile-manager'; export interface ParseOptions { toolName: string; requiresBirthdate?: boolean; requiresName?: boolean; } export interface ParsedData { name: string; birthdate: string; } /** * Parse command line args for either --profile or direct --name/--birthdate * * @param args - process.argv.slice(2) * @param options - Configuration options * @returns Object with name and birthdate * @throws Error if required data is missing */ export function parseProfileOrDirect(args: string[], options: ParseOptions): ParsedData { let name = ''; let birthdate = ''; let profileId = ''; // Parse arguments for (let i = 0; i < args.length; i++) { if ((args[i] === '--profile' || args[i] === '-p') && args[i + 1]) { profileId = args[i + 1]; i++; } else if ((args[i] === '--name' || args[i] === '-n') && args[i + 1]) { name = args[i + 1]; i++; } else if ((args[i] === '--birthdate' || args[i] === '-b') && args[i + 1]) { birthdate = args[i + 1]; i++; } } // Load profile if specified if (profileId) { const profile = loadProfile(profileId); if (!profile) { console.error(`Error: Profile '${profileId}' not found`); console.error('List profiles with: bun profile.ts list'); process.exit(1); } name = profile.name; birthdate = profile.birthdate; console.log(`Using profile '${profileId}': ${name} (${birthdate})`); } // Validate required fields const requiresName = options.requiresName !== false; // default true const requiresBirthdate = options.requiresBirthdate !== false; // default true if (requiresName && !name) { console.error(`Error: ${options.toolName} requires --name (or --profile)`); console.error('Try: bun --help'); process.exit(1); } if (requiresBirthdate && !birthdate) { console.error(`Error: ${options.toolName} requires --birthdate (or --profile)`); console.error('Try: bun --help'); process.exit(1); } return { name, birthdate }; } /** * Add profile flag to help text * * @param existingHelp - Existing help text with --name and --birthdate * @returns Updated help text with --profile option */ export function addProfileToHelp(existingHelp: string): string { // Add profile option to OPTIONS section let updated = existingHelp.replace( /(OPTIONS:|Options:)\s*\n/i, '$1\n -p, --profile ID Use saved profile\n' ); // Update required flags to say "or --profile" updated = updated.replace( /(\s+--name.*)\[required\]/gi, '$1[required if no profile]' ); updated = updated.replace( /(\s+--birthdate.*)\[required\]/gi, '$1[required if no profile]' ); // Add profile example if (updated.includes('EXAMPLES:') || updated.includes('Examples:')) { updated = updated.replace( /(EXAMPLES?:.*?\n)/i, '$1 # With profile\n bun .ts --profile john\n\n' ); } return updated; }