"use strict";
const os_1 = require("os");
class InstallCommand {
    constructor($platformsData, $platformService, $projectData, $projectDataService, $pluginsService, $logger, $fs, $stringParameter, $npm) {
        this.$platformsData = $platformsData;
        this.$platformService = $platformService;
        this.$projectData = $projectData;
        this.$projectDataService = $projectDataService;
        this.$pluginsService = $pluginsService;
        this.$logger = $logger;
        this.$fs = $fs;
        this.$stringParameter = $stringParameter;
        this.$npm = $npm;
        this.enableHooks = false;
        this.allowedParameters = [this.$stringParameter];
    }
    execute(args) {
        return args[0] ? this.installModule(args[0]) : this.installProjectDependencies();
    }
    installProjectDependencies() {
        return (() => {
            let error = "";
            this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
            this.$projectDataService.initialize(this.$projectData.projectDir);
            _.each(this.$platformsData.platformsNames, platform => {
                let platformData = this.$platformsData.getPlatformData(platform);
                let frameworkPackageData = this.$projectDataService.getValue(platformData.frameworkPackageName).wait();
                if (frameworkPackageData && frameworkPackageData.version) {
                    try {
                        this.$platformService.addPlatforms([`${platform}@${frameworkPackageData.version}`]).wait();
                    }
                    catch (err) {
                        error = `${error}${os_1.EOL}${err}`;
                    }
                }
            });
            if (error) {
                this.$logger.error(error);
            }
        }).future()();
    }
    installModule(moduleName) {
        return (() => {
            let projectDir = this.$projectData.projectDir;
            let devPrefix = 'nativescript-dev-';
            if (!this.$fs.exists(moduleName).wait() && moduleName.indexOf(devPrefix) !== 0) {
                moduleName = devPrefix + moduleName;
            }
            this.$npm.install(moduleName, projectDir, { 'save-dev': true }).wait();
        }).future()();
    }
}
exports.InstallCommand = InstallCommand;
$injector.registerCommand("install", InstallCommand);
