"use strict";
/**
 * Migration: Post-cleanup Empty Directories
 *
 * This migration runs after all reorganize migrations to clean up any
 * empty directories left behind after moving packages/applications.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
const child_process_1 = require("child_process");
/**
 * Clean up empty directories in unisphere folder
 */
function cleanupEmptyDirectories() {
    const foldersToClean = [
        'unisphere/packages',
        'unisphere/applications',
        'unisphere/runtimes',
    ];
    let cleaned = false;
    for (const folder of foldersToClean) {
        try {
            const result = (0, child_process_1.execSync)(`find ${folder} -type d -empty 2>/dev/null | head -20 || true`, { stdio: 'pipe', encoding: 'utf-8' });
            if (result.trim()) {
                devkit_1.logger.info(`  Found empty directories in ${folder}:`);
                result
                    .trim()
                    .split('\n')
                    .forEach((dir) => {
                    if (dir)
                        devkit_1.logger.info(`    - ${dir}`);
                });
            }
            (0, child_process_1.execSync)(`find ${folder} -type d -empty -delete 2>/dev/null || true`, {
                stdio: 'pipe',
            });
            cleaned = true;
        }
        catch {
            // Ignore errors - folder might not exist
        }
    }
    if (cleaned) {
        devkit_1.logger.info('  Empty directories removed');
    }
}
/**
 * Main migration function
 */
async function update(tree) {
    devkit_1.logger.info('');
    devkit_1.logger.info('Post-cleanup: Removing empty directories after reorganization...');
    devkit_1.logger.info('');
    // Return callback to run after Tree changes are written
    return () => {
        cleanupEmptyDirectories();
        devkit_1.logger.info('');
        devkit_1.logger.info('Post-cleanup completed');
        devkit_1.logger.info('');
    };
}
