181 lines
5.1 KiB
JavaScript
181 lines
5.1 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
// 环境配置
|
|
const envConfigs = {
|
|
A: {
|
|
name: "数字项管",
|
|
displayName: "A (数字项管)",
|
|
appId: "wx46466c7828eede2b",
|
|
baseUrl: "https://xiangguan.sxyanzhu.com/wechat",
|
|
baseImgUrl: "https://xiangguan.sxyanzhu.com",
|
|
},
|
|
B: {
|
|
name: "筑安施工",
|
|
displayName: "B (筑安施工)",
|
|
appId: "wx007a8fd50dc185b2",
|
|
baseUrl: "https://jaszpt.crfeb.com.cn/wechat",
|
|
baseImgUrl: "https://jaszpt.crfeb.com.cn",
|
|
},
|
|
};
|
|
|
|
// 文件路径
|
|
const configJsPath = path.join(__dirname, "miniprogram", "config.js");
|
|
const configWxsPath = path.join(
|
|
__dirname,
|
|
"miniprogram",
|
|
"utils",
|
|
"config.wxs"
|
|
);
|
|
const projectConfigPath = path.join(
|
|
__dirname,
|
|
"miniprogram",
|
|
"project.config.json"
|
|
);
|
|
|
|
// 更新 config.js
|
|
function updateConfigJs(env) {
|
|
let content = fs.readFileSync(configJsPath, "utf8");
|
|
|
|
// 更新 appId
|
|
content = content.replace(/appId:\s*"[^"]*"/, `appId: "${env.appId}"`);
|
|
|
|
// 更新 baseUrl (取消注释目标环境,注释其他环境)
|
|
if (env.name === "数字项管") {
|
|
// 启用数字项管,注释其他
|
|
content = content.replace(
|
|
/\/\/baseUrl:\s*"https:\/\/xiangguan\.sxyanzhu\.com\/wechat"/,
|
|
`baseUrl: "${env.baseUrl}"`
|
|
);
|
|
content = content.replace(
|
|
/baseUrl:\s*"https:\/\/jaszpt\.crfeb\.com\.cn\/wechat"/,
|
|
`//baseUrl: "https://jaszpt.crfeb.com.cn/wechat"`
|
|
);
|
|
content = content.replace(
|
|
/\/\/baseUrl:\s*"http:\/\/127\.0\.0\.1:8080"/,
|
|
`//baseUrl: "http://127.0.0.1:8080"`
|
|
);
|
|
} else {
|
|
// 启用筑安施工,注释其他
|
|
content = content.replace(
|
|
/\/\/baseUrl:\s*"https:\/\/jaszpt\.crfeb\.com\.cn\/wechat"/,
|
|
`baseUrl: "${env.baseUrl}"`
|
|
);
|
|
content = content.replace(
|
|
/baseUrl:\s*"https:\/\/xiangguan\.sxyanzhu\.com\/wechat"/,
|
|
`//baseUrl: "https://xiangguan.sxyanzhu.com/wechat"`
|
|
);
|
|
content = content.replace(
|
|
/\/\/baseUrl:\s*"http:\/\/127\.0\.0\.1:8080"/,
|
|
`//baseUrl: "http://127.0.0.1:8080"`
|
|
);
|
|
}
|
|
|
|
// 更新 baseImgUrl (取消注释目标环境,注释其他)
|
|
if (env.name === "数字项管") {
|
|
// 启用数字项管,注释其他
|
|
content = content.replace(
|
|
/\/\/baseImgUrl:\s*"https:\/\/xiangguan\.sxyanzhu\.com"/,
|
|
`baseImgUrl: "${env.baseImgUrl}"`
|
|
);
|
|
content = content.replace(
|
|
/baseImgUrl:\s*"https:\/\/jaszpt\.crfeb\.com\.cn"/,
|
|
`//baseImgUrl: "https://jaszpt.crfeb.com.cn"`
|
|
);
|
|
content = content.replace(
|
|
/\/\/baseImgUrl:\s*'http:\/\/127\.0\.0\.1:9300'/,
|
|
`//baseImgUrl: 'http://127.0.0.1:9300'`
|
|
);
|
|
} else {
|
|
// 启用筑安施工,注释其他
|
|
content = content.replace(
|
|
/\/\/baseImgUrl:\s*"https:\/\/jaszpt\.crfeb\.com\.cn"/,
|
|
`baseImgUrl: "${env.baseImgUrl}"`
|
|
);
|
|
content = content.replace(
|
|
/baseImgUrl:\s*"https:\/\/xiangguan\.sxyanzhu\.com"/,
|
|
`//baseImgUrl: "https://xiangguan.sxyanzhu.com"`
|
|
);
|
|
content = content.replace(
|
|
/\/\/baseImgUrl:\s*'http:\/\/127\.0\.0\.1:9300'/,
|
|
`//baseImgUrl: 'http://127.0.0.1:9300'`
|
|
);
|
|
}
|
|
|
|
fs.writeFileSync(configJsPath, content, "utf8");
|
|
console.log(`✅ 已更新 config.js 为 ${env.name} 环境`);
|
|
}
|
|
|
|
// 更新 config.wxs
|
|
function updateConfigWxs(env) {
|
|
let content = fs.readFileSync(configWxsPath, "utf8");
|
|
|
|
// 更新 baseImgUrl
|
|
content = content.replace(
|
|
/baseImgUrl:\s*"[^"]*"/,
|
|
`baseImgUrl: "${env.baseImgUrl}"`
|
|
);
|
|
|
|
fs.writeFileSync(configWxsPath, content, "utf8");
|
|
console.log(`✅ 已更新 config.wxs 为 ${env.name} 环境`);
|
|
}
|
|
|
|
// 更新 project.config.json
|
|
function updateProjectConfig(env) {
|
|
const content = fs.readFileSync(projectConfigPath, "utf8");
|
|
const config = JSON.parse(content);
|
|
|
|
// 更新 appId
|
|
config.appid = env.appId;
|
|
|
|
fs.writeFileSync(projectConfigPath, JSON.stringify(config, null, 4), "utf8");
|
|
console.log(`✅ 已更新 project.config.json 为 ${env.name} 环境`);
|
|
}
|
|
|
|
// 主函数
|
|
function main() {
|
|
const args = process.argv.slice(2);
|
|
let envName = args[0];
|
|
|
|
// 如果没有传参数,提示用户选择
|
|
if (!envName) {
|
|
console.log("🔧 当前支持的环境:");
|
|
console.log(" A - 数字项管");
|
|
console.log(" B - 筑安施工");
|
|
console.log("\n💡 使用方法: node switchEnv.js <环境标识>");
|
|
console.log(" 例如: node switchEnv.js A");
|
|
return;
|
|
}
|
|
|
|
// 匹配环境
|
|
let selectedEnv = null;
|
|
for (const [key, env] of Object.entries(envConfigs)) {
|
|
if (key.includes(envName) || env.name.includes(envName)) {
|
|
selectedEnv = env;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!selectedEnv) {
|
|
console.error(`❌ 未找到环境: ${envName}`);
|
|
console.log("🔧 支持的环境:");
|
|
Object.entries(envConfigs).forEach(([key, env]) => {
|
|
console.log(` ${key} - ${env.name}`);
|
|
});
|
|
return;
|
|
}
|
|
|
|
console.log(`🔄 正在切换到 ${selectedEnv.displayName} 环境...`);
|
|
|
|
try {
|
|
updateConfigJs(selectedEnv);
|
|
updateConfigWxs(selectedEnv);
|
|
updateProjectConfig(selectedEnv);
|
|
console.log(`\n🎉 环境切换成功!当前环境: ${selectedEnv.displayName}`);
|
|
} catch (error) {
|
|
console.error(`❌ 环境切换失败: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
main();
|