"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergePages = exports.mergePackageDependiences = exports.mergePackageJson = exports.mergeSubPackages = void 0;
/**
 * Tencent is pleased to support the open source community by making CloudBaseFramework - 云原生一体化部署工具 available.
 *
 * Copyright (C) 2020 THL A29 Limited, a Tencent company.  All rights reserved.
 *
 * Please refer to license text included with this package for license details.
 */
const fs = __importStar(require("fs-extra"));
const lodash_1 = require("lodash");
const mp_config_1 = require("../mp/mp_config");
// 将后面的子包配置合并到前面
// 暂时不考虑 root 会重名的情况
async function mergeSubPackages(baseAppJsonPath, mergeAppJsonPath) {
    const baseJson = fs.readJSONSync(baseAppJsonPath);
    const mergeJson = fs.readJSONSync(mergeAppJsonPath);
    if (!getSubPackages(mergeJson))
        return;
    const newJson = { ...baseJson };
    newJson.subpackages = (0, mp_config_1.mergeSubPackages)(baseJson.subpackages, mergeJson.subpackages);
    fs.writeJSONSync(baseAppJsonPath, newJson, { spaces: 2 });
    // 处理兼容
    function getSubPackages(json) {
        return json.subpackages || json.subPackages;
    }
}
exports.mergeSubPackages = mergeSubPackages;
async function mergePackageJson(basePkgJsonPath, mergePkgJsonPath) {
    // 可能不存在 package.json 文件，需要初始化一个
    if (!(await fs.pathExists(basePkgJsonPath))) {
        await fs.copy(mergePkgJsonPath, basePkgJsonPath);
    }
    else {
        let baseJson = fs.readJSONSync(basePkgJsonPath);
        const mergeJson = fs.readJSONSync(mergePkgJsonPath);
        baseJson = {
            ...baseJson,
            ...mergePackageDependiences(baseJson, mergeJson),
        };
        await fs.writeJSON(basePkgJsonPath, baseJson, { spaces: 2 });
    }
}
exports.mergePackageJson = mergePackageJson;
function mergePackageDependiences(baseJson, mergeJson) {
    baseJson.dependencies = {
        ...(baseJson.dependencies || {}),
        ...(mergeJson.dependencies || {}),
    };
    baseJson.devDependencies = {
        ...(baseJson.devDependencies || {}),
        ...(mergeJson.devDependencies || {}),
    };
    return baseJson;
}
exports.mergePackageDependiences = mergePackageDependiences;
async function mergePages(baseAppJsonPath, mergeAppJsonPath) {
    const baseJson = fs.readJSONSync(baseAppJsonPath);
    const mergeJson = fs.readJSONSync(mergeAppJsonPath);
    baseJson.pages = (0, lodash_1.uniq)([...(baseJson.pages || []), ...(mergeJson.pages || [])]);
    await fs.writeJSON(baseAppJsonPath, baseJson, { spaces: 2 });
}
exports.mergePages = mergePages;
