mirror of
https://github.com/tiennm99/ccs.git
synced 2026-07-16 04:18:05 +00:00
Issue #1161. Sweeps 127 files to import from src/config/config-loader-facade.ts instead of unified-config-loader or utils/config-manager directly. WRITE callers (32 files): replaced raw saveUnifiedConfig / mutateUnifiedConfig / updateUnifiedConfig calls with the facade's cache-coherent wrappers saveConfig / mutateConfig / updateConfig. This fixes a latent stale-cache window where direct writes through the underlying loader bypassed the facade's memoization. READ callers (95 files): mechanical import-path migration only — function names unchanged because the facade re-exports them. No behavior change. Also updated: - tests/unit/utils/browser/browser-setup.test.ts (DI interface rename) - src/management/checks/image-analysis-check.ts (dynamic import rename) - src/web-server/health-service.ts (dynamic require rename) - src/ccs.ts (path prefix fix from sweep script) After sweep: zero raw write callers remain outside src/config/. Direct imports of config-manager remain only for symbols not in the facade (getConfigPath, getCcsDirSource, etc). Behavior unchanged; full suite passes 1824/1824. Out of scope: switching loadOrCreateUnifiedConfig() callers to getCachedConfig() — needs per-callsite cache-safety analysis. Tracked as follow-up. Refs #1161
160 lines
4.5 KiB
TypeScript
160 lines
4.5 KiB
TypeScript
/**
|
|
* Migrate Command
|
|
*
|
|
* CLI command to migrate from v1 (JSON) to v2 (YAML) config format.
|
|
*
|
|
* Usage:
|
|
* ccs migrate - Run migration
|
|
* ccs migrate --dry-run - Preview migration without changes
|
|
* ccs migrate --rollback <path> - Restore from backup
|
|
* ccs migrate --list-backups - List available backups
|
|
*/
|
|
|
|
import {
|
|
migrate,
|
|
rollback,
|
|
needsMigration,
|
|
getBackupDirectories,
|
|
} from '../config/migration-manager';
|
|
|
|
import { initUI, ok, fail, info, warn, infoBox, dim } from '../utils/ui';
|
|
import { hasUnifiedConfig } from '../config/config-loader-facade';
|
|
|
|
export async function handleMigrateCommand(args: string[]): Promise<void> {
|
|
await initUI();
|
|
|
|
// Handle --list-backups
|
|
if (args.includes('--list-backups')) {
|
|
listBackups();
|
|
return;
|
|
}
|
|
|
|
// Handle --rollback
|
|
if (args.includes('--rollback')) {
|
|
const rollbackIndex = args.indexOf('--rollback');
|
|
const backupPath = args[rollbackIndex + 1];
|
|
|
|
if (!backupPath) {
|
|
console.error(fail('Error: --rollback requires backup path'));
|
|
console.log(info('Usage: ccs migrate --rollback <backup-path>'));
|
|
console.log(info('Use --list-backups to see available backups'));
|
|
process.exit(1);
|
|
}
|
|
|
|
await handleRollback(backupPath);
|
|
return;
|
|
}
|
|
|
|
// Check if already migrated
|
|
if (hasUnifiedConfig() && !needsMigration()) {
|
|
console.log(info('Already using unified config format (config.yaml)'));
|
|
return;
|
|
}
|
|
|
|
// Check if migration is needed
|
|
if (!needsMigration()) {
|
|
console.log(info('No migration needed - no legacy config found'));
|
|
return;
|
|
}
|
|
|
|
// Handle --dry-run
|
|
const dryRun = args.includes('--dry-run');
|
|
|
|
if (dryRun) {
|
|
console.log(info('Dry run - no changes will be made'));
|
|
console.log('');
|
|
}
|
|
|
|
const result = await migrate(dryRun);
|
|
|
|
if (result.success) {
|
|
console.log('');
|
|
if (dryRun) {
|
|
console.log(infoBox('Dry run - migration preview (no changes made)'));
|
|
} else {
|
|
console.log(infoBox('Migrated to unified config (config.yaml)', 'SUCCESS'));
|
|
}
|
|
|
|
if (result.backupPath && !dryRun) {
|
|
console.log(` Backup: ${result.backupPath}`);
|
|
}
|
|
console.log(` Items: ${result.migratedFiles.length} migrated`);
|
|
|
|
if (result.warnings.length > 0) {
|
|
for (const warning of result.warnings) {
|
|
console.log(warn(warning));
|
|
}
|
|
}
|
|
|
|
if (dryRun) {
|
|
console.log(dim(' Run without --dry-run to apply changes'));
|
|
} else {
|
|
console.log(` Rollback: ccs migrate --rollback ${result.backupPath}`);
|
|
}
|
|
console.log('');
|
|
} else {
|
|
console.error(fail(`Migration failed: ${result.error}`));
|
|
|
|
if (result.migratedFiles.length > 0) {
|
|
console.log('');
|
|
console.log(' Partially migrated:');
|
|
result.migratedFiles.forEach((f) => console.log(` - ${f}`));
|
|
}
|
|
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
async function handleRollback(backupPath: string): Promise<void> {
|
|
console.log(info(`Rolling back from: ${backupPath}`));
|
|
console.log('');
|
|
|
|
const success = await rollback(backupPath);
|
|
|
|
if (success) {
|
|
console.log(ok('Rollback complete'));
|
|
console.log(info('Legacy config restored'));
|
|
} else {
|
|
console.error(fail('Rollback failed'));
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
function listBackups(): void {
|
|
const backups = getBackupDirectories();
|
|
|
|
if (backups.length === 0) {
|
|
console.log(info('No backup directories found'));
|
|
return;
|
|
}
|
|
|
|
console.log(info('Available backups (most recent first):'));
|
|
console.log('');
|
|
backups.forEach((backup, index) => {
|
|
console.log(` ${index + 1}. ${backup}`);
|
|
});
|
|
console.log('');
|
|
console.log(info('To rollback: ccs migrate --rollback <backup-path>'));
|
|
}
|
|
|
|
/**
|
|
* Print help for migrate command.
|
|
*/
|
|
export function printMigrateHelp(): void {
|
|
console.log('Usage: ccs migrate [options]');
|
|
console.log('');
|
|
console.log('Migrate from legacy JSON config to unified YAML format.');
|
|
console.log('');
|
|
console.log('Options:');
|
|
console.log(' --dry-run Preview migration without making changes');
|
|
console.log(' --rollback PATH Restore from backup directory');
|
|
console.log(' --list-backups List available backup directories');
|
|
console.log(' --help Show this help message');
|
|
console.log('');
|
|
console.log('Examples:');
|
|
console.log(' ccs migrate # Run migration');
|
|
console.log(' ccs migrate --dry-run # Preview changes');
|
|
console.log(' ccs migrate --list-backups # List backups');
|
|
console.log(' ccs migrate --rollback ~/.ccs/backup-v1-2025-01-15');
|
|
}
|