"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 loadYamlFile = require("load-yaml-file");
const writeYamlFile = require("write-yaml-file");
const rimraf = require("rimraf-then");
const isCI = require("is-ci");
const pnpm_logger_1 = require("pnpm-logger");
const shrinkwrapLogger = pnpm_logger_1.default('shrinkwrap');
exports.SHRINKWRAP_FILENAME = 'shrinkwrap.yaml';
exports.PRIVATE_SHRINKWRAP_FILENAME = path.join('node_modules', '.shrinkwrap.yaml');
const SHRINKWRAP_VERSION = 3;
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;
    }
}
function getDefaultShrinkwrap(registry) {
    return {
        version: SHRINKWRAP_VERSION,
        specifiers: {},
        dependencies: {},
        packages: {},
        registry,
    };
}
function readPrivate(pkgPath, opts) {
    return __awaiter(this, void 0, void 0, function* () {
        const shrinkwrapPath = path.join(pkgPath, exports.PRIVATE_SHRINKWRAP_FILENAME);
        let shrinkwrap;
        try {
            shrinkwrap = yield loadYamlFile(shrinkwrapPath);
        }
        catch (err) {
            if (err.code !== 'ENOENT') {
                throw err;
            }
            return getDefaultShrinkwrap(opts.registry);
        }
        if (shrinkwrap && shrinkwrap.version === SHRINKWRAP_VERSION) {
            return shrinkwrap;
        }
        if (opts.force || isCI) {
            shrinkwrapLogger.warn(`Ignoring not compatible shrinkwrap file at ${shrinkwrapPath}`);
            return getDefaultShrinkwrap(opts.registry);
        }
        throw new ShrinkwrapBreakingChangeError(shrinkwrapPath);
    });
}
exports.readPrivate = readPrivate;
function read(pkgPath, opts) {
    return __awaiter(this, void 0, void 0, function* () {
        const shrinkwrapPath = path.join(pkgPath, exports.SHRINKWRAP_FILENAME);
        let shrinkwrap;
        try {
            shrinkwrap = yield loadYamlFile(shrinkwrapPath);
        }
        catch (err) {
            if (err.code !== 'ENOENT') {
                throw err;
            }
            return getDefaultShrinkwrap(opts.registry);
        }
        if (shrinkwrap && shrinkwrap.version === SHRINKWRAP_VERSION) {
            return shrinkwrap;
        }
        if (opts.force || isCI) {
            shrinkwrapLogger.warn(`Ignoring not compatible shrinkwrap file at ${shrinkwrapPath}`);
            return getDefaultShrinkwrap(opts.registry);
        }
        throw new ShrinkwrapBreakingChangeError(shrinkwrapPath);
    });
}
exports.read = read;
function save(pkgPath, shrinkwrap) {
    const shrinkwrapPath = path.join(pkgPath, exports.SHRINKWRAP_FILENAME);
    const privateShrinkwrapPath = path.join(pkgPath, exports.PRIVATE_SHRINKWRAP_FILENAME);
    // empty shrinkwrap is not saved
    if (Object.keys(shrinkwrap.specifiers).length === 0) {
        return Promise.all([
            rimraf(shrinkwrapPath),
            rimraf(privateShrinkwrapPath),
        ]);
    }
    const formatOpts = {
        sortKeys: true,
        lineWidth: 1000,
        noCompatMode: true,
    };
    return Promise.all([
        writeYamlFile(shrinkwrapPath, shrinkwrap, formatOpts),
        writeYamlFile(privateShrinkwrapPath, shrinkwrap, formatOpts),
    ]);
}
exports.save = save;
//# sourceMappingURL=io.js.map