"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkSystem = exports.checkValidPackageManagerVersion = void 0;
const child_process_1 = require("child_process");
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const core_1 = require("@electron-forge/core");
const debug_1 = __importDefault(require("debug"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const semver_1 = __importDefault(require("semver"));
const d = (0, debug_1.default)('electron-forge:check-system');
async function getGitVersion() {
    return new Promise((resolve) => {
        (0, child_process_1.exec)('git --version', (err, output) => (err ? resolve(null) : resolve(output.toString().trim().split(' ').reverse()[0])));
    });
}
async function checkNodeVersion() {
    const { engines } = await fs_extra_1.default.readJson(path_1.default.resolve(__dirname, '..', '..', 'package.json'));
    const versionSatisfied = semver_1.default.satisfies(process.versions.node, engines.node);
    if (!versionSatisfied) {
        throw new Error(`You are running Node.js version ${process.versions.node}, but Electron Forge requires Node.js ${engines.node}.`);
    }
    return process.versions.node;
}
const NPM_ALLOWLISTED_VERSIONS = {
    all: '^3.0.0 || ^4.0.0 || ~5.1.0 || ~5.2.0 || >= 5.4.2',
    darwin: '>= 5.4.0',
    linux: '>= 5.4.0',
};
const YARN_ALLOWLISTED_VERSIONS = {
    all: '>= 1.0.0',
};
function checkValidPackageManagerVersion(packageManager, version, allowlistedVersions) {
    if (!semver_1.default.valid(version)) {
        d(`Invalid semver-string while checking version: ${version}`);
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        throw new Error(`Could not check ${packageManager} version "${version}", assuming incompatible`);
    }
    if (!semver_1.default.satisfies(version, allowlistedVersions)) {
        throw new Error(`Incompatible version of ${packageManager} detected "${version}", must be in range ${allowlistedVersions}`);
    }
}
exports.checkValidPackageManagerVersion = checkValidPackageManagerVersion;
function warnIfPackageManagerIsntAKnownGoodVersion(packageManager, version, allowlistedVersions) {
    const osVersions = allowlistedVersions[process.platform];
    const versions = osVersions ? `${allowlistedVersions.all} || ${osVersions}` : allowlistedVersions.all;
    const versionString = version.toString();
    checkValidPackageManagerVersion(packageManager, versionString, versions);
}
async function checkPackageManagerVersion() {
    const version = await core_1.utils.yarnOrNpmSpawn(['--version']);
    const versionString = version.toString().trim();
    if (core_1.utils.hasYarn()) {
        warnIfPackageManagerIsntAKnownGoodVersion('Yarn', versionString, YARN_ALLOWLISTED_VERSIONS);
        return `yarn@${versionString}`;
    }
    else {
        warnIfPackageManagerIsntAKnownGoodVersion('NPM', versionString, NPM_ALLOWLISTED_VERSIONS);
        return `npm@${versionString}`;
    }
}
/**
 * Some people know their system is OK and don't appreciate the 800ms lag in
 * start up that these checks (in particular the package manager check) costs.
 *
 * Simply creating this flag file in your home directory will skip these checks
 * and shave ~800ms off your forge start time.
 *
 * This is specifically not documented or everyone would make it.
 */
const SKIP_SYSTEM_CHECK = path_1.default.resolve(os_1.default.homedir(), '.skip-forge-system-check');
async function checkSystem(task) {
    if (!(await fs_extra_1.default.pathExists(SKIP_SYSTEM_CHECK))) {
        d('checking system, create ~/.skip-forge-system-check to stop doing this');
        return task.newListr([
            {
                title: 'Checking git exists',
                task: async (_, task) => {
                    const gitVersion = await getGitVersion();
                    if (gitVersion) {
                        task.title = `Found git@${gitVersion}`;
                    }
                    else {
                        throw new Error('Could not find git in environment');
                    }
                },
            },
            {
                title: 'Checking node version',
                task: async (_, task) => {
                    const nodeVersion = await checkNodeVersion();
                    task.title = `Found node@${nodeVersion}`;
                },
            },
            {
                title: 'Checking packageManager version',
                task: async (_, task) => {
                    const packageManager = await checkPackageManagerVersion();
                    task.title = `Found ${packageManager}`;
                },
            },
        ], {
            concurrent: true,
            exitOnError: false,
            rendererOptions: {
                collapseSubtasks: true,
            },
        });
    }
    d('skipping system check');
    return true;
}
exports.checkSystem = checkSystem;
//# sourceMappingURL=data:application/json;base64, 