"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePackageJSON = exports.handleNPM = void 0;
const path_1 = __importDefault(require("path"));
const webpack_1 = require("../service/webpack");
const fs_extra_1 = __importDefault(require("fs-extra"));
const console_1 = require("../util/console");
const config_1 = require("../config");
const generateFiles_1 = require("../util/generateFiles");
const lodash_1 = require("lodash");
const util_1 = require("../util");
async function handleNPM({ mainAppData, subAppDataList = [], dependencies = [], appKey, appBuildDir, mode, devTool, ignoreInstall = false, }) {
    const allAppDataList = subAppDataList.concat(mainAppData);
    // 安装 NPM 依赖
    const deps = {};
    allAppDataList.map((app) => {
        Object.entries(app.npmDependencies).forEach(([name, version]) => {
            deps[name] = version;
        });
    });
    // 组件库单独安装并且通过 link 的方式 link 到项目目录
    // 合并组件库的公共npm
    // dependencies.map((compLib) => {
    //   // 素材库依赖
    //   if (!compLib.isComposite) {
    //     Object.assign(deps, compLib.dependencies || {});
    //   }
    //   // 组件库级别的依赖
    //   if (compLib.isComposite && compLib.compLibCommonResource) {
    //     Object.assign(deps, compLib.compLibCommonResource.npm);
    //   }
    //   // 单个组件的 lowcode 界别的依赖
    //   (compLib.components || []).forEach((comp) => {
    //     Object.assign(deps, comp.npmDependencies);
    //   });
    // });
    const isChange = await generatePackageJSON(deps, appBuildDir, appKey, mode, devTool);
    if (!isChange) {
        (0, console_1.notice)('package.json dependencies 已经安装，如出现未安装成功或找不到依赖，请重启wa watch');
    }
    await (0, webpack_1.installDependencies)(appBuildDir, {
        isDependence: false,
        ignoreInstall,
    });
}
exports.handleNPM = handleNPM;
async function generatePackageJSON(dependencies = {}, appBuildDir, appKey, mode, devTool) {
    const templatePath = path_1.default.join(config_1.appTemplateDir, 'package.json');
    const dstFilePath = path_1.default.join(appBuildDir, 'package.json');
    const dstTempPath = path_1.default.join(appBuildDir, '.temp', 'package.json');
    const packageTpl = await fs_extra_1.default.readFile(templatePath, { encoding: 'utf8' });
    await fs_extra_1.default.ensureFile(dstTempPath);
    await fs_extra_1.default.writeFile(dstTempPath, (0, lodash_1.template)(packageTpl)({
        canUseVite: (0, util_1.canUseVite)(mode, devTool),
        appKey,
        appId: appKey,
    }));
    const packageInfo = await fs_extra_1.default.readJSON(dstTempPath);
    // weapps-sdk 要与 builder template 版本一致
    // govcloud 包废弃
    // dependencies['@govcloud/weapps-sdk'] =
    //   packageInfo.dependencies['@govcloud/weapps-sdk'];
    Object.keys(dependencies).forEach((key) => {
        const version = dependencies[key];
        if (packageInfo.dependencies[key] !== version) {
            packageInfo.dependencies[key] = version;
        }
    });
    await fs_extra_1.default.remove(dstTempPath);
    // 改用 TPL 去写文件，传入参数
    const isChange = await (0, generateFiles_1.writeFile)(dstFilePath, JSON.stringify(packageInfo, null, 2));
    return isChange;
}
exports.generatePackageJSON = generatePackageJSON;
