"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_1 = require("path");
const path = require("path");
const spawn = require("cross-spawn");
const getTarballName_1 = require("./getTarballName");
const requireJson_1 = require("../fs/requireJson");
const mkdirp_1 = require("../fs/mkdirp");
const fetch_1 = require("./fetch");
const fs = require("mz/fs");
const link_dir_1 = require("link-dir");
/**
 * Resolves a package hosted on the local filesystem
 */
function resolveLocal(spec, opts) {
    return __awaiter(this, void 0, void 0, function* () {
        const dependencyPath = path_1.resolve(opts.root, spec.spec);
        if (dependencyPath.slice(-4) === '.tgz' || dependencyPath.slice(-7) === '.tar.gz') {
            const name = getTarballName_1.default(dependencyPath);
            return {
                id: createLocalPkgId(name, dependencyPath),
                root: path.dirname(dependencyPath),
                fetch: fetch_1.createLocalTarballFetcher({
                    tarball: dependencyPath
                })
            };
        }
        if (opts.linkLocal) {
            const localPkg = yield requireJson_1.default(path_1.resolve(dependencyPath, 'package.json'));
            return {
                id: createLocalPkgId(localPkg.name, dependencyPath),
                root: dependencyPath,
                fetch: function (target) {
                    return __awaiter(this, void 0, void 0, function* () {
                        yield mkdirp_1.default(path.dirname(target));
                        return link_dir_1.default(dependencyPath, target);
                    });
                }
            };
        }
        return resolveFolder(dependencyPath);
    });
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = resolveLocal;
function resolveFolder(dependencyPath) {
    return __awaiter(this, void 0, void 0, function* () {
        const tgzFilename = yield new Promise((resolve, reject) => {
            const proc = spawn('npm', ['pack'], {
                cwd: dependencyPath
            });
            let stdout = '';
            proc.stdout.on('data', (data) => {
                stdout += data.toString();
            });
            proc.on('error', reject);
            proc.on('close', (code) => {
                if (code > 0)
                    return reject(new Error('Exit code ' + code));
                const tgzFilename = stdout.trim();
                return resolve(tgzFilename);
            });
        });
        const localPkg = yield requireJson_1.default(path_1.resolve(dependencyPath, 'package.json'));
        const dist = {
            tarball: path_1.resolve(dependencyPath, tgzFilename)
        };
        return {
            id: createLocalPkgId(localPkg.name, dependencyPath),
            root: dependencyPath,
            fetch: function (target) {
                return __awaiter(this, void 0, void 0, function* () {
                    yield fetch_1.fetchFromLocalTarball(target, dist);
                    return fs.unlink(dist.tarball);
                });
            }
        };
    });
}
function createLocalPkgId(name, dependencyPath) {
    return 'local/' + encodeURIComponent(dependencyPath);
}
//# sourceMappingURL=local.js.map