49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const defaultConfig = require('../data/defaultConfig');
|
|
|
|
const CONFIG_DIR = path.join(__dirname, '..', 'config');
|
|
|
|
function ensureDir() {
|
|
if (!fs.existsSync(CONFIG_DIR)) {
|
|
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
}
|
|
}
|
|
|
|
function getConfigPath(profileId = 'shared') {
|
|
return path.join(CONFIG_DIR, `${profileId}-pickup-config.json`);
|
|
}
|
|
|
|
function hydrateConfigFile(profileId) {
|
|
ensureDir();
|
|
const filePath = getConfigPath(profileId);
|
|
if (!fs.existsSync(filePath)) {
|
|
fs.writeFileSync(filePath, JSON.stringify(defaultConfig, null, 2));
|
|
}
|
|
return filePath;
|
|
}
|
|
|
|
function readConfig(profileId) {
|
|
const filePath = hydrateConfigFile(profileId);
|
|
try {
|
|
const raw = fs.readFileSync(filePath, 'utf8');
|
|
const parsed = JSON.parse(raw);
|
|
return Array.isArray(parsed) ? parsed : [];
|
|
} catch (err) {
|
|
console.error(`Failed to read config for ${profileId}:`, err);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function writeConfig(profileId, payload) {
|
|
const filePath = hydrateConfigFile(profileId);
|
|
fs.writeFileSync(filePath, JSON.stringify(payload, null, 2));
|
|
return filePath;
|
|
}
|
|
|
|
module.exports = {
|
|
readConfig,
|
|
writeConfig,
|
|
getConfigPath
|
|
};
|