"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MakerBase = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const which_1 = __importDefault(require("which"));
class Maker {
    /**
     * @param configOrConfigFetcher - Either a configuration object for this maker or a simple method that returns such a configuration for a given target architecture
     * @param platformsToMakeOn - If you want this maker to run on platforms different from `defaultPlatforms` you can provide those platforms here
     */
    constructor(configOrConfigFetcher = {}, platformsToMakeOn) {
        this.configOrConfigFetcher = configOrConfigFetcher;
        this.platformsToMakeOn = platformsToMakeOn;
        this.requiredExternalBinaries = [];
        Object.defineProperty(this, '__isElectronForgeMaker', {
            value: true,
            enumerable: false,
            configurable: false,
        });
    }
    get platforms() {
        if (this.platformsToMakeOn)
            return this.platformsToMakeOn;
        return this.defaultPlatforms;
    }
    // TODO: Remove this, it is an eye-sore and is a nasty hack to provide forge
    //       v5 style functionality in the new API
    async prepareConfig(targetArch) {
        if (typeof this.configOrConfigFetcher === 'function') {
            this.config = await Promise.resolve(this.configOrConfigFetcher(targetArch));
        }
        else {
            this.config = this.configOrConfigFetcher;
        }
    }
    /**
     * Makers must implement this method and return true or false indicating whether
     * this maker can be run on the current platform.  Normally this is just a process.platform
     * check but it can be a deeper check for dependencies like fake-root or other
     * required external build tools.
     *
     * If the issue is a missing dependency you should log out a HELPFUL error message
     * telling the developer exactly what is missing and if possible how to get it.
     */
    isSupportedOnCurrentPlatform() {
        if (this.isSupportedOnCurrentPlatform === Maker.prototype.isSupportedOnCurrentPlatform) {
            throw new Error(`Maker ${this.name} did not implement the isSupportedOnCurrentPlatform method`);
        }
        return true;
    }
    clone() {
        const MakerClass = this.constructor;
        return new MakerClass(this.configOrConfigFetcher, this.platformsToMakeOn);
    }
    /**
     * Makers must implement this method and return an array of absolute paths
     * to the artifacts generated by your maker
     */
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    async make(opts) {
        if (this.make === Maker.prototype.make) {
            throw new Error(`Maker ${this.name} did not implement the make method`);
        }
        return [];
    }
    /**
     * Helpers
     */
    /**
     * Ensures the directory exists and is forced to be empty.
     *
     * I.e. If the directory already exists it is deleted and recreated, this
     * is a destructive operation
     */
    async ensureDirectory(dir) {
        if (await fs_extra_1.default.pathExists(dir)) {
            await fs_extra_1.default.remove(dir);
        }
        return fs_extra_1.default.mkdirs(dir);
    }
    /**
     * Ensures the path to the file exists and the file does not exist
     *
     * I.e. If the file already exists it is deleted and the path created
     */
    async ensureFile(file) {
        if (await fs_extra_1.default.pathExists(file)) {
            await fs_extra_1.default.remove(file);
        }
        await fs_extra_1.default.mkdirs(path_1.default.dirname(file));
    }
    /**
     * Checks if the specified binaries exist, which are required for the maker to be used.
     */
    externalBinariesExist() {
        return this.requiredExternalBinaries.every((binary) => which_1.default.sync(binary, { nothrow: true }) !== null);
    }
    /**
     * Throws an error if any of the binaries don't exist.
     */
    ensureExternalBinariesExist() {
        if (!this.externalBinariesExist()) {
            throw new Error(`Cannot make for ${this.name}, the following external binaries need to be installed: ${this.requiredExternalBinaries.join(', ')}`);
        }
    }
    /**
     * Checks if the given module is installed, used for testing if optional dependencies
     * are installed or not
     */
    isInstalled(module) {
        try {
            require(module);
            return true;
        }
        catch (e) {
            // Package doesn't exist -- must not be installable on this platform
            return false;
        }
    }
    /**
     * Normalize the given semver-formatted version to a 4-part dot delimited version number without
     * prerelease information for use in Windows apps.
     */
    normalizeWindowsVersion(version) {
        const noPrerelease = version.replace(/[-+].*/, '');
        return `${noPrerelease}.0`;
    }
}
exports.default = Maker;
exports.MakerBase = Maker;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTWFrZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvTWFrZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQUEsZ0RBQXdCO0FB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