"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 ViteTemplate 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.config.js');
                    await this.copyTemplateFile(directory, 'forge.env.d.txt');
                    await fs_extra_1.default.rename(path_1.default.join(directory, 'forge.env.d.txt'), path_1.default.join(directory, 'forge.env.d.ts'));
                },
            },
            {
                title: 'Setting up Vite configuration',
                task: async () => {
                    await this.copyTemplateFile(directory, 'vite.base.config.mjs');
                    await this.copyTemplateFile(directory, 'vite.main.config.mjs');
                    await this.copyTemplateFile(directory, 'vite.preload.config.mjs');
                    await this.copyTemplateFile(directory, 'vite.renderer.config.mjs');
                    await this.copyTemplateFile(path_1.default.join(directory, 'src'), 'renderer.js');
                    await this.copyTemplateFile(path_1.default.join(directory, 'src'), 'preload.js');
                    await this.updateFileByLine(path_1.default.resolve(directory, 'src', 'index.js'), (line) => {
                        if (line.includes('mainWindow.loadFile'))
                            return `  if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
    mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
  } else {
    mainWindow.loadFile(path.join(__dirname, \`../renderer/\${MAIN_WINDOW_VITE_NAME}/index.html\`));
  }`;
                        return line;
                    }, path_1.default.resolve(directory, 'src', 'main.js'));
                    // TODO: Compatible with any path entry.
                    // Vite uses index.html under the root path as the entry point.
                    fs_extra_1.default.moveSync(path_1.default.join(directory, 'src', '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.js"></script>\n  </body>';
                        return line;
                    });
                    // update package.json entry point
                    const pjPath = path_1.default.resolve(directory, 'package.json');
                    const currentPJ = await fs_extra_1.default.readJson(pjPath);
                    currentPJ.main = '.vite/build/main.js';
                    await fs_extra_1.default.writeJson(pjPath, currentPJ, {
                        spaces: 2,
                    });
                },
            },
        ];
    }
}
exports.default = new ViteTemplate();
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiVml0ZVRlbXBsYXRlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL1ZpdGVUZW1wbGF0ZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLGdEQUF3QjtBQUd4QixpRUFBNkQ7QUFDN0Qsd0RBQTBCO0FBRTFCLE1BQU0sWUFBYSxTQUFRLDRCQUFZO0lBQXZDOztRQUNTLGdCQUFXLEdBQUcsY0FBSSxDQUFDLE9BQU8sQ0FBQyxTQUFTLEVBQUUsSUFBSSxFQUFFLE1BQU0sQ0FBQyxDQUFDO0lBMEQ3RCxDQUFDO0lBeERRLEtBQUssQ0FBQyxrQkFBa0IsQ0FBQyxTQUFpQixFQUFFLE9BQTRCO1FBQzdFLE1BQU0sVUFBVSxHQUFHLE1BQU0sS0FBSyxDQUFDLGtCQUFrQixDQUFDLFNBQVMsRUFBRSxPQUFPLENBQUMsQ0FBQztRQUN0RSxPQUFPO1lBQ0wsR0FBRyxVQUFVO1lBQ2I7Z0JBQ0UsS0FBSyxFQUFFLGdDQUFnQztnQkFDdkMsSUFBSSxFQUFFLEtBQUssSUFBSSxFQUFFO29CQUNmLE1BQU0sSUFBSSxDQUFDLGdCQUFnQixDQUFDLFNBQVMsRUFBRSxpQkFBaUIsQ0FBQyxDQUFDO29CQUMxRCxNQUF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