"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContextForSingleImporter = void 0;
const core_loggers_1 = require("@pnpm/core-loggers");
const error_1 = require("@pnpm/error");
const logger_1 = require("@pnpm/logger");
const read_projects_context_1 = require("@pnpm/read-projects-context");
const types_1 = require("@pnpm/types");
const rimraf = require("@zkochan/rimraf");
const fs = require("mz/fs");
const path = require("path");
const pathAbsolute = require("path-absolute");
const R = require("ramda");
const checkCompatibility_1 = require("./checkCompatibility");
const readLockfiles_1 = require("./readLockfiles");
async function getContext(projects, opts) {
    var _a, _b, _c, _d, _e, _f;
    const modulesDir = (_a = opts.modulesDir) !== null && _a !== void 0 ? _a : 'node_modules';
    let importersContext = await read_projects_context_1.default(projects, { lockfileDir: opts.lockfileDir, modulesDir });
    const virtualStoreDir = pathAbsolute((_b = opts.virtualStoreDir) !== null && _b !== void 0 ? _b : path.join(modulesDir, '.pnpm'), opts.lockfileDir);
    if (importersContext.modules) {
        const { purged } = await validateModules(importersContext.modules, importersContext.projects, {
            currentHoistPattern: importersContext.currentHoistPattern,
            currentPublicHoistPattern: importersContext.currentPublicHoistPattern,
            forceNewModules: opts.forceNewModules === true,
            include: opts.include,
            lockfileDir: opts.lockfileDir,
            modulesDir,
            registries: opts.registries,
            storeDir: opts.storeDir,
            virtualStoreDir,
            forceHoistPattern: opts.forceHoistPattern,
            hoistPattern: opts.hoistPattern,
            forcePublicHoistPattern: opts.forcePublicHoistPattern,
            publicHoistPattern: opts.publicHoistPattern,
        });
        if (purged) {
            importersContext = await read_projects_context_1.default(projects, {
                lockfileDir: opts.lockfileDir,
                modulesDir,
            });
        }
    }
    await fs.mkdir(opts.storeDir, { recursive: true });
    projects.forEach((project) => {
        core_loggers_1.packageManifestLogger.debug({
            initial: project.manifest,
            prefix: project.rootDir,
        });
    });
    if ((_c = opts.hooks) === null || _c === void 0 ? void 0 : _c.readPackage) {
        projects = projects.map((project) => ({
            ...project,
            manifest: opts.hooks.readPackage(project.manifest),
        }));
    }
    const extraBinPaths = [
        ...opts.extraBinPaths || [],
    ];
    const hoistedModulesDir = path.join(virtualStoreDir, 'node_modules');
    if ((_d = opts.hoistPattern) === null || _d === void 0 ? void 0 : _d.length) {
        extraBinPaths.unshift(path.join(hoistedModulesDir, '.bin'));
    }
    const ctx = {
        extraBinPaths,
        hoistedDependencies: importersContext.hoistedDependencies,
        hoistedModulesDir,
        hoistPattern: (_e = importersContext.currentHoistPattern) !== null && _e !== void 0 ? _e : opts.hoistPattern,
        include: opts.include || importersContext.include,
        lockfileDir: opts.lockfileDir,
        modulesFile: importersContext.modules,
        pendingBuilds: importersContext.pendingBuilds,
        projects: importersContext.projects,
        publicHoistPattern: (_f = importersContext.currentPublicHoistPattern) !== null && _f !== void 0 ? _f : opts.publicHoistPattern,
        registries: {
            ...opts.registries,
            ...importersContext.registries,
        },
        rootModulesDir: importersContext.rootModulesDir,
        skipped: importersContext.skipped,
        storeDir: opts.storeDir,
        virtualStoreDir,
        ...await readLockfiles_1.default({
            force: opts.force,
            forceSharedLockfile: opts.forceSharedLockfile,
            lockfileDir: opts.lockfileDir,
            projects: importersContext.projects,
            registry: opts.registries.default,
            useLockfile: opts.useLockfile,
            virtualStoreDir,
        }),
    };
    return ctx;
}
exports.default = getContext;
async function validateModules(modules, projects, opts) {
    const rootProject = projects.find(({ id }) => id === '.');
    if (opts.forcePublicHoistPattern && !R.equals(modules.publicHoistPattern, opts.publicHoistPattern)) {
        if (opts.forceNewModules && rootProject) {
            await purgeModulesDirsOfImporter(rootProject);
            return { purged: true };
        }
        throw new error_1.default('PUBLIC_HOIST_PATTERN_DIFF', 'This modules directory was created using a different public-hoist-pattern value.'
            + ' Run "pnpm install" to recreate the modules directory.');
    }
    let purged = false;
    if (opts.forceHoistPattern && rootProject) {
        try {
            if (!R.equals(opts.currentHoistPattern, (opts.hoistPattern || undefined))) {
                throw new error_1.default('HOIST_PATTERN_DIFF', 'This modules directory was created using a different hoist-pattern value.'
                    + ' Run "pnpm install" to recreate the modules directory.');
            }
        }
        catch (err) {
            if (!opts.forceNewModules)
                throw err;
            await purgeModulesDirsOfImporter(rootProject);
            purged = true;
        }
    }
    await Promise.all(projects.map(async (project) => {
        try {
            checkCompatibility_1.default(modules, {
                modulesDir: project.modulesDir,
                storeDir: opts.storeDir,
                virtualStoreDir: opts.virtualStoreDir,
            });
            if (opts.lockfileDir !== project.rootDir && opts.include && modules.included) {
                for (const depsField of types_1.DEPENDENCIES_FIELDS) {
                    if (opts.include[depsField] !== modules.included[depsField]) {
                        throw new error_1.default('INCLUDED_DEPS_CONFLICT', `modules directory (at "${opts.lockfileDir}") was installed with ${stringifyIncludedDeps(modules.included)}. ` +
                            `Current install wants ${stringifyIncludedDeps(opts.include)}.`);
                    }
                }
            }
        }
        catch (err) {
            if (!opts.forceNewModules)
                throw err;
            await purgeModulesDirsOfImporter(project);
            purged = true;
        }
    }));
    if (modules.registries && !R.equals(opts.registries, modules.registries)) {
        if (opts.forceNewModules) {
            await Promise.all(projects.map(purgeModulesDirsOfImporter));
            return { purged: true };
        }
        throw new error_1.default('REGISTRIES_MISMATCH', `This modules directory was created using the following registries configuration: ${JSON.stringify(modules.registries)}. The current configuration is ${JSON.stringify(opts.registries)}. To recreate recreate the modules directory using the new settings, run "pnpm install".`);
    }
    if (purged && !rootProject) {
        await purgeModulesDirsOfImporter({
            modulesDir: path.join(opts.lockfileDir, opts.modulesDir),
            rootDir: opts.lockfileDir,
        });
    }
    return { purged };
}
async function purgeModulesDirsOfImporter(importer) {
    logger_1.default.info({
        message: `Recreating ${importer.modulesDir}`,
        prefix: importer.rootDir,
    });
    try {
        await rimraf(importer.modulesDir);
    }
    catch (err) {
        if (err.code !== 'ENOENT')
            throw err;
    }
}
function stringifyIncludedDeps(included) {
    return types_1.DEPENDENCIES_FIELDS.filter((depsField) => included[depsField]).join(', ');
}
async function getContextForSingleImporter(manifest, opts, alreadyPurged = false) {
    var _a, _b, _c, _d, _e, _f;
    const { currentHoistPattern, currentPublicHoistPattern, hoistedDependencies, projects, include, modules, pendingBuilds, registries, skipped, rootModulesDir, } = await read_projects_context_1.default([
        {
            rootDir: opts.dir,
        },
    ], {
        lockfileDir: opts.lockfileDir,
        modulesDir: opts.modulesDir,
    });
    const storeDir = opts.storeDir;
    const importer = projects[0];
    const modulesDir = importer.modulesDir;
    const importerId = importer.id;
    const virtualStoreDir = pathAbsolute((_a = opts.virtualStoreDir) !== null && _a !== void 0 ? _a : 'node_modules/.pnpm', opts.lockfileDir);
    if (modules && !alreadyPurged) {
        const { purged } = await validateModules(modules, projects, {
            currentHoistPattern,
            currentPublicHoistPattern,
            forceNewModules: opts.forceNewModules === true,
            include: opts.include,
            lockfileDir: opts.lockfileDir,
            modulesDir: (_b = opts.modulesDir) !== null && _b !== void 0 ? _b : 'node_modules',
            registries: opts.registries,
            storeDir: opts.storeDir,
            virtualStoreDir,
            forceHoistPattern: opts.forceHoistPattern,
            hoistPattern: opts.hoistPattern,
            forcePublicHoistPattern: opts.forcePublicHoistPattern,
            publicHoistPattern: opts.publicHoistPattern,
        });
        if (purged) {
            return getContextForSingleImporter(manifest, opts, true);
        }
    }
    await fs.mkdir(storeDir, { recursive: true });
    const extraBinPaths = [
        ...opts.extraBinPaths || [],
    ];
    const hoistedModulesDir = path.join(virtualStoreDir, 'node_modules');
    if ((_c = opts.hoistPattern) === null || _c === void 0 ? void 0 : _c.length) {
        extraBinPaths.unshift(path.join(hoistedModulesDir, '.bin'));
    }
    const ctx = {
        extraBinPaths,
        hoistedDependencies,
        hoistedModulesDir,
        hoistPattern: currentHoistPattern !== null && currentHoistPattern !== void 0 ? currentHoistPattern : opts.hoistPattern,
        importerId,
        include: opts.include || include,
        lockfileDir: opts.lockfileDir,
        manifest: (_f = (_e = (_d = opts.hooks) === null || _d === void 0 ? void 0 : _d.readPackage) === null || _e === void 0 ? void 0 : _e.call(_d, manifest)) !== null && _f !== void 0 ? _f : manifest,
        modulesDir,
        modulesFile: modules,
        pendingBuilds,
        prefix: opts.dir,
        publicHoistPattern: currentPublicHoistPattern !== null && currentPublicHoistPattern !== void 0 ? currentPublicHoistPattern : opts.publicHoistPattern,
        registries: {
            ...opts.registries,
            ...registries,
        },
        rootModulesDir,
        skipped,
        storeDir,
        virtualStoreDir,
        ...await readLockfiles_1.default({
            force: opts.force,
            forceSharedLockfile: opts.forceSharedLockfile,
            lockfileDir: opts.lockfileDir,
            projects: [{ id: importerId, rootDir: opts.dir }],
            registry: opts.registries.default,
            useLockfile: opts.useLockfile,
            virtualStoreDir,
        }),
    };
    core_loggers_1.packageManifestLogger.debug({
        initial: manifest,
        prefix: opts.dir,
    });
    return ctx;
}
exports.getContextForSingleImporter = getContextForSingleImporter;
//# sourceMappingURL=index.js.map