"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StaticConfig = exports.Configuration = void 0;
const path = require("path");
const shelljs = require("shelljs");
const os = require("os");
const _ = require("lodash");
const yok_1 = require("./common/yok");
class Configuration {
    constructor($fs) {
        this.$fs = $fs;
        this.DEBUG = false;
        this.ANDROID_DEBUG_UI = null;
        this.USE_POD_SANDBOX = false;
        this.GA_TRACKING_ID = null;
        this.DISABLE_HOOKS = false;
        _.extend(this, this.loadConfig("config"));
    }
    loadConfig(name) {
        const configFileName = this.getConfigPath(name);
        return this.$fs.readJson(configFileName);
    }
    getConfigPath(filename) {
        return path.join(__dirname, "../config", filename + ".json");
    }
}
exports.Configuration = Configuration;
yok_1.injector.register("config", Configuration);
class StaticConfig {
    get PROFILE_DIR_NAME() {
        return ".nativescript-cli";
    }
    constructor($injector) {
        this.$injector = $injector;
        this.QR_SIZE = 5;
        this.PROJECT_FILE_NAME = "package.json";
        this.CLIENT_NAME_KEY_IN_PROJECT_FILE = "nativescript";
        this.CLIENT_NAME = "tns";
        this.CLIENT_NAME_ALIAS = "NativeScript";
        this.TRACK_FEATURE_USAGE_SETTING_NAME = "TrackFeatureUsage";
        this.ERROR_REPORT_SETTING_NAME = "TrackExceptions";
        this.ANALYTICS_INSTALLATION_ID_SETTING_NAME = "AnalyticsInstallationID";
        this.RESOURCE_DIR_PATH = path.join(__dirname, "..", "resources");
        this.version = require("../package.json").version;
        this.cliBinPath = path.resolve(__dirname, "..", "bin", "nativescript.js");
        this._adbFilePath = null;
        this._userAgent = null;
    }
    get disableCommandHooks() {
        return true;
    }
    get HTML_CLI_HELPERS_DIR() {
        return path.join(__dirname, "../docs/helpers");
    }
    get pathToPackageJson() {
        return path.join(__dirname, "..", "package.json");
    }
    get PATH_TO_BOOTSTRAP() {
        return path.join(__dirname, "bootstrap.js");
    }
    getAdbFilePath() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this._adbFilePath) {
                const androidToolsInfo = this.$injector.resolve("androidToolsInfo");
                this._adbFilePath =
                    (yield androidToolsInfo.getPathToAdbFromAndroidHome()) ||
                        (yield this.getAdbFilePathCore());
            }
            return this._adbFilePath;
        });
    }
    get USER_AGENT_NAME() {
        if (!this._userAgent) {
            this._userAgent = `${this.CLIENT_NAME}CLI`;
        }
        return this._userAgent;
    }
    set USER_AGENT_NAME(userAgentName) {
        this._userAgent = userAgentName;
    }
    get MAN_PAGES_DIR() {
        return path.join(__dirname, "..", "docs", "man_pages");
    }
    get HTML_PAGES_DIR() {
        return path.join(__dirname, "..", "docs", "html");
    }
    get HTML_COMMON_HELPERS_DIR() {
        return path.join(__dirname, "common", "docs", "helpers");
    }
    getAdbFilePathCore() {
        return __awaiter(this, void 0, void 0, function* () {
            const $childProcess = this.$injector.resolve("$childProcess");
            try {
                const proc = yield $childProcess.spawnFromEvent("adb", ["version"], "exit", undefined, { throwError: false });
                if (proc.stderr) {
                    return yield this.spawnPrivateAdb();
                }
            }
            catch (e) {
                if (e.code === "ENOENT") {
                    return yield this.spawnPrivateAdb();
                }
            }
            return "adb";
        });
    }
    spawnPrivateAdb() {
        return __awaiter(this, void 0, void 0, function* () {
            const $fs = this.$injector.resolve("$fs"), $childProcess = this.$injector.resolve("$childProcess"), $hostInfo = this.$injector.resolve("$hostInfo");
            const defaultAdbDirPath = path.join(__dirname, "common", "resources", "platform-tools", "android", process.platform);
            const pathToPackageJson = path.join(__dirname, "..", "package.json");
            const nsCliVersion = require(pathToPackageJson).version;
            const tmpDir = path.join(os.tmpdir(), `nativescript-cli-${nsCliVersion}`);
            $fs.createDirectory(tmpDir);
            const targetAdb = path.join(tmpDir, "adb");
            if (!$fs.exists(tmpDir) || !$fs.readDirectory(tmpDir).length) {
                shelljs.cp(path.join(defaultAdbDirPath, "*"), tmpDir);
                if (!$hostInfo.isWindows) {
                    shelljs.chmod("+x", targetAdb);
                }
            }
            yield $childProcess.spawnFromEvent(targetAdb, ["start-server"], "exit");
            return targetAdb;
        });
    }
}
exports.StaticConfig = StaticConfig;
yok_1.injector.register("staticConfig", StaticConfig);
