soyoo-cocos/scriptBefore.js

77 lines
2.8 KiB
JavaScript
Raw 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.

const fs = require('fs')
const path = require('path')
// 脚本执行之前的操作
var iosUrl = "https://apps.apple.com/kr/app/id6467117398"
var androidUrl = "https://play.google.com/store/apps/details?id=com.mxdzzus.google"
// 版本一(英文版)
// 安卓https://play.google.com/store/apps/details?id=com.mxdzzus.google
// iOShttps://apps.apple.com/app/legend-of-mushroom/id6475333787
// 版本二(韩语)
// 安卓https://play.google.com/store/apps/details?id=com.mxdzzkr.google
// iOShttps://apps.apple.com/kr/app/id6467117398
function removeDist() {
const isExists = fs.existsSync(path.join(__dirname, 'dist'))
if (isExists) {
fs.rmdirSync(path.join(__dirname, 'dist'), { recursive: true })
}
if (!isExists) {
fs.mkdirSync(path.join(__dirname, 'dist'))
}
}
function replaceAppStoreUrl() {
// 1. 替换mraid_support.js文件中的iosUrl和androidUrl
var mraid_support_js = fs.readFileSync(path.join(__dirname, 'networks', 'mraid_support.js'), 'utf8')
mraid_support_js = mraid_support_js.replace(/var iosurl\s*=\s*".*"/g, `var iosUrl = "${iosUrl}"`)
mraid_support_js = mraid_support_js.replace(/var androidUrl\s*=\s*".*"/g, `var androidUrl = "${androidUrl}"`);
fs.writeFileSync(path.join(__dirname, 'networks', 'mraid_support.js'), mraid_support_js, 'utf8')
}
function replaceCss() {
// 获取 web-mobile 目录中的 splash 开头的 png 文件
const splashFile = fs.readdirSync('web-mobile').find(file => file.startsWith('splash') && file.endsWith('.png'));
if (!splashFile) return;
// 读取并转换为 base64
const splashFilePath = path.join('web-mobile', splashFile);
const splashBase64 = fs.readFileSync(splashFilePath, 'base64');
const splashBase64Url = `url(data:image/png;base64,${splashBase64})`;
// 使用正则表达式查找带有哈希值的 CSS 文件
const cssFiles = fs.readdirSync('web-mobile').filter(file => {
return /style-(mobile|desktop)\.[a-f0-9]+\.css$/.test(file)
});
cssFiles.forEach(cssFile => {
const cssFilePath = path.join('web-mobile', cssFile);
let cssContent = fs.readFileSync(cssFilePath, 'utf8');
const splashRegex = /url\(\.\/splash\.\w+\.png\)/g;
if (splashRegex.test(cssContent)) {
cssContent = cssContent.replace(splashRegex, splashBase64Url);
}
if (cssFile.includes('desktop') && !cssContent.includes('orientation: landscape')) {
const mediaRule = '\n@media (orientation: landscape) {#splash {background-size: 15% !important;}}';
cssContent += mediaRule;
}
fs.writeFileSync(cssFilePath, cssContent, 'utf8');
});
}
function startScript() {
removeDist()
replaceCss()
replaceAppStoreUrl()
console.log('脚本执行完毕')
}
startScript()