"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
async function update(tree) {
    devkit_1.logger.info('🔄 Updating root .gitignore to add migrations-*.json...');
    const gitignorePath = '.gitignore';
    if (!tree.exists(gitignorePath)) {
        devkit_1.logger.warn('⚠️ .gitignore file not found, creating one');
        tree.write(gitignorePath, '');
    }
    try {
        const currentContent = tree.read(gitignorePath, 'utf-8') || '';
        let updatedContent = currentContent;
        const entryRegex = /^migrations-\*\.json\s*$/gm;
        if (!entryRegex.test(updatedContent)) {
            if (!updatedContent.includes('# Nx migrations')) {
                updatedContent = updatedContent.trim() + '\n\n# Nx migrations\n';
            }
            if (!updatedContent.includes('migrations-*.json')) {
                updatedContent += 'migrations-*.json\n';
                devkit_1.logger.info('📝 Added migrations-*.json to .gitignore');
            }
            tree.write(gitignorePath, updatedContent);
            devkit_1.logger.info('✅ Successfully updated .gitignore');
        }
        else {
            devkit_1.logger.info('ℹ️ migrations-*.json already exists in .gitignore');
        }
    }
    catch (error) {
        const errorMessage = error instanceof Error ? error.message : 'Unknown error';
        devkit_1.logger.error(`❌ Failed to update .gitignore: ${errorMessage}`);
        throw new Error(`Failed to update .gitignore: ${errorMessage}`);
    }
}
