-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscenes.js
44 lines (36 loc) · 1014 Bytes
/
scenes.js
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
const fs = require('fs').promises;
const path = require('path');
const SCENES_FILE = path.join(__dirname, 'scenes.json');
let scenes = [];
async function loadScenes() {
try {
const data = await fs.readFile(SCENES_FILE, 'utf8');
const parsed = JSON.parse(data);
scenes = parsed.scenes.map(scene => scene[0]); // Extract the first (and only) string from each array
} catch (error) {
console.error('Error loading scenes:', error);
scenes = [];
}
}
async function saveScenes() {
try {
const data = JSON.stringify({ scenes: scenes.map(scene => [scene]) }, null, 2);
await fs.writeFile(SCENES_FILE, data, 'utf8');
} catch (error) {
console.error('Error saving scenes:', error);
}
}
function addScene(scene) {
scenes.push(scene);
}
function getScenes() {
return scenes;
}
// Load scenes when the module is first imported
loadScenes();
module.exports = {
loadScenes,
saveScenes,
addScene,
getScenes
};