"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadAssets = exports.generateWebpackWebDevServerFile = exports.getMaterialNodeModulesPathList = exports.installDependencies = exports.downloadDependencies = exports.downloadAndInstallDependencies = exports.getAllPageMpEntryPath = exports.getWebpackMpBuildParams = exports.getWebpackWebBuildParams = exports.getMpAllRouterConfig = exports.getPageName = exports.getHomePageInstance = exports.getMainAppDataByList = exports.generateMpJsonConfigFile = exports.generateKboneAppConfig = exports.generateKbonePageConfig = exports.downloadAndWriteTabBarIcon = exports.generateKboneTabBarConfig = exports.generateAppConfig = exports.generateWebpackWebBuildParamsFile = exports.fixAppJson = exports.OUTPUT_DIR = exports.PERSISTENT_DEPENDIENCES_MAP = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const lodash_1 = require("lodash");
const util_1 = require("../util");
const axios_1 = __importDefault(require("axios"));
const compressing_1 = __importDefault(require("compressing"));
const cross_spawn_1 = __importDefault(require("cross-spawn"));
const weapps_core_1 = require("@cloudbase/lowcode-generator/lib/weapps-core");
const config_1 = require("../config");
const common_1 = require("../types/common");
const common_2 = require("../util/common");
const types_1 = require("../../types");
const generateFiles_1 = require("../util/generateFiles");
exports.PERSISTENT_DEPENDIENCES_MAP = {};
exports.OUTPUT_DIR = 'preview';
async function fixAppJson(appBuildDir) {
    const appJsonPath = path_1.default.resolve(appBuildDir, 'dist/mp/app.json');
    if (!fs_extra_1.default.existsSync(appJsonPath)) {
        return;
    }
    const appJson = await fs_extra_1.default.readJSON(appJsonPath);
    appJson.subpackages = appJson.subpackages.map((config) => {
        config.pages = config.pages.map((pagePath) => {
            const pagePathArr = pagePath.split('/');
            if (pagePathArr.length === 4 && pagePathArr[1] === config.root) {
                pagePathArr.splice(1, 1);
                return pagePathArr.join('/');
            }
            return pagePath;
        });
        return config;
    });
    await fs_extra_1.default.writeFile(appJsonPath, JSON.stringify(appJson, null, 2));
}
exports.fixAppJson = fixAppJson;
async function generateWebpackWebBuildParamsFile({ appId, allAppDataList, appBuildDir, publicPath, mode, buildTypeList, assets = [], }) {
    let mainAppData = getMainAppDataByList(allAppDataList);
    let extraDefine = {
        'process.env.historyType': `"${mainAppData.historyType || types_1.HISTORY_TYPE.BROWSER}"`,
    };
    const params = getWebpackWebBuildParams(appId, appBuildDir, publicPath, mode, buildTypeList, extraDefine, assets);
    const webpackConfigPath = path_1.default.resolve(appBuildDir, './webpack/webpack.web.prod.js');
    const paramsString = JSON.stringify(params, null, 2);
    const webpackConfigContent = `const params = ${paramsString};\nmodule.exports = require('./web.prod.js')(params);`;
    await (0, generateFiles_1.writeFile)(webpackConfigPath, webpackConfigContent);
    return webpackConfigPath;
}
exports.generateWebpackWebBuildParamsFile = generateWebpackWebBuildParamsFile;
async function generateAppConfig(mainAppData, subAppDataList, appBuildDir) {
    let originMpConfig = {};
    // 如果有 mp_config 则读取内容合并。
    const configMod = mainAppData.codeModules.find((p) => p.name === config_1.MP_CONFIG_MODULE_NAME);
    if (configMod) {
        const code = configMod.code.replace(/export\s+default/, '');
        try {
            originMpConfig = eval(`(${code})`);
        }
        catch (e) {
            console.error('Kbone config file error', e);
        }
    }
    // app 配置
    generateKboneAppConfig(originMpConfig, mainAppData);
    // 页面配置
    originMpConfig = generateKbonePageConfig(originMpConfig, mainAppData, subAppDataList);
    // tabbar 配置
    await generateKboneTabBarConfig(originMpConfig, appBuildDir);
    return originMpConfig;
}
exports.generateAppConfig = generateAppConfig;
async function generateKboneTabBarConfig(mpConfig, appBuildDir) {
    mpConfig.appExtraConfig = mpConfig.appExtraConfig || {};
    if (mpConfig.appExtraConfig.tabBar) {
        if (mpConfig.appExtraConfig.tabBar.list) {
            await Promise.all(mpConfig.appExtraConfig.tabBar.list.map(async (item) => {
                if (item.iconPath) {
                    item.iconPath = await downloadAndWriteTabBarIcon(item.iconPath, item.pagePath, appBuildDir, 'iconPath');
                }
                if (item.selectedIconPath) {
                    item.selectedIconPath = await downloadAndWriteTabBarIcon(item.selectedIconPath, item.pagePath, appBuildDir, 'selectedIconPath');
                }
            }));
        }
    }
}
exports.generateKboneTabBarConfig = generateKboneTabBarConfig;
async function downloadAndWriteTabBarIcon(iconPath, pagePath, appBuildDir, fileName) {
    console.log('开始下载文件', iconPath);
    const extname = path_1.default.extname(iconPath);
    const relativePath = `images_tabBar/${pagePath}/${fileName}${extname}`;
    const fileFullPath = path_1.default.resolve(appBuildDir, `dist/mp/${relativePath}`);
    await fs_extra_1.default.ensureFile(fileFullPath);
    const writer = fs_extra_1.default.createWriteStream(fileFullPath);
    const response = await (0, axios_1.default)({
        method: 'get',
        url: iconPath,
        responseType: 'stream',
    });
    response.data.pipe(writer);
    return new Promise((resolve, reject) => {
        writer.on('finish', () => {
            console.log('下载成功', iconPath);
            // copy 一份到web
            // fs.copy(
            //   fileFullPath,
            //   path.resolve(appBuildDir, `preview/${relativePath}`)
            // )
            resolve(relativePath);
        });
        writer.on('error', () => {
            console.error('下载失败', iconPath);
            reject();
        });
    });
}
exports.downloadAndWriteTabBarIcon = downloadAndWriteTabBarIcon;
// 将 weapps 配置的 page-data 转换到 mp_config
function generateKbonePageConfig(mpConfig, mainAppData, subAppDataList = []) {
    mpConfig.pages = mpConfig.pages || {};
    const allAppDataList = subAppDataList.concat(mainAppData);
    allAppDataList.map((appData) => {
        appData.pageInstanceList.forEach((item) => {
            const pageId = [appData.rootPath, item.id].filter((i) => i).join('_');
            if (!mpConfig.pages[pageId]) {
                mpConfig.pages[pageId] = {};
            }
            const navigationBarTitleText = item.data.navigationBarTitleText || item.data.title;
            // 去除部分 mp 不认的属性
            delete item.data.title;
            delete item.data.params;
            delete item.data.scene;
            // page 配置
            config_1.KBONE_PAGE_KEYS.forEach((key) => {
                if (item.data[key]) {
                    mpConfig.pages[pageId][key] = item.data[key];
                }
            });
            // extra 配置
            mpConfig.pages[pageId].extra = {
                ...item.data,
                navigationBarTitleText,
            };
        });
    });
    return mpConfig;
}
exports.generateKbonePageConfig = generateKbonePageConfig;
function generateKboneAppConfig(mpConfig, mainAppData) {
    if (mainAppData === null || mainAppData === void 0 ? void 0 : mainAppData.appConfig) {
        if (mainAppData.appConfig.window) {
            mpConfig.app = mpConfig.app || {};
            mpConfig.app = {
                ...mainAppData.appConfig.window,
                ...mpConfig.app,
            };
        }
    }
}
exports.generateKboneAppConfig = generateKboneAppConfig;
// 生成 kbone mp 配置文件
async function generateMpJsonConfigFile(allAppDataList, userConfig, appBuildDir, options) {
    const mainAppData = getMainAppDataByList(allAppDataList);
    const subAppDataList = allAppDataList.filter((i) => i.rootPath);
    const homeId = getHomePageInstance(mainAppData.pageInstanceList).id;
    userConfig = userConfig || {};
    const kbConfig = {
        origin: 'https://weapps.tencent.com',
        entry: `/${homeId}`,
        redirect: {
            notFound: `${homeId}`,
            accessDenied: `${homeId}`,
        },
        generate: {
            autoBuildNpm: true,
            appEntry: 'miniprogram-app',
            appWxss: 'display',
            subpackages: {},
            globalVars: [['__injectProxy', '(window.Proxy = Proxy)']],
        },
        app: {
            navigationBarTitleText: `WeApps-${options.appKey}`,
        },
        projectConfig: {
            appid: 'touristappid',
            projectname: `WeApps-${options.appKey}`,
        },
        packageConfig: {
            author: 'weapps',
        },
        ...userConfig,
        global: {
            ...userConfig.global,
            rem: true, // rem 必须为 true
        },
        router: getMpAllRouterConfig(allAppDataList),
        __homePath__: getMpAllRouterConfig(allAppDataList, true),
    };
    if (subAppDataList === null || subAppDataList === void 0 ? void 0 : subAppDataList.length) {
        const subpackages = {};
        subAppDataList.map((appData) => {
            const { rootPath } = appData;
            subpackages[rootPath] = Object.keys(getMpAllRouterConfig([appData]));
        });
        kbConfig.generate = {
            ...kbConfig.generate,
            subpackages,
        };
    }
    // kbone 生成子包模式
    if (options.generateMpType === 'subpackage') {
        kbConfig.generate = {
            ...kbConfig.generate,
            app: 'noemit',
        };
    }
    const templateStr = JSON.stringify(kbConfig, null, 2);
    await fs_extra_1.default.writeFile(path_1.default.resolve(appBuildDir, 'webpack/miniprogram.config.js'), `export default = ${templateStr}`);
}
exports.generateMpJsonConfigFile = generateMpJsonConfigFile;
function getMainAppDataByList(allAppDataList) {
    return allAppDataList.find((item) => !item.rootPath);
}
exports.getMainAppDataByList = getMainAppDataByList;
// 获取设置的 home 页面
function getHomePageInstance(pageInstanceList) {
    let target = pageInstanceList[0];
    (0, weapps_core_1.loopDealWithFn)(pageInstanceList, (pageInstance) => {
        if (pageInstance.isHome) {
            target = pageInstance;
            return pageInstance;
        }
    });
    return target;
}
exports.getHomePageInstance = getHomePageInstance;
// 获取页面名字
function getPageName(name) {
    return `${name}`;
}
exports.getPageName = getPageName;
function getMpAllRouterConfig(allAppDataList, getHome = false) {
    const router = {};
    let homePath = '';
    allAppDataList.map((appData) => {
        const { pageInstanceList, rootPath = '' } = appData;
        (0, weapps_core_1.loopDealWithFn)(pageInstanceList, (pageInstance) => {
            const name = [rootPath, pageInstance.id].filter((i) => i).join('_');
            const path = `/${name}`;
            if (!homePath) {
                homePath = name;
            }
            if (pageInstance.isHome && !rootPath) {
                homePath = name;
            }
            router[name] = [path];
        });
    });
    if (getHome) {
        return homePath;
    }
    return router;
}
exports.getMpAllRouterConfig = getMpAllRouterConfig;
function getWebpackWebBuildParams(appId, appBuildDir, publicPath = '/', mode = common_1.WebpackModeType.NONE, buildTypeList = [common_1.BuildType.WEB], extraDefine = {}, assets = []) {
    return {
        context: appBuildDir,
        mode: mode !== common_1.WebpackModeType.PRODUCTION ? 'development' : mode,
        watch: false,
        entry: path_1.default.resolve(appBuildDir, 'src/index.jsx'),
        output: {
            path: path_1.default.resolve(appBuildDir, exports.OUTPUT_DIR),
            filename: '[name].[contenthash].bundle.js',
            chunkFilename: '[name].[contenthash].chunk.js',
            publicPath: buildTypeList.includes(common_1.BuildType.APP) || buildTypeList.includes(common_1.BuildType.ADMIN_PORTAL) ? '' : publicPath,
            pathinfo: false,
            // clean: mode === WebpackModeType.PRODUCTION,
        },
        htmlTemplatePath: path_1.default.resolve(appBuildDir, './index.html'),
        externals: {
            react: 'window.React',
            'react-dom': 'window.ReactDOM',
            mobx: 'window.mobx',
            '@cloudbase/js-sdk': 'window.cloudbase',
            '@cloudbase/weda-cloud-sdk/dist/h5': 'window.CloudSDK',
            '@cloudbase/weda-cloud-sdk': 'window.CloudSDK',
            '@zxing/library': 'window.ZXing',
            '@cloudbase/lowcode-render': 'window["weda-render"]',
        },
        resolveModules: [path_1.default.resolve(appBuildDir), 'node_modules'],
        definePlugin: {
            'process.env.buildType': `"${buildTypeList[0]}"`,
            ...extraDefine,
        },
        devtool: ['app-nvzcvt10', 'app-msa2ihs9'].includes(appId)
            ? 'source-map'
            : mode !== common_1.WebpackModeType.PRODUCTION
                ? 'eval'
                : false,
    };
}
exports.getWebpackWebBuildParams = getWebpackWebBuildParams;
function getWebpackMpBuildParams(appBuildDir, materialsDir, dependencies = [], nodeModulesPath, allAppDataList, mode, watch, options) {
    return {
        context: appBuildDir,
        mode,
        watch,
        entry: getAllPageMpEntryPath(allAppDataList, appBuildDir),
        outputPath: path_1.default.resolve(appBuildDir, 'dist/mp/common'),
        resolveModules: [
            appBuildDir,
            path_1.default.resolve(appBuildDir, 'node_modules'),
            path_1.default.resolve(appBuildDir, 'src'),
            ...getMaterialNodeModulesPathList(dependencies, materialsDir, appBuildDir),
            'node_modules',
        ],
        watchOptions: {
            ignored: /node_modules/,
            aggregateTimeout: 600,
            poll: 1000,
        },
        cache: {
            type: 'memory',
        },
    };
}
exports.getWebpackMpBuildParams = getWebpackMpBuildParams;
function getAllPageMpEntryPath(allAppDataList, appBuildDir) {
    const entry = {};
    // 优先填首页
    const mainAppData = getMainAppDataByList(allAppDataList);
    const homePageInstance = getHomePageInstance(mainAppData.pageInstanceList);
    entry[homePageInstance.id] = path_1.default.resolve(appBuildDir, `src/pages/${getPageName(homePageInstance.id)}/main.mp.jsx`);
    allAppDataList.map((app) => {
        const { pageInstanceList, rootPath } = app;
        const packagePathStr = rootPath ? `packages/${rootPath}` : '';
        (0, weapps_core_1.loopDealWithFn)(pageInstanceList, (pageInstance) => {
            const pageName = rootPath ? `${rootPath}_${pageInstance.id}` : pageInstance.id;
            entry[pageName] = path_1.default.resolve(appBuildDir, path_1.default.join('src', packagePathStr, `pages/${getPageName(pageInstance.id)}/main.mp.jsx`));
        });
    });
    return entry;
}
exports.getAllPageMpEntryPath = getAllPageMpEntryPath;
async function downloadAndInstallDependencies(dependencies = [], materialsDir, installOptions = {}) {
    const localPkg = (0, util_1.getCurrentPackageJson)();
    await Promise.all(dependencies.map(async (item) => {
        const { name, version, srcZipUrl } = item;
        const materialNameVersion = `${name}@${version}`;
        const targetDir = path_1.default.join(materialsDir, materialNameVersion);
        // 当前本地目录是素材库的时候，直接用本地的
        if (localPkg && localPkg.name === name && localPkg.version === version) {
            console.log('当前本地目录是素材库的时候，无需安装', materialNameVersion);
            return;
        }
        // 如果需要下载素材
        if (!(await fs_extra_1.default.pathExists(targetDir))) {
            const matched = srcZipUrl.match(/\/(cg-.*?)\//);
            const { appBuildDir } = (0, util_1.getCompileDirs)('app');
            if ((matched === null || matched === void 0 ? void 0 : matched[1]) && fs_extra_1.default.existsSync(path_1.default.resolve(appBuildDir, `.component_cache/web/${matched[1]}@${version}`))) {
                console.log(`link ${name}@${version} ...`);
                await fs_extra_1.default.symlink(path_1.default.resolve(appBuildDir, `.component_cache/web/${matched[1]}@${version}`), targetDir, 'dir');
                // await fs.copy(
                //   path.resolve(
                //     appBuildDir,
                //     `.component_cache/web/${matched[1]}@${version}`
                //   ),
                //   targetDir,
                //   {
                //     filter: function (src, dest) {
                //       const path = src.split('/');
                //       return !junk.is(path[path.length - 1]);
                //     },
                //   }
                // );
            }
            else {
                console.log(`下载${name}@${version} ...`);
                await downloadDependencies(targetDir, srcZipUrl);
            }
        }
        console.log(`处理${name}@${version}依赖 ...`);
        await installDependencies(targetDir, {
            ...installOptions,
            isDependence: true,
            ignoreInstall: (0, common_2.isOfficialComponentLib)(name, version) || (installOptions === null || installOptions === void 0 ? void 0 : installOptions.ignoreInstall),
            dependenceMeta: {
                name,
                version,
                downloadUrl: srcZipUrl,
            },
        });
        // 同步依赖
        // const packageJson = await fs.readJson(
        //   path.join(targetDir, 'package.json')
        // );
        // item.dependencies = packageJson.dependencies || {};
    }));
}
exports.downloadAndInstallDependencies = downloadAndInstallDependencies;
async function downloadDependencies(targetDir, srcZipUrl) {
    const isExist = fs_extra_1.default.existsSync(path_1.default.join(targetDir, 'package.json'));
    if (isExist || !srcZipUrl) {
        return;
    }
    try {
        let response = await (0, axios_1.default)({
            url: srcZipUrl,
            responseType: 'stream',
            // proxy: false
        });
        await fs_extra_1.default.ensureDir(targetDir);
        // let download = spawn(
        //   'curl',
        //   ['-fsSL', srcZipUrl, '-o', `${targetDir}.zip`],
        //   {
        //     stdio: 'inherit',
        //   }
        // );
        // await promisifyProcess(download);
        const tag = `uncompress ${srcZipUrl}`;
        console.time(tag);
        await compressing_1.default.zip.uncompress(response.data, targetDir);
        // let unzip = spawn(
        //   'unzip',
        //   ['-q', '-o', `${targetDir}.zip`, '-d', targetDir],
        //   {
        //     stdio: 'inherit',
        //   }
        // );
        // await promisifyProcess(unzip);
        // await fs.remove(`${targetDir}.zip`);
        console.timeEnd(tag);
    }
    catch (e) {
        console.error(`Fail to download weapps material package ${srcZipUrl}`, e);
        throw e;
    }
}
exports.downloadDependencies = downloadDependencies;
// TODO use yarn if installed
async function installDependencies(targetDir, options = {}) {
    var _a;
    let yarnExists = false;
    try {
        let { stdout } = cross_spawn_1.default.sync('yarn', ['-v']);
        let str = stdout.toString();
        if (Number((_a = str.split('.')) === null || _a === void 0 ? void 0 : _a[0]) <= 1) {
            yarnExists = true;
        }
    }
    catch (e) { }
    // 如果generatedFileContents[path.resolve(targetDir, 'package.json')]不存在，说明是不存在缓存文件，isPkgModify必须是true，才不会忽略install
    const isPkgModify = !generateFiles_1.generatedFileContents[path_1.default.resolve(targetDir, 'package.json')]
        ? true
        : generateFiles_1.generatedFileContents[path_1.default.resolve(targetDir, 'package.json')].modify;
    if ((options === null || options === void 0 ? void 0 : options.ignoreInstall) && fs_extra_1.default.existsSync(path_1.default.join(targetDir, 'node_modules'))) {
        console.log(`ignore install dependencies in ${targetDir}`);
        return;
    }
    // const isExist = fs.existsSync(path.join(targetDir, 'package-lock.json'))
    // 是否安装最新的
    let packageName = options.packageName || '';
    if (packageName && options.latest) {
        packageName = `${packageName}@latest`;
    }
    console.log(`Installing dependencies in ${targetDir}`);
    const operationTag = `----------------  ${yarnExists ? 'yarn' : 'npm'} Install dependencies`;
    console.time(operationTag);
    const registry = `--registry=${config_1.npmRegistry}`;
    const npmOptions = ['--prefer-offline', '--no-audit', '--progress=false', registry];
    if (options === null || options === void 0 ? void 0 : options.dependenceMeta) {
        npmOptions.push('--production');
        const { name, version } = options.dependenceMeta;
        if (!fs_extra_1.default.existsSync(path_1.default.join(targetDir, 'node_modules'))) {
            exports.PERSISTENT_DEPENDIENCES_MAP[`${name}@${version}`] = {
                ...options === null || options === void 0 ? void 0 : options.dependenceMeta,
                source: targetDir,
            };
        }
    }
    // 云端构建, 选用 npm
    const installProcessOptions = {
        cwd: targetDir,
        // NODE_ENV="production"时，devDependencies里的包不会下载，兼容h5在线编译
        env: { ...process.env, NODE_ENV: '' },
        stdio: 'inherit',
    };
    let installprocess;
    // TODO: install 失败 应该终止进程
    if (yarnExists && (options === null || options === void 0 ? void 0 : options.runtime) !== types_1.RUNTIME.CI) {
        const addPackage = packageName ? ['add', packageName] : [];
        installprocess = cross_spawn_1.default.sync('yarn', [...addPackage, registry], installProcessOptions);
    }
    else {
        installprocess = cross_spawn_1.default.sync('npm', ['install', packageName, ...npmOptions], installProcessOptions);
    }
    // TODO: 确认 process.status 语义来判断是否标识进程正常退出
    if (installprocess.status) {
        throw new Error(`安装依赖失败 ${installprocess.error || ''}`);
    }
    fixNodeModules(targetDir);
    try {
        await fs_extra_1.default.remove(path_1.default.join(path_1.default.join(targetDir, 'package-lock.json')));
        await fs_extra_1.default.remove(path_1.default.join(path_1.default.join(targetDir, 'yarn.lock')));
    }
    catch (e) { }
    console.timeEnd(operationTag);
}
exports.installDependencies = installDependencies;
function fixNodeModules(projDir, options = { mobx: false, '@cloudbase/js-sdk': false }) {
    const { mobx = false, ['@cloudbase/js-sdk']: jsSDK = false } = options;
    if (mobx) {
        // # fix mobx/package.json: "main": "lib/index.js" -> "main": "lib/index.js"
        const mobxPackageFile = path_1.default.join(projDir, 'node_modules', 'mobx/package.json');
        if (fs_extra_1.default.existsSync(mobxPackageFile)) {
            const mobxPackageFileContent = fs_extra_1.default.readJSONSync(mobxPackageFile);
            if (fs_extra_1.default.existsSync(path_1.default.join(projDir, 'node_modules', 'mobx/lib/mobx.min.js'))) {
                mobxPackageFileContent.main = 'lib/mobx.min.js';
                fs_extra_1.default.writeJSONSync(mobxPackageFile, mobxPackageFileContent);
            }
        }
    }
    if (jsSDK) {
        const scriptPath = path_1.default.resolve(projDir, 'node_modules', '@cloudbase/weda-cloud-sdk/scripts/fix-wx-none-private');
        if (fs_extra_1.default.existsSync(scriptPath)) {
            const fix = require(scriptPath);
            fix();
        }
    }
}
function getMaterialNodeModulesPathList(dependencies = [], materialsDir, base) {
    const localPkg = (0, util_1.getCurrentPackageJson)();
    return dependencies.map(({ name, version }) => {
        const nameVersion = `${name}@${version}`;
        let target = '';
        if (localPkg && localPkg.name === name && localPkg.version === version) {
            console.log('当前本地目录是素材库的时候，直接使用当前目录的 node_modules', nameVersion);
            target = path_1.default.join(process.cwd(), 'node_modules');
        }
        else {
            target = path_1.default.join(materialsDir, nameVersion, 'node_modules');
        }
        return base ? path_1.default.relative(base, target) : target;
    });
}
exports.getMaterialNodeModulesPathList = getMaterialNodeModulesPathList;
// 生成devServer 核心依赖
async function generateWebpackWebDevServerFile({ appBuildDir, buildTypeList }) {
    const dest = path_1.default.resolve(appBuildDir, `./webpack/devServer.js`);
    const template = await fs_extra_1.default.readFile(path_1.default.resolve(config_1.appTemplateDir, './webpack/devServer.js'), {
        encoding: 'utf8',
    });
    const jsContent = (0, lodash_1.template)(template, {
        interpolate: /<%=([\s\S]+?)%>/g,
    })({
        isApp: buildTypeList.includes(common_1.BuildType.APP),
        isAdminPortal: (0, common_1.buildAsAdminPortalByBuildType)(buildTypeList),
    });
    await fs_extra_1.default.ensureFile(dest);
    await fs_extra_1.default.writeFile(dest, jsContent);
}
exports.generateWebpackWebDevServerFile = generateWebpackWebDevServerFile;
// 下载js 文件
async function downloadAssets(targetDir, assetUrl) {
    const isExist = fs_extra_1.default.existsSync(path_1.default.join(targetDir, 'package.json'));
    if (isExist || !assetUrl) {
        return;
    }
    const [err, response] = await (0, util_1.promiseWrapper)((0, axios_1.default)({
        url: assetUrl,
        responseType: 'stream',
        // proxy: false
    }));
    if (err) {
        console.error(`Fail to download weapps material package ${assetUrl}`, err);
        throw err;
    }
    await fs_extra_1.default.ensureDir(targetDir);
    const filename = (0, common_2.getFileNameByUrl)(assetUrl);
    const targetPath = path_1.default.resolve(targetDir, `./${filename}`);
    const writer = fs_extra_1.default.createWriteStream(targetPath);
    response.data.pipe(writer);
    return new Promise((resolve, reject) => {
        writer.on('finish', () => {
            console.log('下载成功', assetUrl);
            fs_extra_1.default.copy(targetDir, path_1.default.resolve(targetDir, '../preview'));
            resolve(targetPath);
        });
        writer.on('error', () => {
            console.error('下载失败', assetUrl);
            reject();
        });
    });
}
exports.downloadAssets = downloadAssets;
