"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeConfig = exports.generateWebpack = exports.overwriteConfig = exports.mergeMainJs = exports.getDocConfig = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const serialize_javascript_1 = __importDefault(require("serialize-javascript"));
const chalk_1 = __importDefault(require("chalk"));
const beautify_file_1 = require("./beautify-file");
const other_1 = require("./other");
const file_1 = require("./file");
const doc_1 = require("../constant/doc");
const fsEx = require('fs-extra');
// 获取storybook配置
/**
 *
 * @param {String} fileName 文件名称，含后缀
 * @param {String} readWay 读取方式。require:require用读取,read:通过read方式读取
 * @returns
 */
function getDocConfig(fileName, readWay) {
    let projectConfig;
    let defaultConfig;
    // 获取项目storybook配置
    const projectConfigPath = path_1.default.join(doc_1.PROJECT_DOC_CONFIG_PATH, fileName);
    // 获取默认的storybook配置
    const defaultConfigPath = path_1.default.join(doc_1.DEFAULT_STORYBOOK_CONFIG_PATH, fileName);
    if (process.env.debug) {
        console.log(chalk_1.default.yellow(`debug:项目路径${projectConfigPath}是否存在: ${fs_1.default.existsSync(projectConfigPath)}`));
        console.log(chalk_1.default.yellow(`debug:默认路径${defaultConfigPath}是否存在: ${fs_1.default.existsSync(defaultConfigPath)}`));
    }
    // 没有文件则自动创建
    fsEx.ensureFileSync(defaultConfigPath);
    if (readWay === 'require') {
        projectConfig = require(projectConfigPath);
        defaultConfig = require(defaultConfigPath);
    }
    else {
        projectConfig = fs_1.default.readFileSync(projectConfigPath).toString();
        defaultConfig = fs_1.default.readFileSync(defaultConfigPath).toString();
    }
    if (projectConfig.stories) {
        const stories = projectConfig.stories.reduce((arr, item) => {
            arr.push((0, other_1.formatPath)(item));
            return arr;
        }, []);
        projectConfig.stories = stories;
    }
    return {
        defaultConfig,
        projectConfig,
    };
}
exports.getDocConfig = getDocConfig;
// 合并main.js配置
function mergeMainJs(fileName) {
    const config = getDocConfig(fileName, 'require');
    const defaultConfig = config.defaultConfig;
    const projectConfig = config.projectConfig;
    const newConfig = Object.assign(defaultConfig, projectConfig);
    // 调试
    process.env.debug &&
        console.log(chalk_1.default.yellow(`debug:main.js配置 ${JSON.stringify(newConfig, null, 2)})}`));
    const targetConfigPath = path_1.default.join(doc_1.STORYBOOK_CONFIG_PATH, fileName);
    const data = (0, beautify_file_1.beautifyJs)(`module.exports = ${(0, serialize_javascript_1.default)(newConfig, {
        unsafe: true,
    })}`);
    fsEx.outputFileSync(targetConfigPath, data);
}
exports.mergeMainJs = mergeMainJs;
// 重写storybook配置文件
function overwriteConfig(fileName) {
    const { projectConfig } = getDocConfig(fileName, 'read') || {};
    // 调试
    process.env.debug &&
        console.log(chalk_1.default.yellow(`debug:${fileName}配置`));
    const targetConfigPath = path_1.default.join(doc_1.STORYBOOK_CONFIG_PATH, fileName);
    if (projectConfig) {
        fsEx.outputFileSync(targetConfigPath, projectConfig);
    }
}
exports.overwriteConfig = overwriteConfig;
// 动态生成webpack配置
function generateWebpack() {
    const projectWebpackPath = (0, other_1.formatPath)(path_1.default.join(doc_1.PROJECT_DOC_CONFIG_PATH, 'webpack.config.js'));
    // 读取项目组的webpack
    const projectWebpack = (0, beautify_file_1.beautifyJs)(`const webpack = require('${projectWebpackPath}')
    await webpack({config, mode})`);
    (0, file_1.ejsWriteFile)(doc_1.DEFAULT_STORYBOOK_WEBPACK_PATH, doc_1.STORYBOOK_WEBPACK_PATH, {
        ROOT_PATH: (0, other_1.formatPath)(process.cwd()),
        WEBPACK: projectWebpack,
    });
}
exports.generateWebpack = generateWebpack;
// 合并storybook配置
function mergeConfig() {
    if (!fsEx.pathExistsSync(doc_1.DOC_ROOT_PATH)) {
        console.log(chalk_1.default.red('脚手架内置文档文件不存在，请重新执行ark generate --doc进行安装'));
        return;
    }
    // 合并main.js配置
    mergeMainJs('main.js');
    // 根据项目路径生成webpack配置
    generateWebpack();
    // 复制preview.js到脚手架
    overwriteConfig('preview.js');
    // 复制preview-head.html
    overwriteConfig('preview-head.html');
    // 复制preview-body.html
    overwriteConfig('preview-body.html');
    // 复制middleware.js
    // overwriteConfig('middleware.js') // 暂时去掉中间件代理功能。因为没人用
}
exports.mergeConfig = mergeConfig;
exports.default = {
    mergeConfig,
};
//# sourceMappingURL=data:application/json;base64, 