UNPKG

cs2-cdn

Version:

Retrieves the Steam CDN Image URLs for CS2/CS:GO Items

44 lines (43 loc) 1.27 kB
import fs from 'fs'; export default class Store { data; path; constructor(path) { if (!fs.existsSync(path)) { fs.writeFileSync(path, '{}'); } this.path = path; this.data = JSON.parse(fs.readFileSync(path).toString()); if (process.platform === 'win32' || process.platform === 'linux' || process.platform === 'darwin') { fs.watch(path, async () => this.data = await this.#readIn()); } else { fs.watchFile(path, async () => this.data = await this.#readIn()); } } getValue(key) { return this.data[key]; } setValue(key, value) { this.data[key] = value; this.#save(); } #save() { fs.writeFileSync(this.path, JSON.stringify(this.data)); } #readIn() { return new Promise((resolve, reject) => { // ReadFileSync is too unstable in this situation fs.readFile(this.path, (err, data) => { if (err) { return console.log(err); } const file = data.toString(); if (!file) { return; } resolve(JSON.parse(file)); }); }); } }