"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.write = exports.read = void 0;
const isWindows = require("is-windows");
const path = require("path");
const read_yaml_file_1 = require("read-yaml-file");
const writeYamlFile = require("write-yaml-file");
// The dot prefix is needed because otherwise `npm shrinkwrap`
// thinks that it is an extraneous package.
const MODULES_FILENAME = '.modules.yaml';
async function read(modulesDir) {
    const modulesYamlPath = path.join(modulesDir, MODULES_FILENAME);
    try {
        const modules = await read_yaml_file_1.default(modulesYamlPath);
        if (!modules.virtualStoreDir) {
            modules.virtualStoreDir = path.join(modulesDir, '.pnpm');
        }
        else if (!path.isAbsolute(modules.virtualStoreDir)) {
            modules.virtualStoreDir = path.join(modulesDir, modules.virtualStoreDir);
        }
        return modules;
    }
    catch (err) {
        if (err.code !== 'ENOENT') {
            throw err;
        }
        return null;
    }
}
exports.read = read;
const YAML_OPTS = { sortKeys: true };
function write(modulesDir, modules) {
    const modulesYamlPath = path.join(modulesDir, MODULES_FILENAME);
    const saveModules = { ...modules };
    if (saveModules.skipped)
        saveModules.skipped.sort();
    if (!saveModules.hoistPattern) {
        // Because the YAML writer fails on undefined fields
        delete saveModules.hoistPattern;
        delete saveModules.hoistedAliases;
    }
    // We should store the absolute virtual store directory path on Windows
    // because junctions are used on Windows. Junctions will break even if
    // the relative path to the virtual store remains the same after moving
    // a project.
    if (!isWindows()) {
        saveModules.virtualStoreDir = path.relative(modulesDir, saveModules.virtualStoreDir);
    }
    return writeYamlFile(modulesYamlPath, saveModules, YAML_OPTS);
}
exports.write = write;
//# sourceMappingURL=index.js.map