"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ora_1 = __importDefault(require("ora"));
const path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const inquirer_1 = __importDefault(require("inquirer"));
const other_1 = require("../../utils/other");
const git_1 = require("../../utils/git");
const npm_1 = require("../../utils/npm");
const cli_1 = require("../../constant/cli");
const doc_1 = require("../../constant/doc");
const fsEx = require('fs-extra');
const docGit = {
    name: '文档上传模板',
    git: 'https://gitee.com/njsyman/ark-doc.git',
    branch: 'master',
    script: 'npm run storybook', // 项目启动脚本
};
function generateDoc(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const spinner = (0, ora_1.default)('获取文档模板信息').start();
        spinner.text = '';
        // 如果用户清除缓存
        if (options.clearCache) {
            fsEx.removeSync(doc_1.DOC_ROOT_PATH);
        }
        // 判断脚手架里是否存在文档模板
        const isExistsDoc = fsEx.pathExistsSync(doc_1.DOC_ROOT_PATH);
        if (!isExistsDoc) {
            // 下载模板到脚手架的temp目录
            spinner.text = '正在下载模板，请稍等...';
            yield (0, other_1.sleep)();
            const downStatus = (0, git_1.downloadRepo)(docGit, {
                cwd: cli_1.TEMP_PATH,
                stdio: 'ignore', // 屏蔽输出内容
            });
            if (!downStatus) {
                spinner.fail(chalk_1.default.red('模板下载失败'));
                return;
            }
            spinner.succeed(chalk_1.default.green('模板下载成功'));
            // 去掉模板里的git，并安装依赖
            spinner.start('开始安装所需依赖，请稍等...');
            console.log('');
            const gitPath = path_1.default.join(doc_1.DOC_ROOT_PATH, '.git');
            fsEx.removeSync(gitPath);
            const { status: installStatus } = (yield (0, npm_1.npm)(['install'], {
                cwd: doc_1.DOC_ROOT_PATH,
            }));
            if (installStatus !== 0) {
                spinner.fail(chalk_1.default.red('依赖安装失败'));
                return;
            }
            spinner.succeed(chalk_1.default.green('依赖安装成功'));
        }
        // 检查项目里是否已存在.arkdoc目录
        const destPath = path_1.default.join(process.cwd(), doc_1.PROJECT_DOC_ROOT_DIR);
        let isCover = true;
        if (fsEx.pathExistsSync(destPath)) {
            spinner.stop();
            const res = yield inquirer_1.default.prompt({
                type: 'confirm',
                name: 'isCover',
                message: '检测到项目中已存在.arkdoc目录，请确认是否覆盖',
            });
            isCover = res.isCover;
        }
        // 选择不覆盖时。脚手架的文档模板其实已经被更新了。所以可以通过这个方式更新文档模板
        if (!isCover) {
            return;
        }
        // 将文档.arkdoc文件复制到项目组的命令执行目录
        const srcPath = path_1.default.join(doc_1.DOC_ROOT_PATH, doc_1.PROJECT_DOC_ROOT_DIR);
        fsEx.copySync(srcPath, destPath);
        // 复制preview.js,preview-head.html,preview-body.html,和example、assets文件夹
        fsEx.copySync(path_1.default.join(doc_1.STORYBOOK_CONFIG_PATH, './preview.js'), path_1.default.join(doc_1.PROJECT_DOC_CONFIG_PATH, './preview.js'));
        fsEx.copySync(path_1.default.join(doc_1.STORYBOOK_CONFIG_PATH, './preview-head.html'), path_1.default.join(doc_1.PROJECT_DOC_CONFIG_PATH, './preview-head.html'));
        fsEx.copySync(path_1.default.join(doc_1.STORYBOOK_CONFIG_PATH, './preview-body.html'), path_1.default.join(doc_1.PROJECT_DOC_CONFIG_PATH, './preview-body.html'));
        fsEx.copySync(path_1.default.join(doc_1.STORYBOOK_SRC_PATH, `./${doc_1.DOC_EXAMPLES_DIR}`), path_1.default.join(doc_1.PROJECT_DOC_ROOT_DIR, `./${doc_1.DOC_EXAMPLES_DIR}`));
        fsEx.copySync(path_1.default.join(doc_1.STORYBOOK_SRC_PATH, `./${doc_1.DOC_ASSETS_DIR}`), path_1.default.join(doc_1.PROJECT_DOC_ROOT_DIR, `./${doc_1.DOC_ASSETS_DIR}`));
        console.log('');
        spinner.succeed(chalk_1.default.green('已在项目中生成文档文件.arkdoc。执行 ark preview --doc 命令即可预览文档'));
    });
}
exports.default = generateDoc;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZG9jLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vY29tbWFuZHMvZ2VuZXJhdGUvZG9jLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7O0FBQUEsOENBQXFCO0FBQ3JCLGdEQUF1QjtBQUN2QixrREFBeUI7QUFDekIsd0RBQStCO0FBQy9CLDZDQUF5QztBQUN6Qyx5Q0FBOEM7QUFDOUMseUNBQXFDO0FBQ3JDLDRDQUE4QztBQUM5Qyw0Q0FRMkI7QUFDM0IsTUFBTSxJQUFJLEdBQUcsT0FBTyxDQUFDLFVBQVUsQ0FBQyxDQUFBO0FBRWhDLE1BQU0sTUFBTSxHQUFHO0lBQ2IsSUFBSSxFQUFFLFFBQVE7SUFDZCxHQUFHLEVBQUUsdUNBQXVDO0lBQzVDLE1BQU0sRUFBRSxRQUFRO0lBQ2hCLE1BQU0sRUFBRSxtQkFBbUIsRUFBRSxTQUFTO0NBQ3ZDLENBQUE7QUFFRCxTQUFlLFdBQVcsQ0FBQyxPQUFZOztRQUNyQyxNQUFNLE9BQU8sR0FBRyxJQUFBLGFBQUcsRUFBQyxVQUFVLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtRQUN2QyxPQUFPLENBQUMsSUFBSSxHQUFHLEVBQUUsQ0FBQTtRQUVqQixXQUFXO1FBQ1gsSUFBSSxPQUFPLENBQUMsVUFBVSxFQUFFO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