"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 loadYamlFile = require("load-yaml-file");
const path = require("path");
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';
function read(virtualStoreDir) {
    return __awaiter(this, void 0, void 0, function* () {
        const modulesYamlPath = path.join(virtualStoreDir, MODULES_FILENAME);
        try {
            const m = yield loadYamlFile(modulesYamlPath);
            // for backward compatibility
            // tslint:disable:no-string-literal
            if (m['storePath']) {
                m.store = m['storePath'];
                delete m['storePath'];
            }
            if (!m.importers) {
                m.importers = {
                    '.': {
                        hoistedAliases: m['hoistedAliases'],
                        shamefullyFlatten: m['shamefullyFlatten'],
                    },
                };
                delete m['hoistedAliases'];
                delete m['shamefullyFlatten'];
            }
            // tslint:enable:no-string-literal
            return m;
        }
        catch (err) {
            if (err.code !== 'ENOENT') {
                throw err;
            }
            return null;
        }
    });
}
exports.read = read;
const YAML_OPTS = { sortKeys: true };
function write(virtualStoreDir, modules) {
    const modulesYamlPath = path.join(virtualStoreDir, MODULES_FILENAME);
    if (modules['skipped'])
        modules['skipped'].sort(); // tslint:disable-line:no-string-literal
    return writeYamlFile(modulesYamlPath, normalizeModules(modules), YAML_OPTS);
}
exports.write = write;
function normalizeModules(m) {
    const normalized = Object.assign({}, m);
    if (Object.keys(m.importers).length === 1 && m.importers['.']) {
        Object.assign(normalized, m.importers['.']);
        delete normalized.importers;
    }
    return normalized;
}
//# sourceMappingURL=index.js.map