Repository navigation
Expand file tree
/
Copy pathpoints-config.js
More file actions
172 lines (151 loc) · 4.56 KB
/
Copy pathpoints-config.js
File metadata and controls
172 lines (151 loc) · 4.56 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
import Store from 'electron-store';
import { getUserDataPath } from './config-path.js';
import { CampusDeviceIdService } from './campus-device-id-service.js';
import { getEnvFileCandidates } from './utils/env-file-helpers.js';
import { decryptEnvContent } from './utils/env-file-crypto.js';
import { normalizeBaseUrl, normalizeString } from './utils/string-helpers.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PROJECT_ROOT = path.resolve(__dirname, '..');
const DEFAULT_BASE_URL = 'https://ossptest.voicecloud.cn/loomy/integration';
const DEFAULT_CONFIG_FILE_PATH = path.resolve(PROJECT_ROOT, 'config', 'points.config.local.json');
function readJsonFile(filePath) {
try {
if (!fs.existsSync(filePath)) {
return {};
}
const content = fs.readFileSync(filePath, 'utf-8');
const parsed = JSON.parse(content);
return parsed && typeof parsed === 'object' ? parsed : {};
} catch (error) {
console.warn('[PointsConfig] 读取积分配置文件失败:', filePath, error);
return {};
}
}
function readEnvFile(filePath) {
try {
if (!fs.existsSync(filePath)) {
return {};
}
const content = decryptEnvContent(fs.readFileSync(filePath, 'utf-8'));
return dotenv.parse(content);
} catch (error) {
console.warn('[PointsConfig] 读取环境配置文件失败:', filePath, error);
return {};
}
}
function getSearchRoots() {
return [
PROJECT_ROOT,
process.cwd(),
process.resourcesPath,
].filter(Boolean);
}
function resolveEnvFileConfig() {
const envFileCandidates = getEnvFileCandidates({
explicitEnvPath: normalizeString(process.env.LOOMY_POINTS_ENV_PATH || process.env.LOOMY_ENV_PATH),
searchRoots: getSearchRoots(),
});
let resolvedPath = envFileCandidates[0] || '';
let values = {
baseUrl: '',
token: '',
};
for (const envPath of envFileCandidates) {
const envValues = readEnvFile(envPath);
const nextValues = {
baseUrl: normalizeString(
envValues.LOOMY_POINTS_BASE_URL
|| envValues.VITE_LOOMY_POINTS_BASE_URL
),
token: normalizeString(
envValues.LOOMY_POINTS_TOKEN
|| envValues.VITE_POINTS_TOKEN
|| envValues.LOOMY_ASR_TOKEN
|| envValues.VITE_ASR_TOKEN
),
};
if (!nextValues.baseUrl && !nextValues.token) {
continue;
}
resolvedPath = envPath;
values = nextValues;
break;
}
return {
path: resolvedPath,
values,
};
}
function resolveFileConfig() {
const envConfigPath = normalizeString(process.env.LOOMY_POINTS_CONFIG_PATH);
const configPath = envConfigPath || DEFAULT_CONFIG_FILE_PATH;
const fileConfig = readJsonFile(configPath);
return {
path: configPath,
values: {
baseUrl: normalizeString(fileConfig.baseUrl),
token: normalizeString(fileConfig.token || fileConfig.asrToken),
},
};
}
export class PointsConfig {
constructor() {
this.store = new Store({
cwd: getUserDataPath(),
name: 'points-config',
defaults: {
baseUrl: DEFAULT_BASE_URL,
token: '',
campusDeviceId: '',
},
});
this.campusDeviceIdService = new CampusDeviceIdService({
store: this.store,
});
}
getConfig() {
const envFileConfig = resolveEnvFileConfig();
const fileConfig = resolveFileConfig();
const resolvedBaseUrl =
normalizeBaseUrl(
process.env.LOOMY_POINTS_BASE_URL
|| process.env.VITE_LOOMY_POINTS_BASE_URL
|| envFileConfig.values.baseUrl
)
|| normalizeBaseUrl(fileConfig.values.baseUrl)
|| normalizeBaseUrl(this.store.get('baseUrl'))
|| DEFAULT_BASE_URL;
return {
envFilePath: envFileConfig.path,
configFilePath: fileConfig.path,
baseUrl: resolvedBaseUrl,
token:
normalizeString(
process.env.LOOMY_POINTS_TOKEN
|| process.env.VITE_POINTS_TOKEN
|| process.env.LOOMY_ASR_TOKEN
|| process.env.VITE_ASR_TOKEN
|| envFileConfig.values.token
)
|| normalizeString(fileConfig.values.token)
|| normalizeString(this.store.get('token')),
};
}
saveConfig({ baseUrl, token }) {
if (baseUrl) {
this.store.set('baseUrl', normalizeBaseUrl(baseUrl));
}
if (typeof token === 'string') {
this.store.set('token', normalizeString(token));
}
}
getCampusDeviceId() {
return this.campusDeviceIdService.getDeviceId();
}
}
export const pointsConfig = new PointsConfig();