-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig-manager.js
More file actions
285 lines (256 loc) · 7.89 KB
/
config-manager.js
File metadata and controls
285 lines (256 loc) · 7.89 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const fs = require('fs');
const path = require('path');
const os = require('os');
class ConfigManager {
constructor() {
// Use app.getPath('userData') for Electron apps, fallback to __dirname for development
try {
const { app } = require('electron');
this.configPath = path.join(app.getPath('userData'), 'config.json');
console.log('Using Electron userData path:', this.configPath);
} catch (error) {
// Fallback for when not running in Electron context
// Try to find the project root directory
let projectRoot = __dirname;
while (projectRoot !== '/' && !fs.existsSync(path.join(projectRoot, 'package.json'))) {
projectRoot = path.dirname(projectRoot);
}
this.configPath = path.join(projectRoot, 'config.json');
console.log('Using project root fallback path:', this.configPath);
}
this.config = this.loadConfig();
}
/**
* Load configuration from file or create default
*/
loadConfig() {
try {
console.log('Loading config from:', this.configPath);
console.log('File exists:', fs.existsSync(this.configPath));
if (fs.existsSync(this.configPath)) {
const configData = fs.readFileSync(this.configPath, 'utf8');
console.log('Config file content length:', configData.length);
const config = JSON.parse(configData);
console.log('Parsed config:', config);
return this.expandPaths(config);
} else {
// Create default config
console.log('Config file does not exist, creating default');
const defaultConfig = this.getDefaultConfig();
this.saveConfig(defaultConfig);
return defaultConfig;
}
} catch (error) {
console.error('Error loading config:', error);
return this.getDefaultConfig();
}
}
/**
* Get default configuration
*/
getDefaultConfig() {
const homeDir = os.homedir();
return {
library: {
musicDirectories: [
path.join(homeDir, 'Music'),
path.join(homeDir, 'Downloads', 'Music'),
path.join(homeDir, 'Desktop', 'Music')
],
supportedExtensions: ['mp3', 'flac', 'ogg', 'wav', 'aac', 'm4a'],
autoScan: true,
scanInterval: 300,
excludePatterns: [
'**/.*',
'**/System Volume Information/**',
'**/Thumbs.db',
'**/desktop.ini'
]
},
audio: {
defaultVolume: 0.7,
crossfade: true,
crossfadeDuration: 3.0,
replayGain: true
},
playlists: {
defaultDirectory: path.join(homeDir, 'Music', 'Playlists'),
autoSave: true,
maxHistory: 50
},
interface: {
theme: 'dark',
language: 'en',
showFileExtensions: false,
compactMode: false
},
services: {
lastfm: {
apiKey: '',
sharedSecret: ''
}
},
advanced: {
enableLogging: true,
logLevel: 'info',
cacheSize: 1000,
autoUpdate: true
}
};
}
/**
* Expand paths with environment variables and home directory
*/
expandPaths(config) {
const expanded = JSON.parse(JSON.stringify(config));
// Expand library directories
if (expanded.library && expanded.library.musicDirectories) {
expanded.library.musicDirectories = expanded.library.musicDirectories.map(dir => {
if (typeof dir === 'string') {
return dir.replace(/^~/, os.homedir());
}
return dir;
});
}
// Expand playlist directory
if (expanded.playlists && expanded.playlists.defaultDirectory) {
expanded.playlists.defaultDirectory = expanded.playlists.defaultDirectory.replace(/^~/, os.homedir());
}
if (!expanded.services) {
expanded.services = {};
}
if (!expanded.services.lastfm) {
expanded.services.lastfm = {
apiKey: '',
sharedSecret: ''
};
} else {
expanded.services.lastfm.apiKey = expanded.services.lastfm.apiKey || '';
expanded.services.lastfm.sharedSecret = expanded.services.lastfm.sharedSecret || '';
}
return expanded;
}
/**
* Save configuration to file
*/
saveConfig(config) {
try {
console.log('Saving config to:', this.configPath);
console.log('Config to save:', JSON.stringify(config, null, 2));
// Create backup of existing config
if (fs.existsSync(this.configPath)) {
const backupPath = this.configPath + '.backup';
fs.copyFileSync(this.configPath, backupPath);
console.log('Created backup at:', backupPath);
}
// Save new config
fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2), 'utf8');
console.log('Config saved successfully');
this.config = this.expandPaths(config);
return true;
} catch (error) {
console.error('Error saving config:', error);
console.error('Config path:', this.configPath);
console.error('Error details:', error.message);
return false;
}
}
/**
* Get current configuration
*/
getConfig() {
return this.config;
}
/**
* Update specific configuration section
*/
updateConfig(section, key, value) {
if (!this.config[section]) {
this.config[section] = {};
}
this.config[section][key] = value;
return this.saveConfig(this.config);
}
/**
* Add a music directory
*/
addMusicDirectory(directory) {
console.log('ConfigManager.addMusicDirectory called with:', directory);
const expandedDir = directory.replace(/^~/, os.homedir());
console.log('Expanded directory:', expandedDir);
console.log('Current music directories:', this.config.library.musicDirectories);
if (!this.config.library.musicDirectories.includes(expandedDir)) {
console.log('Adding directory to config');
this.config.library.musicDirectories.push(expandedDir);
const saveResult = this.saveConfig(this.config);
console.log('Save result:', saveResult);
return saveResult;
}
console.log('Directory already exists');
return false; // Directory already exists
}
/**
* Remove a music directory
*/
removeMusicDirectory(directory) {
const expandedDir = directory.replace(/^~/, os.homedir());
const index = this.config.library.musicDirectories.indexOf(expandedDir);
if (index > -1) {
this.config.library.musicDirectories.splice(index, 1);
return this.saveConfig(this.config);
}
return false; // Directory not found
}
/**
* Get music directories
*/
getMusicDirectories() {
return this.config.library.musicDirectories;
}
/**
* Check if a directory is in the music directories
*/
isMusicDirectory(directory) {
const expandedDir = directory.replace(/^~/, os.homedir());
return this.config.library.musicDirectories.includes(expandedDir);
}
/**
* Get supported file extensions
*/
getSupportedExtensions() {
return this.config.library.supportedExtensions;
}
/**
* Check if a file extension is supported
*/
isSupportedExtension(extension) {
return this.config.library.supportedExtensions.includes(extension.toLowerCase());
}
/**
* Reset configuration to defaults
*/
resetToDefaults() {
const defaultConfig = this.getDefaultConfig();
return this.saveConfig(defaultConfig);
}
/**
* Validate configuration
*/
validateConfig(config) {
const errors = [];
// Validate library directories exist
if (config.library && config.library.musicDirectories) {
for (const dir of config.library.musicDirectories) {
if (!fs.existsSync(dir)) {
errors.push(`Music directory does not exist: ${dir}`);
}
}
}
// Validate audio volume range
if (config.audio && (config.audio.defaultVolume < 0 || config.audio.defaultVolume > 1)) {
errors.push('Audio volume must be between 0 and 1');
}
return errors;
}
}
module.exports = ConfigManager;