"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const LocalDevServerConfiguration_1 = __importDefault(require("../user/LocalDevServerConfiguration"));
const SfdxProject_1 = __importDefault(require("./SfdxProject"));
const Constants_1 = require("../server/Constants");
class Project {
    constructor(directory) {
        this._isSFDX = false;
        const rootDirectory = this.resolveProjectDirectory(directory);
        if (rootDirectory === null) {
            throw new Error(`Directory specified '${directory}' does not resolve to a project. The specified directory must have package.json or sfdx-project.json in it.`);
        }
        this.rootDirectory = rootDirectory;
        const configurationPath = path_1.default.join(this.rootDirectory, 'localdevserver.config.json');
        this._configuration = new LocalDevServerConfiguration_1.default(configurationPath);
        this._isSFDX = SfdxProject_1.default.isSfdxProjectJsonPresent(this.rootDirectory);
        if (this._isSFDX) {
            new SfdxProject_1.default(this._configuration, this.rootDirectory).initWithSfdxConfiguration();
        }
    }
    get configuration() {
        return this._configuration;
    }
    get isSfdx() {
        return this._isSFDX;
    }
    get modulesSourceDirectory() {
        var srcDir = path_1.default.isAbsolute(this.configuration.modulesSourceDirectory)
            ? this.configuration.modulesSourceDirectory
            : path_1.default.join(this.rootDirectory, this.configuration.modulesSourceDirectory || 'src');
        if (!fs_extra_1.default.existsSync(srcDir) || !fs_extra_1.default.lstatSync(srcDir).isDirectory()) {
            console.warn(`modules source directory '${srcDir}' does not exist`);
        }
        return srcDir;
    }
    get staticResourcesDirectories() {
        const staticResourceDirectoriesResults = [];
        if (!Array.isArray(this.configuration.staticResourcesDirectories)) {
            console.warn('staticResourcesDirectories must be provided in a list format');
            return staticResourceDirectoriesResults;
        }
        this.configuration.staticResourcesDirectories.forEach(staticResourceDirectory => {
            if (path_1.default.isAbsolute(staticResourceDirectory)) {
                staticResourceDirectoriesResults.push(staticResourceDirectory);
            }
            else {
                staticResourceDirectoriesResults.push(path_1.default.join(this.rootDirectory, staticResourceDirectory));
            }
        });
        return staticResourceDirectoriesResults;
    }
    get customLabelsPath() {
        return this.getConfigurationPath(this.configuration.customLabelsFile);
    }
    get contentAssetsDirectory() {
        var dir = this.getConfigurationPath(this.configuration.contentAssetsDirectory);
        if (dir && (!fs_extra_1.default.existsSync(dir) || !fs_extra_1.default.lstatSync(dir).isDirectory())) {
            console.warn(`content assets directory '${dir}' does not exist`);
            return undefined;
        }
        return dir;
    }
    get directory() {
        return this.rootDirectory;
    }
    get port() {
        return this.configuration.port;
    }
    resolveProjectDirectory(directory, previousDirectory) {
        let currentDirectory = directory;
        if (currentDirectory === previousDirectory) {
            return null;
        }
        if (currentDirectory === '.' || currentDirectory === '') {
            currentDirectory = process.cwd();
        }
        if (!fs_extra_1.default.existsSync(currentDirectory)) {
            return null;
        }
        if (!fs_extra_1.default.existsSync(path_1.default.join(currentDirectory, 'package.json')) &&
            !fs_extra_1.default.existsSync(path_1.default.join(currentDirectory, Constants_1.SFDX_PROJECT_JSON))) {
            return this.resolveProjectDirectory(path_1.default.join(currentDirectory, '..'), currentDirectory);
        }
        return currentDirectory;
    }
    getConfigurationPath(config) {
        if (path_1.default.isAbsolute(config)) {
            return config;
        }
        if (config !== '') {
            return path_1.default.join(this.rootDirectory, config);
        }
    }
}
exports.default = Project;
//# sourceMappingURL=Project.js.map