"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 core_utils_1 = require("@electron-forge/core-utils");
const debug_1 = __importDefault(require("debug"));
const listr2_1 = require("listr2");
const semver_1 = __importDefault(require("semver"));
const install_dependencies_1 = __importStar(require("../util/install-dependencies"));
const read_package_json_1 = require("../util/read-package-json");
const find_template_1 = require("./init-scripts/find-template");
const init_directory_1 = require("./init-scripts/init-directory");
const init_git_1 = require("./init-scripts/init-git");
const init_link_1 = require("./init-scripts/init-link");
const init_npm_1 = require("./init-scripts/init-npm");
const d = (0, debug_1.default)('electron-forge:init');
async function validateTemplate(template, templateModule) {
    if (!templateModule.requiredForgeVersion) {
        throw new Error(`Cannot use a template (${template}) with this version of Electron Forge, as it does not specify its required Forge version.`);
    }
    const forgeVersion = (await (0, read_package_json_1.readRawPackageJson)(path_1.default.join(__dirname, '..', '..'))).version;
    if (!semver_1.default.satisfies(forgeVersion, templateModule.requiredForgeVersion)) {
        throw new Error(`Template (${template}) is not compatible with this version of Electron Forge (${forgeVersion}), it requires ${templateModule.requiredForgeVersion}`);
    }
}
exports.default = async ({ dir = process.cwd(), interactive = false, copyCIFiles = false, force = false, template = 'base' }) => {
    d(`Initializing in: ${dir}`);
    const packageManager = (0, core_utils_1.safeYarnOrNpm)();
    const runner = new listr2_1.Listr([
        {
            title: `Locating custom template: "${template}"`,
            task: async (ctx) => {
                ctx.templateModule = await (0, find_template_1.findTemplate)(dir, template);
            },
        },
        {
            title: 'Initializing directory',
            task: async (_, task) => {
                await (0, init_directory_1.initDirectory)(dir, task, force);
                await (0, init_git_1.initGit)(dir);
            },
            rendererOptions: { persistentOutput: true },
        },
        {
            title: 'Preparing template',
            task: async ({ templateModule }) => {
                await validateTemplate(template, templateModule);
            },
        },
        {
            title: 'Initializing template',
            task: async ({ templateModule }, task) => {
                if (typeof templateModule.initializeTemplate === 'function') {
                    const tasks = await templateModule.initializeTemplate(dir, { copyCIFiles });
                    if (tasks) {
                        return task.newListr(tasks, { concurrent: false });
                    }
                }
            },
        },
        {
            title: 'Installing template dependencies',
            task: async ({ templateModule }, task) => {
                return task.newListr([
                    {
                        title: 'Installing production dependencies',
                        task: async (_, task) => {
                            d('installing dependencies');
                            if (templateModule.dependencies?.length) {
                                task.output = `${packageManager} install ${templateModule.dependencies.join(' ')}`;
                            }
                            return await (0, install_dependencies_1.default)(dir, templateModule.dependencies || [], install_dependencies_1.DepType.PROD, install_dependencies_1.DepVersionRestriction.RANGE);
                        },
                        exitOnError: false,
                    },
                    {
                        title: 'Installing development dependencies',
                        task: async (_, task) => {
                            d('installing devDependencies');
                            if (templateModule.devDependencies?.length) {
                                task.output = `${packageManager} install --dev ${templateModule.devDependencies.join(' ')}`;
                            }
                            await (0, install_dependencies_1.default)(dir, templateModule.devDependencies || [], install_dependencies_1.DepType.DEV);
                        },
                        exitOnError: false,
                    },
                    {
                        title: 'Finalizing dependencies',
                        task: async (_, task) => {
                            return task.newListr([
                                {
                                    title: 'Installing common dependencies',
                                    task: async (_, task) => {
                                        await (0, init_npm_1.initNPM)(dir, task);
                                    },
                                    exitOnError: false,
                                },
                                {
                                    title: process.env.LINK_FORGE_DEPENDENCIES_ON_INIT ? 'Linking forge dependencies' : 'Skip linking forge dependencies',
                                    task: async (_, task) => {
                                        await (0, init_link_1.initLink)(dir, task);
                                    },
                                    exitOnError: true,
                                },
                            ]);
                        },
                    },
                ], {
                    concurrent: false,
                });
            },
        },
    ], {
        concurrent: false,
        silentRendererCondition: !interactive,
        fallbackRendererCondition: Boolean(process.env.DEBUG) || Boolean(process.env.CI),
    });
    await runner.run();
};
//# sourceMappingURL=data:application/json;base64, 