"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 path = require("path");
const logger_1 = require("./logger");
const constants_1 = require("./constants");
const loadYamlFile = require("load-yaml-file");
const constants_2 = require("./constants");
class PnpmError extends Error {
    constructor(code, message) {
        super(message);
        this.code = code;
    }
}
class ShrinkwrapBreakingChangeError extends PnpmError {
    constructor(filename) {
        super('SHRINKWRAP_BREAKING_CHANGE', `Shrinkwrap file ${filename} not compatible with current pnpm`);
        this.filename = filename;
    }
}
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 yield _read(shrinkwrapPath, 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 yield _read(shrinkwrapPath, opts);
    });
}
exports.readWanted = readWanted;
function _read(shrinkwrapPath, 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;
        }
        // for backward compatibility
        if (shrinkwrap && shrinkwrap['version'] && !shrinkwrap.shrinkwrapVersion) {
            shrinkwrap.shrinkwrapVersion = shrinkwrap['version'];
            delete shrinkwrap['version'];
        }
        if (shrinkwrap && Math.floor(shrinkwrap.shrinkwrapVersion) === Math.floor(constants_2.SHRINKWRAP_VERSION)) {
            if (shrinkwrap.shrinkwrapVersion > constants_2.SHRINKWRAP_VERSION) {
                logger_1.default.warn(`Your shrinkwrap.yaml was generated by a newer version of pnpm.
        It is a compatible version but it might get downgraded to version ${constants_2.SHRINKWRAP_VERSION}`);
            }
            return shrinkwrap;
        }
        if (opts.ignoreIncompatible) {
            logger_1.default.warn(`Ignoring not compatible shrinkwrap file at ${shrinkwrapPath}`);
            return null;
        }
        throw new ShrinkwrapBreakingChangeError(shrinkwrapPath);
    });
}
function create(registry) {
    return {
        shrinkwrapVersion: constants_2.SHRINKWRAP_VERSION,
        shrinkwrapMinorVersion: constants_2.SHRINKWRAP_MINOR_VERSION,
        specifiers: {},
        dependencies: {},
        packages: {},
        registry,
    };
}
exports.create = create;
//# sourceMappingURL=read.js.map