//#region imports
import { path, ___NS__cloneDeep, ___NS__first, ___NS__isUndefined, ___NS__merge, ___NS__startCase, ___NS__upperFirst, Helpers__NS__error, Helpers__NS__filesFrom } from 'tnp-core/lib-prod';
import { CLASS__NS__getMethodsNames } from 'typescript-class-helpers/lib-prod';
import { HelpersTaon__NS__getValueFromJSON } from '../index';
//#endregion
//#region constants
//#region constants / defult db
const defaultDb = {
    projects: [],
};
//#endregion
export class CoreProject {
    //#region static
    //#region static / core projects
    static coreProjects = [];
    //#endregion
    //#region static / from
    static from(options) {
        //#region @backendFunc
        const proj = ___NS__merge(new CoreProject(), ___NS__cloneDeep(options));
        const methodsToCheck = [
            ...CLASS__NS__getMethodsNames(CoreProject),
            ...CLASS__NS__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] ||
                    (() => {
                        Helpers__NS__error(`${___NS__upperFirst(___NS__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 path.basename(this.url).replace(/\.git$/, '');
    }
    /**
     * main (default for reset) branch
     */
    get branch() {
        return ___NS__first(this.branches);
    }
    /**
     * url for git repo
     */
    get url() {
        return (this.urlHttp ? this.urlHttp : this.urlSSH) || '';
    }
}
export const CoreTypescriptProject = CoreProject.from({
    //#region configuration
    branches: ['master', 'develop'],
    environments: [],
    recognizedFn: project => {
        //#region @backendFunc
        return (!project.hasFile('angular.json') &&
            !___NS__isUndefined(Helpers__NS__filesFrom(project.location, false).find(f => path.basename(f).startsWith('tsconfig'))));
        //#endregion
    },
    async startCommand({ project }) {
        //#region @backendFunc
        await project.nodeModules.makeSureInstalled();
        const mainFilePath = HelpersTaon__NS__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
});
export const CoreAngularProject = CoreProject.from({
    //#region configuration
    extends: 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=c:/Users/darek/projects/npm/taon-dev/tnp-helpers/dist-nocutsrc-prod/lib/base/core-project.js.map