"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderTemplate = renderTemplate;
exports.createTemplateData = createTemplateData;
const ejs_1 = __importDefault(require("ejs"));
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const glob_1 = require("glob");
async function renderTemplate(templateDir, targetDir, data) {
    // 确保目标目录存在
    await fs_extra_1.default.ensureDir(targetDir);
    // 获取所有模板文件
    const files = await (0, glob_1.glob)('**/*.ejs', {
        cwd: templateDir,
        nodir: true,
        dot: true,
    });
    for (const file of files) {
        const srcPath = path_1.default.join(templateDir, file);
        const content = await fs_extra_1.default.readFile(srcPath, 'utf-8');
        // 渲染模板
        const rendered = ejs_1.default.render(content, data, {
            filename: srcPath,
        });
        // 计算目标路径（移除 .ejs 扩展名）
        const targetPath = path_1.default.join(targetDir, file.replace(/\.ejs$/, ''));
        // 确保目标目录存在
        await fs_extra_1.default.ensureDir(path_1.default.dirname(targetPath));
        // 写入文件
        await fs_extra_1.default.writeFile(targetPath, rendered, 'utf-8');
    }
    // 复制非模板文件
    const allFiles = await (0, glob_1.glob)('**/*', {
        cwd: templateDir,
        nodir: true,
        dot: true,
    });
    for (const file of allFiles) {
        if (!file.endsWith('.ejs')) {
            const srcPath = path_1.default.join(templateDir, file);
            const targetPath = path_1.default.join(targetDir, file);
            await fs_extra_1.default.ensureDir(path_1.default.dirname(targetPath));
            await fs_extra_1.default.copy(srcPath, targetPath);
        }
    }
    // 确保空目录也被创建（包括 services 和 local-lib）
    const ensureAllDirs = (srcDir, destDir) => {
        fs_extra_1.default.ensureDirSync(destDir);
        const entries = fs_extra_1.default.readdirSync(srcDir, { withFileTypes: true });
        for (const entry of entries) {
            if (entry.isDirectory()) {
                const srcPath = path_1.default.join(srcDir, entry.name);
                const destPath = path_1.default.join(destDir, entry.name);
                ensureAllDirs(srcPath, destPath);
            }
        }
    };
    ensureAllDirs(templateDir, targetDir);
    // 显式创建 services 和 local-lib 目录，确保它们一定存在
    await fs_extra_1.default.ensureDir(path_1.default.join(targetDir, 'src', 'services'));
    await fs_extra_1.default.ensureDir(path_1.default.join(targetDir, 'src', 'local-lib'));
    // 删除所有 .gitkeep 文件
    const gitkeepFiles = await (0, glob_1.glob)('**/.gitkeep', {
        cwd: targetDir,
        nodir: true,
        dot: true,
    });
    for (const gitkeepFile of gitkeepFiles) {
        await fs_extra_1.default.remove(path_1.default.join(targetDir, gitkeepFile));
    }
}
function createTemplateData(projectName, projectVersionId, authorization, options = {}) {
    return {
        projectName,
        projectVersionId,
        authorization,
        description: options.description || '',
        author: options.author || '',
        isSimple: options.isSimple ?? false,
        features: options.features || {
            sharedLib: false,
            redis: false,
            mq: false,
            uts: false,
        },
    };
}
//# sourceMappingURL=template.js.map