"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MakerZIP = void 0;
const path_1 = __importDefault(require("path"));
const util_1 = require("util");
const maker_base_1 = require("@electron-forge/maker-base");
const fs_extra_1 = __importDefault(require("fs-extra"));
const got_1 = __importDefault(require("got"));
class MakerZIP extends maker_base_1.MakerBase {
    constructor() {
        super(...arguments);
        this.name = 'zip';
        this.defaultPlatforms = ['darwin', 'mas', 'win32', 'linux'];
    }
    isSupportedOnCurrentPlatform() {
        return true;
    }
    async make({ dir, makeDir, appName, packageJSON, targetArch, targetPlatform }) {
        const { zip } = require('cross-zip');
        const zipDir = ['darwin', 'mas'].includes(targetPlatform) ? path_1.default.resolve(dir, `${appName}.app`) : dir;
        const zipName = `${path_1.default.basename(dir)}-${packageJSON.version}.zip`;
        const zipPath = path_1.default.resolve(makeDir, 'zip', targetPlatform, targetArch, zipName);
        await this.ensureFile(zipPath);
        await (0, util_1.promisify)(zip)(zipDir, zipPath);
        // Only generate RELEASES.json for darwin builds (not MAS)
        if (targetPlatform === 'darwin' && this.config.macUpdateManifestBaseUrl) {
            const parsed = new URL(this.config.macUpdateManifestBaseUrl);
            parsed.pathname += '/RELEASES.json';
            const response = await got_1.default.get(parsed.toString(), {
                throwHttpErrors: false,
            });
            let currentValue = {
                currentRelease: '',
                releases: [],
            };
            if (response.statusCode === 200) {
                currentValue = JSON.parse(response.body);
            }
            const updateUrl = new URL(this.config.macUpdateManifestBaseUrl);
            updateUrl.pathname += `/${zipName}`;
            // Remove existing release if it is already in the manifest
            currentValue.releases = currentValue.releases || [];
            currentValue.releases = currentValue.releases.filter((release) => release.version !== packageJSON.version);
            // Add the current version as the current release
            currentValue.currentRelease = packageJSON.version;
            currentValue.releases.push({
                version: packageJSON.version,
                updateTo: {
                    name: `${appName} v${packageJSON.version}`,
                    version: packageJSON.version,
                    pub_date: new Date().toISOString(),
                    url: updateUrl.toString(),
                    notes: this.config.macUpdateReleaseNotes || '',
                },
            });
            const releasesPath = path_1.default.resolve(makeDir, 'zip', targetPlatform, targetArch, 'RELEASES.json');
            await this.ensureFile(releasesPath);
            await fs_extra_1.default.writeJson(releasesPath, currentValue);
            return [zipPath, releasesPath];
        }
        return [zipPath];
    }
}
exports.default = MakerZIP;
exports.MakerZIP = MakerZIP;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTWFrZXJaSVAuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvTWFrZXJaSVAudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQUEsZ0RBQXdCO0FBQ3hCLCtCQUFpQztBQUVqQywyREFBcUU7QUFFckUsd0RBQTBCO0FBQzFCLDhDQUFzQjtBQW9CdEIsTUFBcUIsUUFBUyxTQUFRLHNCQUF5QjtJQUEvRDs7UUFDRSxTQUFJLEdBQUcsS0FBSyxDQUFDO1FBRWIscUJBQWdCLEdBQW9CLENBQUMsUUFBUSxFQUFFLEtBQUssRUFBRSxPQUFPLEVBQUUsT0FBTyxDQUFDLENBQUM7SUEwRDFFLENBQUM7SUF4REMsNEJBQTRCO1FBQzFCLE9BQU8sSUFBSSxDQUFDO0lBQ2QsQ0FBQztJQUVELEtBQUssQ0FBQyxJQUFJLENBQUMsRUFBRSxHQUFHLEVBQUUsT0FBTyxFQUFFLE9BQU8sRUFBRSxXQUFXLEVBQUUsVUFBVSxFQUFFLGNBQWMsRUFBZ0I7UUFDekYsTUFBTSxFQUFFLEdBQUcsRUFBRSxHQUFHLE9BQU8sQ0FBQyxXQUFXLENBQUMsQ0FBQztRQUVyQyxNQUFNLE1BQU0sR0FBRyxDQUFDLFFBQVEsRUFBRSxLQUFLLENBQUMsQ0FBQyxRQUFRLENBQUMsY0FBYyxDQUFDLENBQUMsQ0FBQyxDQUFDLGNBQUksQ0FBQyxPQUFPLENBQUMsR0FBRy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