"use strict";
/**
 * Migration: Update .gitignore to exclude user-scripts directory
 *
 * Adds user-scripts/ to .gitignore to prevent user scripts
 * from being committed to the repository.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
async function update(tree) {
    devkit_1.logger.info('🔄 Updating .gitignore to exclude user-scripts/...');
    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 userScriptsRegex = /^user-scripts\/\s*$/gm;
        if (!userScriptsRegex.test(updatedContent)) {
            if (!updatedContent.includes('user-scripts/')) {
                updatedContent = updatedContent.trim() + '\nuser-scripts/\n';
                devkit_1.logger.info('📝 Added user-scripts/ to .gitignore');
            }
            tree.write(gitignorePath, updatedContent);
            devkit_1.logger.info('✅ Successfully updated .gitignore');
        }
        else {
            devkit_1.logger.info('ℹ️ user-scripts/ 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}`);
    }
}
