28 lines
666 B
JavaScript
28 lines
666 B
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
export function booksPath() {
|
|
return (
|
|
process.env.BOOKS_PATH ||
|
|
path.join(process.env.FLEET_STATE_DIR || process.cwd(), 'data', 'books.json')
|
|
);
|
|
}
|
|
|
|
export function loadInto(books) {
|
|
const p = booksPath();
|
|
if (!fs.existsSync(p)) return books;
|
|
try {
|
|
books.load(JSON.parse(fs.readFileSync(p, 'utf8')));
|
|
} catch {
|
|
/* keep empty */
|
|
}
|
|
return books;
|
|
}
|
|
|
|
export function saveFrom(books) {
|
|
const p = booksPath();
|
|
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
const tmp = `${p}.tmp`;
|
|
fs.writeFileSync(tmp, JSON.stringify(books.dump(), null, 2));
|
|
fs.renameSync(tmp, p);
|
|
}
|