Initial import of zappier-account-balance from zapier monorepo
This commit is contained in:
commit
498b44e1da
10 changed files with 457 additions and 0 deletions
28
src/persist.js
Normal file
28
src/persist.js
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue