"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstallCommand = void 0;
const os_1 = require("os");
const yok_1 = require("../common/yok");
class InstallCommand {
    constructor($options, $mobileHelper, $platformsDataService, $platformCommandHelper, $projectData, $projectDataService, $pluginsService, $logger, $fs, $stringParameter, $packageManager) {
        this.$options = $options;
        this.$mobileHelper = $mobileHelper;
        this.$platformsDataService = $platformsDataService;
        this.$platformCommandHelper = $platformCommandHelper;
        this.$projectData = $projectData;
        this.$projectDataService = $projectDataService;
        this.$pluginsService = $pluginsService;
        this.$logger = $logger;
        this.$fs = $fs;
        this.$stringParameter = $stringParameter;
        this.$packageManager = $packageManager;
        this.enableHooks = false;
        this.allowedParameters = [this.$stringParameter];
        this.$projectData.initializeProjectData();
    }
    async execute(args) {
        return args[0]
            ? this.installModule(args[0])
            : this.installProjectDependencies();
    }
    async installProjectDependencies() {
        let error = "";
        await this.$pluginsService.ensureAllDependenciesAreInstalled(this.$projectData);
        for (const platform of this.$mobileHelper.platformNames) {
            const platformData = this.$platformsDataService.getPlatformData(platform, this.$projectData);
            const frameworkPackageData = this.$projectDataService.getRuntimePackage(this.$projectData.projectDir, platformData.platformNameLowerCase);
            if (frameworkPackageData && frameworkPackageData.version) {
                try {
                    const platformProjectService = platformData.platformProjectService;
                    await platformProjectService.validate(this.$projectData, this.$options);
                    await this.$platformCommandHelper.addPlatforms([`${platform}@${frameworkPackageData.version}`], this.$projectData, this.$options.frameworkPath);
                }
                catch (err) {
                    error = `${error}${os_1.EOL}${err}`;
                }
            }
        }
        if (error) {
            this.$logger.error(error);
        }
    }
    async installModule(moduleName) {
        const projectDir = this.$projectData.projectDir;
        const devPrefix = "nativescript-dev-";
        if (!this.$fs.exists(moduleName) && moduleName.indexOf(devPrefix) !== 0) {
            moduleName = devPrefix + moduleName;
        }
        await this.$packageManager.install(moduleName, projectDir, {
            "save-dev": true,
            disableNpmInstall: this.$options.disableNpmInstall,
            frameworkPath: this.$options.frameworkPath,
            ignoreScripts: this.$options.ignoreScripts,
            path: this.$options.path,
        });
    }
}
exports.InstallCommand = InstallCommand;
yok_1.injector.registerCommand("install", InstallCommand);
