File size: 910 Bytes
69a00c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const fs = require('fs');
const path = require('path');

// Data storage helper functions
const readJSON = (p) => {
  try {
    return JSON.parse(fs.readFileSync(p, 'utf8'));
  } catch {
    return [];
  }
};

const writeJSON = (p, v) => fs.writeFileSync(p, JSON.stringify(v, null, 2));

// Text sanitization
const sanitize = (s) => (s || '').toString().replace(/\u0000/g, '').trim();

// JSON parsing with safety
const safeParseJSON = (s) => {
  try {
    return JSON.parse(s);
  } catch {
    return null;
  }
};

module.exports = {
  readJSON,
  writeJSON,
  sanitize,
  safeParseJSON,
  readUsers: () => readJSON(path.join(process.cwd(), 'data', 'users.json')),
  writeUsers: (a) => writeJSON(path.join(process.cwd(), 'data', 'users.json'), a),
  readPacks: () => readJSON(path.join(process.cwd(), 'data', 'packs.json')),
  writePacks: (a) => writeJSON(path.join(process.cwd(), 'data', 'packs.json'), a)
};