"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const template_base_1 = require("@electron-forge/template-base");
const fs_extra_1 = __importDefault(require("fs-extra"));
class ViteTypeScriptTemplate extends template_base_1.BaseTemplate {
    constructor() {
        super(...arguments);
        this.templateDir = path_1.default.resolve(__dirname, '..', 'tmpl');
    }
    async initializeTemplate(directory, options) {
        const superTasks = await super.initializeTemplate(directory, options);
        return [
            ...superTasks,
            {
                title: 'Setting up Forge configuration',
                task: async () => {
                    await this.copyTemplateFile(directory, 'forge.env.d.ts');
                    await this.copyTemplateFile(directory, 'forge.config.ts');
                    await fs_extra_1.default.remove(path_1.default.resolve(directory, 'forge.config.js'));
                },
            },
            {
                title: 'Preparing TypeScript files and configuration',
                task: async () => {
                    const filePath = (fileName) => path_1.default.join(directory, 'src', fileName);
                    // Copy Vite files
                    await this.copyTemplateFile(directory, 'vite.base.config.ts');
                    await this.copyTemplateFile(directory, 'vite.main.config.ts');
                    await this.copyTemplateFile(directory, 'vite.preload.config.ts');
                    await this.copyTemplateFile(directory, 'vite.renderer.config.ts');
                    // Copy tsconfig with a small set of presets
                    await this.copyTemplateFile(directory, 'tsconfig.json');
                    // Copy eslint config with recommended settings
                    await this.copyTemplateFile(directory, '.eslintrc.json');
                    // Remove index.js and replace with main.ts
                    await fs_extra_1.default.remove(filePath('index.js'));
                    await this.copyTemplateFile(path_1.default.join(directory, 'src'), 'main.ts');
                    await this.copyTemplateFile(path_1.default.join(directory, 'src'), 'renderer.ts');
                    // Remove preload.js and replace with preload.ts
                    await fs_extra_1.default.remove(filePath('preload.js'));
                    await this.copyTemplateFile(path_1.default.join(directory, 'src'), 'preload.ts');
                    // TODO: Compatible with any path entry.
                    // Vite uses index.html under the root path as the entry point.
                    await fs_extra_1.default.move(filePath('index.html'), path_1.default.join(directory, 'index.html'));
                    await this.updateFileByLine(path_1.default.join(directory, 'index.html'), (line) => {
                        if (line.includes('link rel="stylesheet"'))
                            return '';
                        if (line.includes('</body>'))
                            return '    <script type="module" src="/src/renderer.ts"></script>\n  </body>';
                        return line;
                    });
                    // update package.json
                    const packageJSONPath = path_1.default.resolve(directory, 'package.json');
                    const packageJSON = await fs_extra_1.default.readJson(packageJSONPath);
                    packageJSON.main = '.vite/build/main.js';
                    // Configure scripts for TS template
                    packageJSON.scripts.lint = 'eslint --ext .ts,.tsx .';
                    await fs_extra_1.default.writeJson(packageJSONPath, packageJSON, {
                        spaces: 2,
                    });
                },
            },
        ];
    }
}
exports.default = new ViteTypeScriptTemplate();
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiVml0ZVR5cGVTY3JpcHRUZW1wbGF0ZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9WaXRlVHlwZVNjcmlwdFRlbXBsYXRlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUEsZ0RBQXdCO0FBR3hCLGlFQUE2RDtBQUM3RCx3REFBMEI7QUFFMUIsTUFBTSxzQkFBdUIsU0FBUSw0QkFBWTtJQUFqRDs7UUFDUyxnQkFBVyxHQUFHLGNBQUksQ0FBQyxPQUFPLENBQUMsU0FBUyxFQUFFLElBQUksRUFBRSxNQUFNLENBQUMsQ0FBQztJQStEN0QsQ0FBQztJQTdEUSxLQUFLLENBQUMsa0JBQWtCLENBQUMsU0FBaUIsRUFBRSxPQUE0QjtRQUM3RSxNQUFNLFVBQVUsR0FBRyxNQUFNLEtBQUssQ0FBQyxrQkFBa0IsQ0FBQyxTQUFTLEVBQUUsT0FBTyxDQUFDLENBQUM7UUFDdEUsT0FBTztZQUNMLEdBQUcsVUFBVTtZQUNiO2dCQUNFLEtBQUssRUFBRSxnQ0FBZ0M7Z0JBQ3ZDLElBQUksRUFBRSxLQUFLLElBQUksRUFBRTtvQkFDZixNQUFNLElB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