"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());
    });
};
const path = require('path');
const requireJson_1 = require('../fs/requireJson');
const link_dir_1 = require('link-dir');
const mkdirp_1 = require('../fs/mkdirp');
/**
 * Perform the final symlinking of ./.store/x@1.0.0 -> ./x.
 *
 * @example
 *     target = '/node_modules/.store/lodash@4.0.0'
 *     modules = './node_modules'
 *     symlinkToModules(target, modules)
 */
function symlinkToModules(target, modules) {
    return __awaiter(this, void 0, void 0, function* () {
        // TODO: uncomment to make things fail
        const pkgData = yield requireJson_1.default(path.join(target, 'package.json'));
        if (!pkgData.name) {
            throw new Error('Invalid package.json for ' + target);
        }
        // lodash -> .store/lodash@4.0.0
        // .store/foo@1.0.0/node_modules/lodash -> ../../../.store/lodash@4.0.0
        const out = path.join(modules, pkgData.name);
        yield mkdirp_1.default(path.dirname(out));
        yield link_dir_1.default(target, out);
    });
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = symlinkToModules;
//# sourceMappingURL=symlinkToModules.js.map