"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 canLink = require("can-link");
const mkdirp = require("mkdirp-promise");
const fs = require("mz/fs");
const osHomedir = require("os-homedir");
const path = require("path");
const pathAbsolute = require("path-absolute");
const pathTemp = require("path-temp");
const rootLinkTarget = require("root-link-target");
const touch = require("touch");
const STORE_VERSION = '2';
function default_1(pkgRoot, storePath) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!storePath || isHomepath(storePath)) {
            const relStorePath = storePath ? storePath.substr(2) : '.pnpm-store';
            return yield storePathRelativeToHome(pkgRoot, relStorePath);
        }
        const storeBasePath = pathAbsolute(storePath, pkgRoot);
        if (storeBasePath.endsWith(`${path.sep}${STORE_VERSION}`)) {
            return storeBasePath;
        }
        return path.join(storeBasePath, STORE_VERSION);
    });
}
exports.default = default_1;
function storePathRelativeToHome(pkgRoot, relStore) {
    return __awaiter(this, void 0, void 0, function* () {
        const tempFile = pathTemp(pkgRoot);
        yield mkdirp(path.dirname(tempFile));
        yield touch(tempFile);
        const homedir = getHomedir();
        if (yield canLink(tempFile, path.join(homedir, path.basename(tempFile)))) {
            yield fs.unlink(tempFile);
            // If the project is on the drive on which the OS home directory
            // then the store is placed in the home directory
            return path.join(homedir, relStore, STORE_VERSION);
        }
        try {
            const mountpoint = yield rootLinkTarget(tempFile);
            // If linking works only in the project folder
            // then prefer to place the store inside the homedir
            if (path.relative(pkgRoot, mountpoint) === '.') {
                return path.join(homedir, relStore, STORE_VERSION);
            }
            return path.join(mountpoint, relStore, STORE_VERSION);
        }
        catch (err) {
            // this is an unlikely situation but if there is no way to find
            // a linkable place on the disk, create the store in homedir
            return path.join(homedir, relStore, STORE_VERSION);
        }
        finally {
            yield fs.unlink(tempFile);
        }
    });
}
function getHomedir() {
    const home = osHomedir();
    if (!home)
        throw new Error('Could not find the homedir');
    return home;
}
function isHomepath(filepath) {
    return filepath.indexOf('~/') === 0 || filepath.indexOf('~\\') === 0;
}
//# sourceMappingURL=index.js.map