diff --git a/.DS_Store b/.DS_Store index c625820..213b1be 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/README.md b/README.md index 0b0cd17..53294c8 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ zip文件的默认打包方式,是根据以下不同渠道操作后,将web-m 在里添加 ``` - + ``` ### 2. 打包 diff --git a/cicdScript.js b/cicdScript.js index df409fe..6c638bc 100644 --- a/cicdScript.js +++ b/cicdScript.js @@ -74,7 +74,10 @@ async function startScript() { ], // html包 htmlChannel: [ + 'facebook', + 'applovin', + // 'unity', // 'appier', 'ironsource', // 'mintegral', 'moloco', diff --git a/htmlChannelScript.js b/htmlChannelScript.js index 62b5d14..0c9d19b 100644 --- a/htmlChannelScript.js +++ b/htmlChannelScript.js @@ -1,117 +1,185 @@ - -import * as fs from 'fs' -import * as path from 'path' -import do_task from './single-html/build.js' -import { createDist } from './common/utils.js' - +import * as fs from "fs"; +import * as path from "path"; +import do_task from "./single-html/build.js"; +import { createDist } from "./common/utils.js"; // 处理 HTML 渠道 async function processHtmlChannels(options) { - const outputPrefix = options.outputPrefix || ''; - const htmlChannel = options.htmlChannel || ['applovin', 'unity', 'appier', 'ironsource', 'mintegral', 'moloco']; - for (const channelName of htmlChannel) { - // 删除所有可能存在的旧文件 - const filesToDelete = ['mraid_support.js', 'mraid.js', 'mintegral.js', 'moloco.js']; - filesToDelete.forEach(file => { - const filePath = path.join('web-mobile', file); - if (fs.existsSync(filePath)) { - fs.unlinkSync(filePath); - console.log(`已删除: ${filePath}`); - } - }); - - // 从 networks 找到复制到 web-mobile 中 - const mraidSupportFile = path.join('networks', 'mraid_support.js'); - const mraidDestinationFile = path.join('web-mobile', 'mraid_support.js'); - - if (['applovin', 'unity', 'appier', 'ironsource'].includes(channelName)) { - if (fs.existsSync(mraidSupportFile)) { - fs.copyFileSync(mraidSupportFile, mraidDestinationFile); - console.log(`已复制: ${mraidSupportFile} 到 ${mraidDestinationFile}`); - } - } - - if (['mintegral', 'moloco'].includes(channelName)) { - const fileName = `${channelName}.js`; - const sourceFile = path.join('networks', fileName); - const destFile = path.join('web-mobile', fileName); - if (fs.existsSync(sourceFile)) { - fs.copyFileSync(sourceFile, destFile); - console.log(`已复制: ${sourceFile} 到 ${destFile}`); - } - } - - const htmlFilePath = path.join('web-mobile', 'index.html'); - let htmlContent = fs.readFileSync(htmlFilePath, 'utf-8'); - - // 移除非当前渠道包的 \\n`, 'g'); - htmlContent = htmlContent.replace(scriptRegex, ''); - } - }); - - // 移除 mraid_support.js 和 mraid.js 的 \n`; - } else { - channelScriptTag = ` \n`; - } - - if (!htmlContent.includes(channelScriptTag)) { - htmlContent = htmlContent.replace(/<\/head>/, `${channelScriptTag}`); - } - - // 替换包含 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 中`); - - // 调用 do_task 方法,将 web-mobile 下的所有包打包成一个 HTML - createDist() - - do_task(); - - // 如果是 ironsource 渠道,将 插入到 body 标签之前 - if (channelName === 'ironsource') { - htmlContent = fs.readFileSync(path.join('dist', 'index.html'), 'utf-8'); - htmlContent = htmlContent.replace(/<\/body>/, ``); - fs.writeFileSync(path.join('dist', 'index.html'), htmlContent); - console.log(`已将 写入到 ironsource.html 的 body 标签之前`); - } - - // 重命名生成的 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`); + const outputPrefix = options.outputPrefix || ""; + const htmlChannel = options.htmlChannel || [ + "applovin", + "unity", + "appier", + "ironsource", + "mintegral", + "moloco", + ]; + for (const channelName of htmlChannel) { + // 遍历 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 中 + if (["applovin", "unity", "appier", "ironsource"].includes(channelName)) { + 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 (["mintegral", "moloco"].includes(channelName)) { + const fileName = `${channelName}.js`; + const sourceFile = path.join("networks", fileName); + const destFile = path.join("web-mobile", fileName); + if (fs.existsSync(sourceFile)) { + fs.copyFileSync(sourceFile, destFile); + console.log(`已复制: ${sourceFile} 到 ${destFile}`); + } + } + + if (["facebook", "google", "tiktok"].includes(channelName)) { + if (channelName == "tiktok") { + fs.copyFileSync( + path.join("facebook/config.json"), + path.join("web-mobile", `config.json`) + ); + fs.copyFileSync( + path.join("tiktok/tiktok.js"), + path.join("web-mobile", `tiktok.js`) + ); + } else { + const sourceFile = path.join("networks", `${channelName}.js`); + const destinationFile = path.join("web-mobile", `${channelName}.js`); + console.log("------------------------", sourceFile); + if (fs.existsSync(sourceFile)) { + fs.copyFileSync(sourceFile, destinationFile); + console.log(`已复制: ${sourceFile} 到 ${destinationFile}`); + } + } + } + + const htmlFilePath = path.join("web-mobile", "index.html"); + // 读取 index.html 文件 + let htmlContent = fs.readFileSync(htmlFilePath, "utf-8"); + + // 移除非当前渠道包的 \\n`, + "g" + ); + htmlContent = htmlContent.replace(scriptRegex, ""); + } + }); + + // 移除 mraid_support.js 和 mraid.js 的 \n`; + } else { + // @TODO + if (!channelName == "facebook") { + channelScriptTag = ` \n`; + } + } + + if (!htmlContent.includes(channelScriptTag)) { + htmlContent = htmlContent.replace( + /<\/head>/, + `${channelScriptTag}` + ); + } + + // 替换包含 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(cocosScriptTag) && + !htmlContent.includes(assetMapScriptTag) + ) { + htmlContent = htmlContent.replace(cocosScriptTag, bodyScriptTags); + } + + // 移除单渠道的 SDK script 标签 + if (!["tiktok", "facebook", "google"].includes(channelName)) { + const sdkScriptTag = `\n`; + htmlContent = htmlContent.replace(sdkScriptTag, ""); + } + + fs.writeFileSync(htmlFilePath, htmlContent); + console.log(`已将 ${channelName}.js 和相关 script 引入到 index.html 中`); + + // 调用 do_task 方法,将 web-mobile 下的所有包打包成一个 HTML + createDist(); + + do_task(); + + // 如果是 ironsource 渠道,将 插入到 body 标签之前 + if (channelName === "ironsource") { + htmlContent = fs.readFileSync(path.join("dist", "index.html"), "utf-8"); + htmlContent = htmlContent.replace( + /<\/body>/, + `` + ); + fs.writeFileSync(path.join("dist", "index.html"), htmlContent); + console.log( + `已将 写入到 ironsource.html 的 body 标签之前` + ); + } + + // 重命名生成的 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`); + } + } } export default processHtmlChannels; diff --git a/single-html/build.js b/single-html/build.js index 284bfb5..5de323b 100644 --- a/single-html/build.js +++ b/single-html/build.js @@ -101,7 +101,11 @@ function replaceRelativeScript(html) { matchedScripts.forEach((script) => { var reg = /(\\n`; - // } else { - // channelScriptTag = ` \n`; - // } + let channelScriptTag; + if (channelName === "vungle" || channelName === "liftoff") { + channelScriptTag = ` \n`; + } else { + channelScriptTag = ` \n`; + } - // if (!htmlContent.includes(channelScriptTag)) { - // htmlContent = htmlContent.replace( - // /<\/head>/, - // `${channelScriptTag}` - // ); - // } + if (!htmlContent.includes(channelScriptTag)) { + htmlContent = htmlContent.replace( + /<\/head>/, + `${channelScriptTag}` + ); + } // 如果是 google 渠道,添加 标签 if (channelName === "google") { @@ -109,7 +105,38 @@ async function processChannels(options) { } } - if (zipToSingleDir) { + fs.writeFileSync(htmlFilePath, htmlContent); + console.log(`已将 ${channelName}.js 和 SDK script 引入到 index.html 中`); + + createDist(); + + if (!zipToSingleDir) { + // 打包 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(); + } else { + // 单html包 + const htmlFilePath = path.join("web-mobile", "index.html"); + let htmlContent = fs.readFileSync(htmlFilePath, "utf-8"); + // 替换包含 window.vConsole = new VConsole 的 \n`; - // htmlContent = htmlContent.replace(sdkScriptTag, ""); + // 检查并插入新的 SDK script 标签到 之前 + // const sdkScriptTag = `\n`; + // if (!htmlContent.includes(sdkScriptTag)) { + // htmlContent = htmlContent.replace(/<\/body>/, `${sdkScriptTag}`); + // } - fs.writeFileSync(htmlFilePath, htmlContent); - console.log(`已将 ${channelName}.js 和相关 script 引入到 index.html 中`); + 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;