"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 doc_1 = require("../../constant/doc");
const npm_1 = require("../../utils/npm");
const doc_2 = require("../../constant/doc");
const doc_methods_1 = require("../../utils/doc-methods");
const rimraf_1 = __importDefault(require("rimraf"));
const puppeteer_1 = __importDefault(require("puppeteer"));
const fsEx = require('fs-extra');
// 封面截图
function screenshot() {
    return __awaiter(this, void 0, void 0, function* () {
        const browser = yield puppeteer_1.default.launch({
            // headless: false,
            defaultViewport: {
                width: 1024,
                height: 4000,
            },
        });
        const page = yield browser.newPage();
        const configPath = path_1.default.join(doc_2.STORYBOOK_CONFIG_PATH, 'main.js');
        const config = require(configPath);
        const pageUrl = `file://${doc_2.PROJECT_DOC_DIST_PATH}/index.html?path=${config.coverPath}`;
        yield page.goto(pageUrl, { waitUntil: 'load' });
        return new Promise((resolve) => {
            setTimeout(() => __awaiter(this, void 0, void 0, function* () {
                try {
                    // 关掉右侧控件
                    const closeBtn = yield page.waitForSelector("button[title='Hide addons [A]']");
                    closeBtn.click();
                    yield new Promise((resolve) => setTimeout(resolve, 1000)); // 等待界面重新渲染
                    const iframe = yield page.waitForSelector('#storybook-preview-iframe', {
                        timeout: 2 * 1000,
                    });
                    const iframeEle = yield iframe.contentFrame(); // 解析iframe
                    const dom = yield iframeEle.waitForSelector('#root', {
                        timeout: 2 * 1000,
                    });
                    // 截图
                    yield dom.screenshot({
                        path: path_1.default.resolve(doc_2.PROJECT_DOC_DIST_PATH, './ark-cover-shot.png'),
                    });
                    yield browser.close();
                    resolve(true);
                }
                catch (e) {
                    console.log(e);
                    yield browser.close();
                    console.log(chalk_1.default.yellow('截取封面超时'));
                    resolve(true);
                }
            }), 2000); // 等待页面重定向
        });
    });
}
function buildDoc() {
    return __awaiter(this, void 0, void 0, function* () {
        // 打包编译文档
        const spinner = (0, ora_1.default)('正在编译打包文档，时间较长，请耐心等待...').start();
        spinner.text = '';
        console.log('');
        // 合并storybook配置
        (0, doc_methods_1.mergeConfig)();
        // 执行打包命令
        const { status } = yield (0, npm_1.npm)(['run', 'build-storybook'], {
            cwd: doc_1.DOC_ROOT_PATH,
        });
        if (status !== 0) {
            spinner.fail(chalk_1.default.red('文档编译失败!编译前请先本地预览下文档，确保本地能够正常预览文档的情况下，再重新执行打包命令'));
            return;
        }
        // 清理之前的打包产物，然后重新把包复制到项目根路径
        if (fsEx.pathExistsSync(doc_2.PROJECT_DOC_DIST_PATH)) {
            rimraf_1.default.sync(doc_2.PROJECT_DOC_DIST_PATH);
        }
        fsEx.copySync(doc_2.STORYBOOK_DIST_PATH, doc_2.PROJECT_DOC_DIST_PATH);
        const configPath = path_1.default.join(doc_2.STORYBOOK_CONFIG_PATH, 'main.js');
        const config = require(configPath);
        if (config.coverPath) {
            // 进行页面截图
            if (yield screenshot()) {
                spinner.succeed(chalk_1.default.green('编译完成!'));
            }
            else {
                spinner.fail('编译失败');
            }
        }
        else {
            spinner.succeed(chalk_1.default.green('编译完成!'));
        }
    });
}
exports.default = buildDoc;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZG9jLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vY29tbWFuZHMvYnVpbGQvZG9jLnRzIl0sIm5hbWVzIj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