"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("@pnpm/types");
const loadYamlFile = require("load-yaml-file");
const path = require("path");
const constants_1 = require("./constants");
const errors_1 = require("./errors");
const logger_1 = require("./logger");
exports.readPrivate = readCurrent;
function readCurrent(pkgPath, opts) {
    return __awaiter(this, void 0, void 0, function* () {
        const shrinkwrapPath = path.join(pkgPath, constants_1.CURRENT_SHRINKWRAP_FILENAME);
        return _read(shrinkwrapPath, pkgPath, opts);
    });
}
exports.readCurrent = readCurrent;
exports.read = readWanted;
function readWanted(pkgPath, opts) {
    return __awaiter(this, void 0, void 0, function* () {
        const shrinkwrapPath = path.join(pkgPath, constants_1.WANTED_SHRINKWRAP_FILENAME);
        return _read(shrinkwrapPath, pkgPath, opts);
    });
}
exports.readWanted = readWanted;
function _read(shrinkwrapPath, prefix, opts) {
    return __awaiter(this, void 0, void 0, function* () {
        let shrinkwrap;
        try {
            shrinkwrap = yield loadYamlFile(shrinkwrapPath);
        }
        catch (err) {
            if (err.code !== 'ENOENT') {
                throw err;
            }
            return null;
        }
        // tslint:disable:no-string-literal
        if (shrinkwrap && typeof shrinkwrap['specifiers'] !== 'undefined') {
            shrinkwrap.importers = {
                '.': {
                    specifiers: shrinkwrap['specifiers'],
                },
            };
            delete shrinkwrap['specifiers'];
            for (const depType of types_1.DEPENDENCIES_FIELDS) {
                if (shrinkwrap[depType]) {
                    shrinkwrap.importers['.'][depType] = shrinkwrap[depType];
                    delete shrinkwrap[depType];
                }
            }
        }
        // for backward compatibility
        if (shrinkwrap && shrinkwrap['version'] && !shrinkwrap.shrinkwrapVersion) {
            shrinkwrap.shrinkwrapVersion = shrinkwrap['version'];
            delete shrinkwrap['version'];
        }
        // tslint:enable:no-string-literal
        if (shrinkwrap && Math.floor(shrinkwrap.shrinkwrapVersion) === Math.floor(constants_1.SHRINKWRAP_VERSION)) {
            if (shrinkwrap.shrinkwrapVersion > constants_1.SHRINKWRAP_VERSION) {
                logger_1.default.warn({
                    message: 'Your shrinkwrap.yaml was generated by a newer version of pnpm. ' +
                        `It is a compatible version but it might get downgraded to version ${constants_1.SHRINKWRAP_VERSION}`,
                    prefix,
                });
            }
            return shrinkwrap;
        }
        if (opts.ignoreIncompatible) {
            logger_1.default.warn({
                message: `Ignoring not compatible shrinkwrap file at ${shrinkwrapPath}`,
                prefix,
            });
            return null;
        }
        throw new errors_1.ShrinkwrapBreakingChangeError(shrinkwrapPath);
    });
}
function create(registry, importerIds, opts) {
    const importers = importerIds.reduce((acc, importerId) => {
        acc[importerId] = {
            dependencies: {},
            specifiers: {},
        };
        return acc;
    }, {});
    return {
        importers,
        registry,
        shrinkwrapMinorVersion: opts.shrinkwrapMinorVersion || constants_1.SHRINKWRAP_MINOR_VERSION,
        shrinkwrapVersion: constants_1.SHRINKWRAP_VERSION,
    };
}
exports.create = create;
//# sourceMappingURL=read.js.map