"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initLink = void 0;
const path_1 = __importDefault(require("path"));
const core_utils_1 = require("@electron-forge/core-utils");
const debug_1 = __importDefault(require("debug"));
const read_package_json_1 = require("../../util/read-package-json");
const d = (0, debug_1.default)('electron-forge:init:link');
/**
 * Link local forge dependencies
 *
 * This allows developers working on forge itself to easily init
 * a local template and have it use their local plugins / core / cli packages.
 *
 * Note: `yarn link:prepare` needs to run first before dependencies can be
 * linked.
 */
async function initLink(dir, task) {
    const shouldLink = process.env.LINK_FORGE_DEPENDENCIES_ON_INIT;
    if (shouldLink) {
        d('Linking forge dependencies');
        const packageJson = await (0, read_package_json_1.readRawPackageJson)(dir);
        const packageManager = (0, core_utils_1.safeYarnOrNpm)();
        const linkFolder = path_1.default.resolve(__dirname, '..', '..', '..', '..', '..', '..', '.links');
        for (const packageName of Object.keys(packageJson.devDependencies)) {
            if (packageName.startsWith('@electron-forge/')) {
                if (task)
                    task.output = `${packageManager} link --link-folder ${linkFolder} ${packageName}`;
                await (0, core_utils_1.yarnOrNpmSpawn)(['link', '--link-folder', linkFolder, packageName], {
                    cwd: dir,
                });
            }
        }
    }
    else {
        d('LINK_FORGE_DEPENDENCIES_ON_INIT is falsy. Skipping.');
    }
}
exports.initLink = initLink;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5pdC1saW5rLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2FwaS9pbml0LXNjcmlwdHMvaW5pdC1saW5rLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQUFBLGdEQUF3QjtBQUV4QiwyREFBMkU7QUFFM0Usa0RBQTBCO0FBRTFCLG9FQUFrRTtBQUVsRSxNQUFNLENBQUMsR0FBRyxJQUFBLGVBQUssRUFBQyw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