"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceFileName = exports.customizeAppInfo = void 0;
const fs = require("fs-extra");
const path = require("path");
const Handlebars = require("handlebars");
const { readdirSync, readFileSync, writeFileSync, renameSync } = fs;
const customizeTool = (file, proName) => {
    const compName = proName.replace(proName[0], proName[0].toLowerCase());
    const tpl = readFileSync(file, 'utf-8');
    const template = Handlebars.compile(tpl);
    const data = {
        NAME: compName,
        COMPONENT_NAME: proName,
    };
    const result = template(data);
    writeFileSync(file, result, { encoding: 'utf8' });
};
// 递归转化 模板中的占位符
const customizeAppInfo = (root, proName) => {
    const appDirFiles = readdirSync(root, { withFileTypes: true });
    if (appDirFiles.length > 0) {
        appDirFiles.forEach((file) => {
            const fileDir = path.join(root, file.name);
            if (file.isDirectory()) {
                (0, exports.customizeAppInfo)(fileDir, proName);
            }
            else {
                const fileName = path.basename(fileDir);
                if (fileName.includes('Sample')) {
                    // 替换文件名中的 ‘Sample’ 为 组件名
                    const newName = fileName.replace('Sample', `${proName}Comp`);
                    const newFileDir = path.join(path.dirname(fileDir), newName);
                    renameSync(fileDir, newFileDir);
                    customizeTool(newFileDir, proName);
                }
                else {
                    customizeTool(fileDir, proName);
                }
            }
        });
    }
    else {
        customizeTool(root, proName);
    }
};
exports.customizeAppInfo = customizeAppInfo;
// 替换文件名
const replaceFileName = (fileDir, proName, oldPath = 'Sample') => {
    const fileName = path.basename(fileDir);
    if (fileName.includes(oldPath)) {
        let newName = fileName.replace(oldPath, proName);
        newName = newName.replace(newName[0], newName[0].toLowerCase());
        const newFileDir = path.join(path.dirname(fileDir), newName);
        renameSync(fileDir, newFileDir);
        customizeTool(newFileDir, proName);
    }
};
exports.replaceFileName = replaceFileName;
