"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 = require("path");
const cli_utils_1 = require("@ionic/cli-utils");
/**
 * Get all platforms based on resource/platforms directories
 * TODO: should we get this from the config.xml or just the directories like app-lib
 */
function getProjectPlatforms(projectDir) {
    return __awaiter(this, void 0, void 0, function* () {
        let platformDirContents = [];
        let platformsDir = path.join(projectDir, 'platforms');
        try {
            platformDirContents = yield cli_utils_1.fsReadDir(platformsDir);
        }
        catch (e) {
            if (e.code === 'ENOENT') {
                return [];
            }
            throw e;
        }
        return platformDirContents;
    });
}
exports.getProjectPlatforms = getProjectPlatforms;
/**
 * Returns true or false after checking if any plugin is installed
 * Synchronous
 *
 * @param {String} baseDir The projects base directory
 * @return {Boolean} True if any plugin is installed
 */
function arePluginsInstalled(projectDir) {
    return __awaiter(this, void 0, void 0, function* () {
        let pluginDir = path.join(projectDir, 'plugins');
        let hasPlugins = false;
        try {
            yield cli_utils_1.fsReadDir(pluginDir);
            hasPlugins = true;
        }
        catch (ex) {
            hasPlugins = false;
        }
        return hasPlugins;
    });
}
exports.arePluginsInstalled = arePluginsInstalled;
/**
 * Install ionic required plugins
 *
 * @return {Promise} Promise upon completion
 */
function installPlugins() {
    var plugins = [
        'cordova-plugin-device',
        'cordova-plugin-console',
        'cordova-plugin-whitelist',
        'cordova-plugin-splashscreen',
        'cordova-plugin-statusbar',
        'ionic-plugin-keyboard'
    ];
    return Promise.all(plugins.map(plugin => (new cli_utils_1.Shell().run('cordova', ['plugin', 'add', '--save', plugin]))));
}
exports.installPlugins = installPlugins;
/**
 * Install the platform specified using cordova
 *
 * @param {String} platform The platform to install (ios, android, etc.)
 * @return {Promise} Promise upon completion
 */
function installPlatform(platform) {
    return new cli_utils_1.Shell().run('cordova', ['platform', 'add', platform]);
}
exports.installPlatform = installPlatform;
