@acurast/cli
Version:
A cli to interact with the Acurast Cloud.
51 lines • 1.69 kB
JavaScript
import * as fs from 'fs';
import { ensureDirectoryExistence } from './ensureDirectoryExistence.js';
import { ACURAST_BASE_PATH } from '../constants.js';
export class LocalStorage {
constructor(fileName = 'keys.json', mode, basePath = ACURAST_BASE_PATH) {
this.filePath = `${basePath}/${fileName}`;
this.mode = mode;
this.ensureFile();
}
ensureFile() {
ensureDirectoryExistence(this.filePath);
if (!fs.existsSync(this.filePath)) {
fs.writeFileSync(this.filePath, '{}', { encoding: 'utf8', ...(this.mode ? { mode: this.mode } : {}) });
}
else if (this.mode !== undefined) {
// Best-effort tighten permissions on an existing file (no-op on
// filesystems that ignore Unix modes, e.g. WSL DrvFs / Windows).
try {
fs.chmodSync(this.filePath, this.mode);
}
catch {
// ignore
}
}
}
readStorage() {
const data = fs.readFileSync(this.filePath, 'utf8');
return JSON.parse(data);
}
writeStorage(storage) {
fs.writeFileSync(this.filePath, JSON.stringify(storage, null, 2), 'utf8');
}
getItem(key) {
const storage = this.readStorage();
return storage[key] || null;
}
setItem(key, value) {
const storage = this.readStorage();
storage[key] = value;
this.writeStorage(storage);
}
removeItem(key) {
const storage = this.readStorage();
delete storage[key];
this.writeStorage(storage);
}
clear() {
this.writeStorage({});
}
}
//# sourceMappingURL=LocalStorage.js.map