"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocsPages = void 0;
const fs = __importStar(require("fs"));
const commander_1 = require("commander");
const yaml_1 = __importDefault(require("yaml"));
const shell = __importStar(require("shelljs"));
const chalk_1 = __importDefault(require("chalk"));
const git_utils_1 = require("../utils/git.utils");
class DocsPages {
    constructor() {
        this.DOCS_PAGES_STAGE = 'docs-pages';
        this.DOCS_PAGES_INCLUDE = {
            project: 'infrastructure/gitlab-ci-templates',
            file: '/templates/docs-retype.yml',
        };
    }
    setup() {
        commander_1.program
            .command('docs-pages')
            .description('Setup docs for GitLab pages')
            .action(async () => {
            if (git_utils_1.GitUtils.logErrorIfNotInGitProject())
                return;
            this.updateGitLabCi();
            this.repoGitLabInfo = this.getRepoInfo();
            this.mkdirDocs();
            this.logResult();
        });
    }
    updateGitLabCi() {
        let gitlabCiYaml;
        const hasGitlabCi = fs.existsSync('.gitlab-ci.yml');
        if (!hasGitlabCi) {
            gitlabCiYaml = {};
        }
        else {
            gitlabCiYaml = yaml_1.default.parse(fs.readFileSync('./.gitlab-ci.yml').toString());
        }
        if (!(typeof gitlabCiYaml.stages?.includes === 'function' &&
            gitlabCiYaml.stages.includes(this.DOCS_PAGES_STAGE))) {
            gitlabCiYaml.stages = [...gitlabCiYaml.stages, this.DOCS_PAGES_STAGE];
        }
        if (!(typeof gitlabCiYaml.include?.find === 'function' &&
            gitlabCiYaml.include?.find((inc) => inc.project === this.DOCS_PAGES_INCLUDE.project &&
                inc.file === this.DOCS_PAGES_INCLUDE.file))) {
            gitlabCiYaml.include = [...(gitlabCiYaml.include || []), this.DOCS_PAGES_INCLUDE];
        }
        fs.writeFileSync('./.gitlab-ci.yml', yaml_1.default.stringify(gitlabCiYaml));
    }
    getRepoInfo() {
        const gitRemoteUrl = shell.exec('git remote get-url origin', { silent: true }).stdout;
        const splitRemoteUrl = gitRemoteUrl.split('/');
        const projectName = splitRemoteUrl.pop()?.split('.')[0];
        const groupName = splitRemoteUrl.pop();
        return {
            groupName,
            projectName,
            pagesUrl: `https://${groupName}.pages.tapsell.ir/${projectName}`,
        };
    }
    mkdirDocs() {
        const dir = './docs';
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir);
        }
        fs.writeFileSync(`${dir}/retype.yml`, `
input: .
output: ../public
branding:
  title: ${this.repoGitLabInfo?.projectName ?? 'Project Docs'}
edit: true`);
    }
    logResult() {
        const pagesUrl = this.repoGitLabInfo?.pagesUrl;
        console.log(`

    ${chalk_1.default.green('Successful! Docs are implemented')}
    -------------
    - Create ".md" files in the "docs" folder
    - Your doc will be deployed when you push to the ${chalk_1.default.blue('master')} branch.

    Docs URL: ${pagesUrl}

    - Please update the README.md file and add this URL:
          **View [Documentation](${pagesUrl})**
    `);
    }
}
exports.DocsPages = DocsPages;
//# sourceMappingURL=docs-pages.js.map