54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
import fs from 'fs';
|
|
import { ensureWritableConfigFile, getBundledConfigPath, getWritableConfigPath } from './runtime.js';
|
|
|
|
function readJson(filePath) {
|
|
try {
|
|
if (!filePath || !fs.existsSync(filePath)) {
|
|
return {};
|
|
}
|
|
|
|
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
function mergeConfig(defaultConfig, currentConfig) {
|
|
return {
|
|
...defaultConfig,
|
|
...currentConfig,
|
|
remoteWs: {
|
|
...(defaultConfig.remoteWs || {}),
|
|
...(currentConfig.remoteWs || {})
|
|
}
|
|
};
|
|
}
|
|
|
|
export default {
|
|
read() {
|
|
try {
|
|
const configPath = ensureWritableConfigFile();
|
|
const bundledConfig = readJson(getBundledConfigPath());
|
|
const writableConfig = readJson(configPath);
|
|
const mergedConfig = mergeConfig(bundledConfig, writableConfig);
|
|
|
|
if (JSON.stringify(writableConfig) !== JSON.stringify(mergedConfig)) {
|
|
fs.writeFileSync(configPath, JSON.stringify(mergedConfig, null, 2), 'utf-8');
|
|
}
|
|
|
|
return mergedConfig;
|
|
} catch {
|
|
return {};
|
|
}
|
|
},
|
|
write(newConfig) {
|
|
try {
|
|
const configPath = getWritableConfigPath();
|
|
fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2), 'utf-8');
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
};
|