"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoreAngularProject = exports.CoreTypescriptProject = exports.CoreProject = void 0;
//#region imports
const lib_1 = require("tnp-core/lib");
const lib_2 = require("typescript-class-helpers/lib");
const index_1 = require("../index");
//#endregion
//#region constants
//#region constants / defult db
const defaultDb = {
    projects: [],
};
//#endregion
class CoreProject {
    //#region static
    //#region static / core projects
    static coreProjects = [];
    //#endregion
    //#region static / from
    static from(options) {
        //#region @backendFunc
        const proj = lib_1._.merge(new CoreProject(), lib_1._.cloneDeep(options));
        const methodsToCheck = [
            ...lib_2.CLASS.getMethodsNames(CoreProject),
            ...lib_2.CLASS.getMethodsNames(proj.extends || {}),
            // .filter(f => f.endsWith('Command')),
        ];
        // console.log('methodsToCheck '+ proj.name, methodsToCheck)
        for (const commandName of methodsToCheck) {
            // console.log('initing commandName', commandName)
            proj[commandName] =
                proj[commandName] ||
                    proj?.extends[commandName] ||
                    (() => {
                        lib_1.Helpers.error(`${lib_1._.upperFirst(lib_1._.startCase(commandName))} not defined for ${proj.name}`);
                    });
        }
        this.coreProjects.push(proj);
        return proj;
        //#endregion
    }
    //#endregion
    //#endregion
    //#region constructor
    constructor() { }
    //#endregion
    //#region properties
    extends;
    color;
    /**
     * second color for project (optional)
     */
    secondColor;
    /**
     * second color for project (optional)
     */
    thirdColor;
    npmRegistry;
    description;
    /**
     * ssh url for git repo
     */
    urlSSH;
    /**
     * https url for git repo
     */
    urlHttp;
    /**
     * main branches - first is default
     */
    branches;
    /**
     * project environments
     */
    environments = [];
    //#endregion
    //#region methods & getters
    cleanCommand;
    initCommand;
    /**
     * Build whatever is needed to run project
     * buildCommand includes initCommand
     */
    buildCommand;
    /**
     * startCommand includes initCommand, buildCommand and launchCommand
     */
    startCommand;
    /**
     * launch already built project
     */
    launchCommand;
    releaseCommand;
    publishCommand;
    deployCommand;
    testCommand;
    docsCommand;
    /**
     * function to recognize project
     */
    recognizedFn;
    /**
     * core porject name from repo basename
     */
    get name() {
        return lib_1.path.basename(this.url).replace(/\.git$/, '');
    }
    /**
     * main (default for reset) branch
     */
    get branch() {
        return lib_1._.first(this.branches);
    }
    /**
     * url for git repo
     */
    get url() {
        return (this.urlHttp ? this.urlHttp : this.urlSSH) || '';
    }
}
exports.CoreProject = CoreProject;
exports.CoreTypescriptProject = CoreProject.from({
    //#region configuration
    branches: ['master', 'develop'],
    environments: [],
    recognizedFn: project => {
        //#region @backendFunc
        return (!project.hasFile('angular.json') &&
            !lib_1._.isUndefined(lib_1.Helpers.filesFrom(project.location, false).find(f => lib_1.path.basename(f).startsWith('tsconfig'))));
        //#endregion
    },
    async startCommand({ project }) {
        //#region @backendFunc
        await project.nodeModules.makeSureInstalled();
        const mainFilePath = index_1.HelpersTaon.getValueFromJSON(project.pathFor('package.json'), 'main');
        project.run(`node ${mainFilePath}`).sync();
        //#endregion
    },
    async buildCommand({ project, watch }) {
        //#region @backendFunc
        // console.log('Building typescript project');
        // process.exit(0)
        await project.nodeModules.makeSureInstalled();
        project.run(`npm-run tsc ${watch ? '--watch' : ''}`).sync();
        //#endregion
    },
    //#endregion
});
exports.CoreAngularProject = CoreProject.from({
    //#region configuration
    extends: exports.CoreTypescriptProject,
    branches: ['master', 'develop'],
    environments: [],
    recognizedFn: project => {
        return project.hasFile('angular.json');
    },
    async startCommand({ project }) {
        await project.npmHelpers.nodeModules.makeSureInstalled();
        const port = await project.assignFreePort(4200);
        project.run(`npm-run ng serve --port ${port}`).sync();
    },
    async buildCommand({ project }) {
        await project.npmHelpers.nodeModules.makeSureInstalled();
        project.run(`npm-run ng build`).sync();
    },
    //#endregion
});
//# sourceMappingURL=core-project.js.map