Files
YGChatCS/scripts/update-appid.js

221 lines
6.2 KiB
JavaScript
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
/**
* 客户端配置切换脚本
* 支持通过客户端名称来切换配置
*
* 使用方法:
* 1. node scripts/update-appid.js <client-name>
* 2. npm run switch-client <client-name>
*/
// 读取客户端配置文件
const configPath = path.join(__dirname, "../client-configs.json");
let CLIENT_CONFIGS;
try {
const configContent = fs.readFileSync(configPath, "utf8");
CLIENT_CONFIGS = JSON.parse(configContent);
} catch (error) {
console.error("❌ 读取配置文件失败:", error.message);
console.error(" 配置文件路径:", configPath);
process.exit(1);
}
// 获取命令行参数
const args = process.argv.slice(2);
const input = args[0];
// 验证参数
if (!input) {
console.error("❌ 错误: 请提供客户端名称");
console.log("使用方法:");
console.log(" node scripts/update-appid.js <client-name>");
console.log(" npm run switch-client <client-name>");
console.log("");
console.log("支持的客户端名称:");
Object.keys(CLIENT_CONFIGS).forEach((key) => {
console.log(` ${key} - ${CLIENT_CONFIGS[key].name}`);
});
console.log("");
console.log("示例:");
console.log(" node scripts/update-appid.js tianmu");
console.log(" npm run switch-client tianmu");
process.exit(1);
}
// 文件路径
const manifestPath = path.join(__dirname, "../src/manifest.json");
const projectConfigPath = path.join(__dirname, "../project.config.json");
/**
* 更新base.js中的getCurrentConfig函数
*/
function updateBaseJs(clientName) {
const basePath = path.join(__dirname, "../src/constant/base.js");
try {
console.log("📝 正在更新 base.js...");
let content = fs.readFileSync(basePath, "utf8");
// 查找getCurrentConfig函数并替换其中的CLIENT_CONFIGS引用
const regex =
/export const getCurrentConfig = \(\) => CLIENT_CONFIGS\.\w+;/;
const newExport = `export const getCurrentConfig = () => CLIENT_CONFIGS.${clientName};`;
if (regex.test(content)) {
content = content.replace(regex, newExport);
fs.writeFileSync(basePath, content, "utf8");
console.log(
`✅ base.js 更新成功,切换到 ${CLIENT_CONFIGS[clientName].name} 配置`
);
return true;
} else {
console.log("⚠️ base.js 中未找到getCurrentConfig函数跳过更新");
return true;
}
} catch (error) {
console.error("❌ 更新base.js失败:", error.message);
return false;
}
}
/**
* 更新manifest.json中的appid
*/
function updateManifestJson(newAppId) {
try {
console.log("📝 正在更新 manifest.json...");
let content = fs.readFileSync(manifestPath, "utf8");
// 由于manifest.json包含注释需要特殊处理
// 使用正则表达式替换mp-weixin中的appid
const regex = /"mp-weixin":\s*{[^}]*"appid":\s*"[^"]*"/;
if (regex.test(content)) {
content = content.replace(regex, (match) => {
return match.replace(/"appid":\s*"[^"]*"/, `"appid": "${newAppId}"`);
});
fs.writeFileSync(manifestPath, content, "utf8");
console.log("✅ manifest.json 更新成功");
} else {
console.log(" manifest.json 中未找到mp-weixin配置跳过更新");
}
return true;
} catch (error) {
console.error("❌ 更新manifest.json失败:", error.message);
return false;
}
}
/**
* 更新project.config.json中的appid
*/
function updateProjectConfigJson(newAppId) {
try {
console.log("📝 正在更新 project.config.json...");
const content = fs.readFileSync(projectConfigPath, "utf8");
const config = JSON.parse(content);
const oldAppId = config.appid;
if (config.appid !== newAppId) {
config.appid = newAppId;
fs.writeFileSync(
projectConfigPath,
JSON.stringify(config, null, 2),
"utf8"
);
console.log("✅ project.config.json 更新成功");
console.log(` 旧appid: ${oldAppId}`);
console.log(` 新appid: ${newAppId}`);
} else {
console.log(" project.config.json 无需更新");
}
return true;
} catch (error) {
console.error("❌ 更新project.config.json失败:", error.message);
return false;
}
}
/**
* 验证必要文件是否存在
*/
function validateFiles() {
const basePath = path.join(__dirname, "../src/constant/base.js");
const files = [
{ path: basePath, name: "base.js" },
{ path: manifestPath, name: "manifest.json" },
{ path: projectConfigPath, name: "project.config.json" },
];
for (const file of files) {
if (!fs.existsSync(file.path)) {
console.error(`❌ 文件不存在: ${file.name}`);
return false;
}
}
return true;
}
/**
* 主函数
*/
function main() {
console.log("🚀 开始更新配置...");
// 解析输入参数
let newAppId;
let clientName;
// 检查是否为有效的客户端名称
if (CLIENT_CONFIGS[input]) {
// 使用客户端名称
clientName = input;
newAppId = CLIENT_CONFIGS[input].appId;
console.log(
`🎯 检测到客户端配置: ${CLIENT_CONFIGS[input].name} (${clientName})`
);
console.log(
`目标客户端: ${CLIENT_CONFIGS[clientName].name} (${clientName})`
);
console.log(`目标appid: ${newAppId}\n`);
// 验证文件存在
if (!validateFiles()) {
process.exit(1);
}
// 更新文件
const baseSuccess = updateBaseJs(clientName);
const manifestSuccess = updateManifestJson(newAppId);
const projectConfigSuccess = updateProjectConfigJson(newAppId);
if (baseSuccess && manifestSuccess && projectConfigSuccess) {
console.log("\n🎉 所有文件更新完成!");
console.log(`💡 已切换到 ${CLIENT_CONFIGS[clientName].name} 客户端配置`);
console.log("💡 提示: 请检查文件内容确认更新正确");
} else {
console.log("\n❌ 部分文件更新失败,请检查错误信息");
process.exit(1);
}
} else {
// 不支持的客户端名称
console.error(`❌ 错误: 不支持的客户端名称 "${input}"`);
console.log("");
console.log("支持的客户端名称:");
Object.keys(CLIENT_CONFIGS).forEach((key) => {
console.log(` ${key} - ${CLIENT_CONFIGS[key].name}`);
});
process.exit(1);
}
}
// 执行主函数
main();