Repository navigation
Expand file tree
/
Copy pathstorage.js
More file actions
66 lines (57 loc) · 1.79 KB
/
Copy pathstorage.js
File metadata and controls
66 lines (57 loc) · 1.79 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
/**
* storage.js — Reads/writes setlist-meta.json for persistent metadata
* (notes, colors, virtual order, excluded songs)
*/
import { readFile, writeFile } from "fs/promises";
import { existsSync } from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const META_FILE = path.join(__dirname, "setlist-meta.json");
let cache = null;
// Serializes writes so rapid-fire calls (e.g. drag-reorder followed
// immediately by a notes save) can't interleave and clobber each other —
// each write now waits for the previous one to finish before starting.
let writeQueue = Promise.resolve();
export async function loadMeta() {
if (cache) return cache;
if (!existsSync(META_FILE)) {
cache = { songs: {}, order: [] };
return cache;
}
try {
const raw = await readFile(META_FILE, "utf8");
cache = JSON.parse(raw);
cache.songs = cache.songs || {};
cache.order = cache.order || [];
return cache;
} catch (e) {
console.error("[storage] Failed to load meta:", e.message);
cache = { songs: {}, order: [] };
return cache;
}
}
function saveMeta() {
writeQueue = writeQueue.then(async () => {
try {
await writeFile(META_FILE, JSON.stringify(cache, null, 2), "utf8");
} catch (e) {
console.error("[storage] Failed to save meta:", e.message);
}
});
return writeQueue;
}
export async function setSongMeta(songName, updates) {
const meta = await loadMeta();
meta.songs[songName] = { ...(meta.songs[songName] || {}), ...updates };
await saveMeta();
return meta.songs[songName];
}
export async function saveOrder(orderedNames) {
const meta = await loadMeta();
meta.order = orderedNames;
await saveMeta();
}
export async function clearCache() {
cache = null;
}