import * as fs from "fs";
import * as path from "path";
import archiver from "archiver";
import { createDist } from "./common/utils.js";
import { processHtmlFiles } from "./scriptTo.js";
import do_task from "./single-html/build.js";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function processChannels(options) {
const zipChannel = options.zipChannel || [
"facebook",
"google",
"tiktok",
"vungle",
"liftoff",
];
const outputPrefix = options.outputPrefix || "";
const zipToSingleDir = options.zipToSingleDir || false;
const jsPrefix = options.jsPrefix || "script";
for (const channelName of zipChannel) {
// 遍历 networks 目录下的所有js文件,获取 文件名 然后删除 web-mobile 目录下的同名文件
const files = fs.readdirSync(path.join("networks"));
for (const file of files) {
if (file.endsWith(".js")) {
const destinationFile = path.join("web-mobile", file);
if (fs.existsSync(destinationFile)) {
fs.unlinkSync(destinationFile);
console.log(`已删除: ${destinationFile}`);
}
}
}
// tiktok 处理
const tiktokConfig = path.join("web-mobile", `config.json`);
if (fs.existsSync(tiktokConfig)) {
fs.unlinkSync(tiktokConfig);
}
const tiktokJs = path.join("web-mobile", `tiktok.js`);
if (fs.existsSync(tiktokJs)) {
fs.unlinkSync(tiktokJs);
}
// 从 networks 找到复制到 web-mobile 中
const sourceFile = path.join("networks", `${channelName}.js`);
const destinationFile = path.join("web-mobile", `${channelName}.js`);
if (channelName === "tiktok") {
fs.copyFileSync(
path.join("tiktok/config.json"),
path.join("web-mobile", `config.json`)
);
fs.copyFileSync(
path.join("tiktok/tiktok.js"),
path.join("web-mobile", `tiktok.js`)
);
} else if (channelName === "vungle" || channelName === "liftoff") {
// 对于 vungle 和 liftoff,使用 mraid_support.js
const mraidSupportFile = path.join("networks", "mraid_support.js");
const mraidDestinationFile = path.join("web-mobile", "mraid_support.js");
if (fs.existsSync(mraidSupportFile)) {
fs.copyFileSync(mraidSupportFile, mraidDestinationFile);
console.log(`已复制: ${mraidSupportFile} 到 ${mraidDestinationFile}`);
}
} else {
// if (fs.existsSync(sourceFile)) {
// fs.copyFileSync(sourceFile, destinationFile);
// console.log(`已复制: ${sourceFile} 到 ${destinationFile}`);
// }
}
const htmlFilePath = path.join("web-mobile", "index.html");
let htmlContent = fs.readFileSync(htmlFilePath, "utf-8");
// 移除其他渠道包的 \\n`,
"g"
);
htmlContent = htmlContent.replace(scriptRegex, "");
});
// 移除所有渠道的 标签
const metaTagRegex = /\n/g;
htmlContent = htmlContent.replace(metaTagRegex, "");
// 检查并插入新的渠道 script 标签
// let channelScriptTag;
// if (channelName === "vungle" || channelName === "liftoff") {
// channelScriptTag = ` \n`;
// } else {
// channelScriptTag = ` \n`;
// }
// if (!htmlContent.includes(channelScriptTag)) {
// htmlContent = htmlContent.replace(
// /<\/head>/,
// `${channelScriptTag}`
// );
// }
// 如果是 google 渠道,添加 标签
if (channelName === "google") {
const metaTag = `\n`;
if (!htmlContent.includes(metaTag)) {
htmlContent = htmlContent.replace(/<\/head>/, `${metaTag}`);
}
}
if (zipToSingleDir) {
// 替换包含 window.vConsole = new VConsole 的 \n`;
htmlContent = htmlContent.replace(vConsoleScriptRegex, cocosScriptTag);
// 额外引入 loader-and-starter.js 和 asset-map.js 到 body 中
const loaderScriptTag = `\n`;
const assetMapScriptTag = `\n`;
const bodyScriptTags = `${cocosScriptTag}${loaderScriptTag}${assetMapScriptTag}`;
if (
!htmlContent.includes(loaderScriptTag) &&
!htmlContent.includes(assetMapScriptTag)
) {
htmlContent = htmlContent.replace(cocosScriptTag, bodyScriptTags);
}
}
// 移除单渠道的 SDK script 标签
// const sdkScriptTag = `\n`;
// htmlContent = htmlContent.replace(sdkScriptTag, "");
fs.writeFileSync(htmlFilePath, htmlContent);
console.log(`已将 ${channelName}.js 和相关 script 引入到 index.html 中`);
createDist();
// 判断 zip 是否需要转成单目录包
if (zipToSingleDir) {
do_task();
// 重命名生成的 index.html 为对应的渠道名称
const distIndexPath = path.join('dist', 'index.html');
const channelIndexPath = path.join('dist', `${outputPrefix}${channelName}.html`);
if (fs.existsSync(distIndexPath)) {
fs.renameSync(distIndexPath, channelIndexPath);
console.log(`已重命名 index.html 为 ${channelName}.html`);
}
} else {
// 打包 web-mobile 目录为 zip 文件
const output = fs.createWriteStream(
path.join("dist", `${outputPrefix}${channelName}.zip`)
);
const archive = archiver("zip", {
zlib: { level: 9 }, // 设置压缩级别
});
output.on("close", function () {
console.log(
`已创建 ${channelName}.zip,大小为 ${archive.pointer()} 字节`
);
});
archive.on("error", function (err) {
throw err;
});
archive.pipe(output);
archive.directory("web-mobile/", false);
await archive.finalize();
}
}
// 提取 html 中的 js 文件
console.log("开始转成单目录包----");
// if (zipToSingleDir) {
// processHtmlFiles(path.join(__dirname, 'dist'), jsPrefix);
// }
}
export default processChannels;