soyoo-cocos/zipChannelScript.js

119 lines
5.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import * as fs from 'fs'
import * as path from 'path'
import archiver from 'archiver'
import { createDist } from './common/utils.js'
async function processChannels(options) {
const zipChannel = options.zipChannel || ['facebook', 'google', 'tiktok', 'vungle', 'liftoff'];
const outputPrefix = options.outputPrefix || '';
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');
// 移除其他渠道包的 <script> 标签
zipChannel.forEach(channel => {
const scriptRegex = new RegExp(`<script src="${channel}.js" charset="utf-8"></script>\\n`, 'g');
htmlContent = htmlContent.replace(scriptRegex, '');
});
// 移除所有渠道的 <meta> 标签
const metaTagRegex = /<meta name="ad.size" content="width=480,height=320">\n/g;
htmlContent = htmlContent.replace(metaTagRegex, '');
// 检查并插入新的渠道 script 标签
let channelScriptTag;
if (channelName === 'vungle' || channelName === 'liftoff') {
channelScriptTag = ` <script src="mraid_support.js" charset="utf-8"></script>\n`;
} else {
channelScriptTag = ` <script src="${channelName}.js" charset="utf-8"></script>\n`;
}
if (!htmlContent.includes(channelScriptTag)) {
htmlContent = htmlContent.replace(/<\/head>/, `${channelScriptTag}</head>`);
}
// 如果是 google 渠道,添加 <meta> 标签
if (channelName === 'google') {
const metaTag = `<meta name="ad.size" content="width=480,height=320">\n`;
if (!htmlContent.includes(metaTag)) {
htmlContent = htmlContent.replace(/<\/head>/, `${metaTag}</head>`);
}
}
// 检查并插入新的 SDK script 标签到 <body> 之前
const sdkScriptTag = `<script src="https://sf16-muse-va.ibytedtos.com/obj/union-fe-nc-i18n/playable/sdk/playable-sdk.js"></script>\n`;
if (!htmlContent.includes(sdkScriptTag)) {
htmlContent = htmlContent.replace(/<\/body>/, `${sdkScriptTag}</body>`);
}
fs.writeFileSync(htmlFilePath, htmlContent);
console.log(`已将 ${channelName}.js 和 SDK script 引入到 index.html 中`);
createDist()
// 打包 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();
}
}
export default processChannels;