Something went wrong. Try again.
Mirroring: https://github.com/MunifTanjim/scripts.sh
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556export class CacheFile { constructor(name) { this.cachePath = `${os.homedir()}/.cache/@muniftanjim/${name}`; this.content = null; this.exists = false; }
#ensure = async () => { if (this.content && this.exists) { return; }
const cacheDir = String(await $`dirname ${this.cachePath}`).trim();
try { await fs.access(cacheDir); } catch (err) { if (err.code !== 'ENOENT') { throw err; }
await fs.mkdir(cacheDir, { recursive: true }); }
try { const content = await fs.readFile(this.cachePath, { encoding: 'utf8' }); this.content = JSON.parse(content); } catch (err) { this.content = {}; await fs.writeFile(this.cachePath, JSON.stringify(this.content), { encoding: 'utf8', }); }
this.exists = true; };
read = async () => { await this.#ensure(); return this; };
write = async () => { if (!this.content) { this.content = {}; }
await this.#ensure(); fs.writeFile(this.cachePath, JSON.stringify(this.content), { encoding: 'utf8', });
return this; };}