"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 pacote = require("pacote");
const tar = require("tar");
const path = require("path");
class PacoteService {
    constructor($fs, $npm, $proxyService, $logger) {
        this.$fs = $fs;
        this.$npm = $npm;
        this.$proxyService = $proxyService;
        this.$logger = $logger;
    }
    manifest(packageName, options) {
        return __awaiter(this, void 0, void 0, function* () {
            this.$logger.trace(`Calling pacoteService.manifest for packageName: '${packageName}' and options: ${options}`);
            const manifestOptions = yield this.getPacoteBaseOptions();
            if (options) {
                _.extend(manifestOptions, options);
            }
            packageName = this.getRealPackageName(packageName);
            this.$logger.trace(`Calling pacote.manifest for packageName: ${packageName} and options: ${JSON.stringify(manifestOptions, null, 2)}`);
            return pacote.manifest(packageName, manifestOptions);
        });
    }
    extractPackage(packageName, destinationDirectory, options) {
        return __awaiter(this, void 0, void 0, function* () {
            this.$logger.trace(`Calling pacoteService.extractPackage for packageName: '${packageName}', destinationDir: '${destinationDirectory}' and options: ${options}`);
            const extractOptions = { strip: 1, C: destinationDirectory };
            if (options) {
                _.extend(extractOptions, options);
            }
            packageName = this.getRealPackageName(packageName);
            const pacoteOptions = yield this.getPacoteBaseOptions();
            return new Promise((resolve, reject) => {
                this.$logger.trace(`Calling pacoteService.extractPackage for packageName: '${packageName}', destinationDir: '${destinationDirectory}' and options: ${options}`);
                const source = pacote.tarball.stream(packageName, pacoteOptions);
                source.on("error", (err) => {
                    this.$logger.trace(`Error in source while trying to extract stream from ${packageName}. Error is ${err}`);
                    reject(err);
                });
                this.$logger.trace(`Creating extract tar stream with options: ${JSON.stringify(extractOptions, null, 2)}`);
                const destination = tar.x(extractOptions);
                source.pipe(destination);
                destination.on("error", (err) => {
                    this.$logger.trace(`Error in destination while trying to extract stream from ${packageName}. Error is ${err}`);
                    reject(err);
                });
                destination.on("finish", () => {
                    this.$logger.trace(`Successfully extracted '${packageName}' to ${destinationDirectory}`);
                    resolve();
                });
            });
        });
    }
    getPacoteBaseOptions() {
        return __awaiter(this, void 0, void 0, function* () {
            const cache = yield this.$npm.getCachePath();
            const pacoteOptions = { cache };
            const proxySettings = yield this.$proxyService.getCache();
            if (proxySettings) {
                _.extend(pacoteOptions, proxySettings);
            }
            return pacoteOptions;
        });
    }
    getRealPackageName(packageName) {
        if (this.$fs.exists(packageName)) {
            this.$logger.trace(`Will resolve the full path to package ${packageName}.`);
            packageName = path.resolve(packageName);
        }
        return packageName;
    }
}
exports.PacoteService = PacoteService;
$injector.register("pacoteService", PacoteService);
