"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const utils_1 = require("../utils");
const path = require("path");
class PackageVersion {
    constructor(major, minor, patch) {
        this.major = major;
        this.minor = minor;
        this.patch = patch;
    }
    static initialize(mainRepo) {
        if (PackageVersion._version !== undefined) {
            throw new Error(`(PackageVersion) version has already been initialized`);
        }
        const packagePath = path.resolve(mainRepo, PackageVersion.PACKAGE_JSON);
        if (!fs.existsSync(packagePath)) {
            console.warn(utils_1.cwarn `[NPM] Could not find package.json in repo ${mainRepo}...`);
            console.warn(utils_1.cwarn `[NPM] -> Assuming version 1.0.0`);
            PackageVersion._version = new PackageVersion(1, 0, 0);
        }
        else {
            const packageJson_String = fs.readFileSync(packagePath, 'utf8');
            const packageJson = JSON.parse(packageJson_String);
            const versionString = packageJson.version;
            const versionParts = versionString.split('.');
            if (versionParts.length !== 3) {
                console.error(utils_1.cerr `[NPM] Expected package.json "version" to consist of 3 parts`);
                throw new Error(`[NPM] Expected package.json "version" to consist of 3 parts`);
            }
            PackageVersion._version = new PackageVersion(parseInt(versionParts[0]), parseInt(versionParts[1]), parseInt(versionParts[2]));
        }
        return PackageVersion._version;
    }
    static get() {
        if (PackageVersion._version === undefined) {
            throw new Error(`(PackageVersion) version has not yet been initialized`);
        }
        return PackageVersion._version;
    }
}
PackageVersion.PACKAGE_JSON = 'package.json';
PackageVersion._version = undefined;
exports.PackageVersion = PackageVersion;
//# sourceMappingURL=PackageVersion.js.map