All files / rw-lazy-installer/src/commands check.ts

0% Statements 0/78
0% Branches 0/1
0% Functions 0/1
0% Lines 0/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                                                                                                                                                                                                       
import chalk from 'chalk';
import { ModManager } from '../services/mod-manager.js';
import type { CliOptions } from '../types/cli';
 
/**
 * Format version range for display
 */
function formatVersionRange(versions: string[]): string {
  if (!versions || versions.length === 0) return '';
 
  const sorted = [...versions].sort();
  if (sorted.length <= 2) {
    return sorted.join(', ');
  } else {
    return `${sorted[0]}-${sorted[sorted.length - 1]}`;
  }
}
 
/**
 * Check command - scans directory for installed mods and syncs with config
 */
export async function checkCommand(
  modManager: ModManager,
  installDir: string,
  _options: CliOptions
): Promise<void> {
  console.log(chalk.green`Checking`, chalk.yellow`${installDir}`, chalk.green`for installed mods\n`);
 
  try {
    // Scan directory for mods
    const scanResult = await modManager.scanDirectory(installDir);
 
    // Display found mods
    for (const found of scanResult.found) {
      const mod = modManager.findMod(found.name);
      if (!mod) continue;
 
      if (found.isNew) {
        console.log(chalk.green`\tAdding`, chalk.white`${mod.name}`, chalk.green`as new mod`);
      } else {
        console.log(chalk.green`\tFound`, chalk.white`${mod.name}`, chalk.green`as installed mod`);
      }
    }
 
    // Display missing mods (in config but not on disk)
    for (const missing of scanResult.missing) {
      console.log(
        chalk.red`\tMissing:`,
        chalk.white`${missing.name}`,
        chalk.gray`(in config but not found on disk)`
      );
    }
 
    // Display unknown directories
    for (const unknown of scanResult.unknown) {
      console.log(
        chalk.yellow`\tUnknown:`,
        chalk.white`${unknown}`,
        chalk.gray`(not a recognized mod)`
      );
    }
 
    // Sync configuration with disk state
    await modManager.syncConfigWithDisk(installDir);
 
    console.log(chalk.green`\nConfiguration synced with disk state`);
 
    // List all installed mods
    const installed = modManager.getInstalledMods();
    if (installed.length > 0) {
      console.log(chalk.green`\nInstalled mods:\n`);
 
      for (const installation of installed) {
        const mod = modManager.findMod(installation.name);
        if (!mod) continue;
 
        const versions = formatVersionRange(installation.supportedVersions || []);
        const displayPath = installation.directory.replace(installDir, '[mods]');
 
        if (mod.deprecated) {
          console.log(
            chalk.strikethrough.green`\t${mod.label.padEnd(40)}`,
            chalk.strikethrough.bold.white`${installation.name.padEnd(30)}`,
            chalk.strikethrough.bold.yellow`${versions.padEnd(20)}`,
            chalk.strikethrough.yellow`${displayPath.padEnd(40)}`,
            mod.remark ? chalk.gray` - ${mod.remark}` : ''
          );
        } else {
          console.log(
            chalk.green`\t${mod.label.padEnd(40)}`,
            chalk.bold.white`${installation.name.padEnd(30)}`,
            chalk.bold.yellow`${versions.padEnd(20)}`,
            chalk.yellow`${displayPath.padEnd(40)}`,
            mod.remark ? chalk.gray` - ${mod.remark}` : ''
          );
        }
      }
    } else {
      console.log(chalk.red`\tNo mods found!`);
    }
 
    console.log(chalk.green`\nInstall or update the mods with the ${chalk.bold.white`install`} or ${chalk.bold.white`update`} command.\n`);
 
  } catch (error) {
    console.error(chalk.red`Error:`, (error as Error).message);
    process.exit(1);
  }
}