import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { getPackageManagerScriptCommand } from '../package-manager.js';
export async function postInitScript(environment, options) {
    const packageJsonPath = resolve(options.targetDir, 'package.json');
    if (!environment.exists(packageJsonPath)) {
        environment.warn('Warning', 'No package.json found, skipping post-create-init script');
        return;
    }
    try {
        const packageJsonContent = readFileSync(packageJsonPath, 'utf-8');
        const packageJson = JSON.parse(packageJsonContent);
        if (!packageJson.scripts || !packageJson.scripts['post-create-init']) {
            // No post-create-init script found, skip silently
            return;
        }
        environment.startStep({
            id: 'post-init-script',
            type: 'command',
            message: 'Running post-create-init script...',
        });
        const { command, args } = getPackageManagerScriptCommand(options.packageManager, ['post-create-init']);
        await environment.execute(command, args, options.targetDir, {
            inherit: true,
        });
        environment.finishStep('post-init-script', 'Post-cta-init script complete');
    }
    catch (error) {
        environment.error(`Failed to run post-create-init script: ${error instanceof Error ? error.message : String(error)}`);
    }
}
