"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginDescriptorParser = void 0;
const path = require("path");
const fs = require("fs");
const utils_1 = require("../utils");
const CplacePlugin_1 = require("./CplacePlugin");
class PluginDescriptorParser {
    constructor(pluginDir) {
        this.pluginDir = pluginDir;
        this.pathToDescriptor = CplacePlugin_1.default.getPathToDescriptor(pluginDir);
        if (!fs.existsSync(this.pathToDescriptor)) {
            console.error(utils_1.cerr `(PluginDescriptor) Failed to find plugin descriptor for ${path.basename(pluginDir)}`);
            console.error();
            console.error(utils_1.cerr `(PluginDescriptor) Try running "gradle generatePluginDescriptor" in the containing repository`);
            console.error();
            throw Error(`PluginDescriptor ${this.pathToDescriptor} does not exist`);
        }
        this.descriptor = this.parseFile();
    }
    getPluginDescriptor() {
        return this.descriptor;
    }
    parseFile() {
        var _a;
        const content = fs.readFileSync(this.pathToDescriptor, {
            encoding: 'utf8',
        });
        const pluginDescriptor = JSON.parse(content);
        if (((_a = pluginDescriptor === null || pluginDescriptor === void 0 ? void 0 : pluginDescriptor.dependencies) === null || _a === void 0 ? void 0 : _a.length) &&
            (typeof pluginDescriptor.dependencies[0] === 'string' ||
                pluginDescriptor.dependencies[0] instanceof String)) {
            const newDependencies = [];
            pluginDescriptor.dependencies.forEach((dependency) => {
                newDependencies.push({
                    name: dependency + '',
                });
            });
            pluginDescriptor.dependencies.splice(0);
            pluginDescriptor.dependencies.push(...newDependencies);
        }
        return pluginDescriptor;
    }
}
exports.PluginDescriptorParser = PluginDescriptorParser;
//# sourceMappingURL=PluginDescriptorParser.js.map