"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseStaticPages = void 0;
//#region imports
const lib_1 = require("tnp-core/lib");
const lib_2 = require("tnp-core/lib");
const index_1 = require("../../index");
const base_feature_for_project_1 = require("./base-feature-for-project");
//#endregion
/**
 * Easy way to create github pages
 * (or similar static site hosting)
 */
class BaseStaticPages extends base_feature_for_project_1.BaseFeatureForProject {
    TMP_STATIC_PAGE_SITE_REPOS = 'tmp-static-pages-sites-repos';
    //#region clear folder for gh-pages
    async cleanFolderForStaticPages(pathToBranchRepoFolder) {
        //#region @backendFunc
        const noAllowedToDeleteFiles = [lib_1.config.file._gitignore];
        const noAllowedToDeleteFolders = ['.git'];
        for (const fileAbsPath of lib_1.Helpers.filesFrom(pathToBranchRepoFolder)) {
            if (!noAllowedToDeleteFiles.includes(lib_2.path.basename(fileAbsPath))) {
                lib_1.Helpers.removeIfExists(fileAbsPath);
            }
        }
        for (const folderAbsPath of lib_1.Helpers.foldersFrom(pathToBranchRepoFolder)) {
            if (!noAllowedToDeleteFolders.includes(lib_2.path.basename(folderAbsPath))) {
                lib_1.Helpers.removeFolderIfExists(folderAbsPath);
            }
        }
        //#endregion
    }
    //#endregion
    //#region main folder
    get mainFolder() {
        return this.project.cache['mainFolder'];
    }
    get mainFolderAbsPath() {
        return this.project.pathFor([this.TMP_STATIC_PAGE_SITE_REPOS, this.mainFolder]);
    }
    //#endregion
    //#region pages branch name
    get pagesBranchName() {
        return `${this.mainFolder}-pages`;
    }
    //#endregion
    //#region init
    async init(mainFolder, completeProcess = false) {
        //#region @backendFunc
        this.project.cache['mainFolder'] = mainFolder;
        const tempRepoPath = this.project.pathFor([
            this.TMP_STATIC_PAGE_SITE_REPOS,
            mainFolder,
        ]);
        if (!lib_1.Helpers.exists(tempRepoPath)) {
            await index_1.HelpersTaon.git.clone({
                cwd: this.project.pathFor(this.TMP_STATIC_PAGE_SITE_REPOS),
                url: this.project.git.originURL,
                destinationFolderName: mainFolder,
            });
        }
        index_1.HelpersTaon.git.checkout(tempRepoPath, this.pagesBranchName, {
            createBranchIfNotExists: true,
            switchBranchWhenExists: true,
        });
        await this.cleanFolderForStaticPages(tempRepoPath);
        const currentDocsFolderAbsPath = this.project.pathFor(lib_1.config.folder.docs);
        if (lib_1.Helpers.filesFrom(currentDocsFolderAbsPath).length > 0 &&
            lib_1.Helpers.exists([currentDocsFolderAbsPath, 'index.html'])) {
            index_1.HelpersTaon.copy(currentDocsFolderAbsPath, tempRepoPath, {
                recursive: true,
                overwrite: true,
            });
        }
        if (completeProcess) {
            index_1.HelpersTaon.git.stageAllFiles(tempRepoPath);
            index_1.HelpersTaon.git.commit(tempRepoPath, 'update docs');
            try {
                await index_1.HelpersTaon.git.pushCurrentBranch(tempRepoPath);
            }
            catch (error) {
                lib_1.Helpers.error(`Cannot push to ${this.pagesBranchName} branch`, false, true);
            }
            if (lib_1.Helpers.filesFrom(currentDocsFolderAbsPath).length > 0 &&
                lib_1.Helpers.exists([currentDocsFolderAbsPath, 'index.html'])) {
                lib_1.Helpers.removeFolderIfExists(currentDocsFolderAbsPath);
                lib_1.Helpers.mkdirp(currentDocsFolderAbsPath);
                lib_1.Helpers.writeFile([currentDocsFolderAbsPath, 'info.md'], `# ${this.project.name} docs`);
                this.project.git.addAndCommit(`chore: gh pages branch instead docs`);
                try {
                    await this.project.git.pushCurrentBranch({
                        forcePushNoQuestion: true,
                        force: true,
                    });
                }
                catch (error) {
                    lib_1.Helpers.error(`Cannot push to ${this.pagesBranchName} branch`, false, true);
                }
            }
        }
        //#endregion
    }
}
exports.BaseStaticPages = BaseStaticPages;
//# sourceMappingURL=base-static-pages.js.map