-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathenv-config.js
More file actions
101 lines (90 loc) · 2.44 KB
/
env-config.js
File metadata and controls
101 lines (90 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* 环境变量配置管理模块
* 统一处理所有环境变量的加载和配置
*/
const fs = require('fs');
const path = require('path');
/**
* 环境变量配置类
*/
class EnvConfig {
constructor() {
this.loadEnvironmentVariables();
}
/**
* 加载环境变量
* 根据脚本所在位置加载 .env 文件
*/
loadEnvironmentVariables() {
try {
// 优先加载 notify-system.js 所在目录的 .env 文件
const envPath = path.join(__dirname, '.env');
if (fs.existsSync(envPath)) {
require('dotenv').config({ path: envPath });
console.log('✅ 环境变量加载成功');
} else {
console.log('⚠️ .env 文件不存在,使用系统环境变量');
require('dotenv').config();
}
} catch (error) {
console.log('❌ 环境变量加载失败:', error.message);
}
}
/**
* 获取飞书配置
*/
getFeishuConfig() {
return {
webhook_url: process.env.FEISHU_WEBHOOK_URL || '',
enabled: process.env.FEISHU_WEBHOOK_URL ? true : false
};
}
/**
* 获取Telegram配置
*/
getTelegramConfig() {
return {
bot_token: process.env.TELEGRAM_BOT_TOKEN || '',
chat_id: process.env.TELEGRAM_CHAT_ID || '',
enabled: !!(process.env.TELEGRAM_BOT_TOKEN && process.env.TELEGRAM_CHAT_ID),
proxy_url: process.env.HTTPS_PROXY ||
process.env.HTTP_PROXY ||
process.env.https_proxy ||
process.env.http_proxy || ''
};
}
/**
* 获取声音通知配置
*/
getSoundConfig() {
return {
enabled: process.env.SOUND_ENABLED !== 'false',
backup: true
};
}
/**
* 获取通用通知配置
*/
getNotificationConfig() {
return {
enabled: process.env.NOTIFICATION_ENABLED !== 'false'
};
}
/**
* 获取所有配置
*/
getAllConfig() {
return {
feishu: this.getFeishuConfig(),
telegram: this.getTelegramConfig(),
sound: this.getSoundConfig(),
notification: this.getNotificationConfig()
};
}
}
// 导出单例实例
const envConfig = new EnvConfig();
module.exports = {
EnvConfig,
envConfig
};