ai-clone/frontend-ai/config/switch-env.js
2026-03-05 14:29:21 +08:00

71 lines
1.9 KiB
JavaScript
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.

/**
* 环境切换辅助脚本
* 快速切换开发/生产环境
*
* 使用方法:
* 1. 在 package.json 中添加脚本:
* "dev": "node config/switch-env.js development && npm run dev:mp-weixin"
* "prod": "node config/switch-env.js production && npm run build:mp-weixin"
*
* 2. 或者直接运行:
* node config/switch-env.js development
* node config/switch-env.js production
*/
const fs = require('fs');
const path = require('path');
// 获取命令行参数
const targetEnv = process.argv[2];
if (!targetEnv || !['development', 'production'].includes(targetEnv)) {
console.error('❌ 错误: 请指定环境 (development 或 production)');
console.log('使用方法: node switch-env.js [development|production]');
process.exit(1);
}
// API 配置文件路径
const apiConfigPath = path.join(__dirname, 'api.js');
try {
// 读取配置文件
let content = fs.readFileSync(apiConfigPath, 'utf8');
// 替换环境变量
const envRegex = /const ENV = ['"](\w+)['"];/;
const currentEnv = content.match(envRegex)?.[1];
if (currentEnv === targetEnv) {
console.log(`✅ 当前已经是 ${targetEnv} 环境,无需切换`);
process.exit(0);
}
content = content.replace(envRegex, `const ENV = '${targetEnv}';`);
// 写入文件
fs.writeFileSync(apiConfigPath, content, 'utf8');
// 获取环境配置信息
const envConfig = {
development: {
baseURL: 'http://115.190.167.176:20002',
name: '开发环境(服务器 115.190.167.176:20002'
},
production: {
baseURL: 'http://115.190.167.176:20002',
name: '生产环境(服务器 115.190.167.176:20002'
}
};
const config = envConfig[targetEnv];
console.log('\n🎉 环境切换成功!\n');
console.log(`📍 当前环境: ${config.name}`);
console.log(`🔗 API地址: ${config.baseURL}`);
console.log('\n⚠ 请重新编译项目以使更改生效\n');
} catch (error) {
console.error('❌ 切换失败:', error.message);
process.exit(1);
}