"use strict";
/**
 * Migration: Replace cicd.yml workflow
 *
 * Replaces .github/workflows/cicd.yml with updated version:
 * - Adds pull-requests: write permission
 * - Removes legacy Cache npm step (uses actions/setup-node cache instead)
 * - Adds cache: npm and cache-dependency-path to Setup Node.js step
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
const fs_1 = require("fs");
const path_1 = require("path");
async function update(tree) {
    devkit_1.logger.info('🔄 Updating GitHub CICD workflow for v4.14.0');
    // Ensure .github/workflows directory exists
    if (!tree.exists('.github')) {
        tree.write('.github/.gitkeep', '');
        devkit_1.logger.info('✅ Created .github directory');
    }
    if (!tree.exists('.github/workflows')) {
        tree.write('.github/workflows/.gitkeep', '');
        devkit_1.logger.info('✅ Created .github/workflows directory');
    }
    try {
        const workflowPath = '.github/workflows/cicd.yml';
        const templatePath = (0, path_1.join)(__dirname, 'templates', 'cicd.template');
        const templateContent = (0, fs_1.readFileSync)(templatePath, 'utf-8');
        const workflowExists = tree.exists(workflowPath);
        tree.write(workflowPath, templateContent);
        if (workflowExists) {
            devkit_1.logger.info(`✅ Updated ${workflowPath}`);
        }
        else {
            devkit_1.logger.info(`✅ Created ${workflowPath}`);
        }
    }
    catch (error) {
        devkit_1.logger.error(`❌ Failed to update GitHub CICD workflow: ${error?.message || 'Unknown error'}`);
        throw error;
    }
}
