2021-04-07 10:58:09 +00:00
|
|
|
// Handles most of the file system operations, all of the ones related to `data` at least.
|
2020-12-15 01:44:28 +00:00
|
|
|
import fs from "fs";
|
2020-07-25 08:15:26 +00:00
|
|
|
|
|
|
|
const Storage = {
|
2020-12-15 01:44:28 +00:00
|
|
|
read(header: string): object {
|
|
|
|
this.open("data");
|
|
|
|
const path = `data/${header}.json`;
|
|
|
|
let data = {};
|
2020-10-15 09:23:24 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
if (fs.existsSync(path)) {
|
|
|
|
const file = fs.readFileSync(path, "utf-8");
|
2020-10-15 09:23:24 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
try {
|
|
|
|
data = JSON.parse(file);
|
|
|
|
} catch (error) {
|
|
|
|
if (process.argv[2] !== "dev") {
|
2021-05-08 13:32:45 +00:00
|
|
|
console.warn("[storage.read]", `Malformed JSON data (header: ${header}), backing it up.`, file);
|
|
|
|
fs.writeFile(`${path}.backup`, file, (error) => {
|
|
|
|
if (error) console.error("[storage.read]", error);
|
|
|
|
console.log("[storage.read]", `Backup file of "${header}" successfully written as ${file}.`);
|
|
|
|
});
|
2020-12-15 01:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-15 09:23:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
return data;
|
|
|
|
},
|
2021-05-08 13:32:45 +00:00
|
|
|
// There is no need to log successfully written operations as it pollutes the log with useless info for debugging.
|
2020-12-15 01:44:28 +00:00
|
|
|
write(header: string, data: object, asynchronous = true) {
|
|
|
|
this.open("data");
|
|
|
|
const path = `data/${header}.json`;
|
2020-10-15 09:23:24 +00:00
|
|
|
|
2021-03-30 08:58:21 +00:00
|
|
|
if (IS_DEV_MODE || header === "config") {
|
2020-12-15 01:44:28 +00:00
|
|
|
const result = JSON.stringify(data, null, "\t");
|
2020-07-25 08:15:26 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
if (asynchronous)
|
2021-05-08 13:32:45 +00:00
|
|
|
fs.writeFile(path, result, (error) => {
|
|
|
|
if (error) console.error("[storage.write]", error);
|
|
|
|
});
|
2020-12-15 01:44:28 +00:00
|
|
|
else fs.writeFileSync(path, result);
|
|
|
|
} else {
|
|
|
|
const result = JSON.stringify(data);
|
2020-10-15 09:23:24 +00:00
|
|
|
|
2021-05-08 13:32:45 +00:00
|
|
|
if (asynchronous)
|
|
|
|
fs.writeFile(path, result, (error) => {
|
|
|
|
if (error) console.error("[storage.write]", error);
|
|
|
|
});
|
2020-12-15 01:44:28 +00:00
|
|
|
else fs.writeFileSync(path, result);
|
|
|
|
}
|
|
|
|
},
|
2020-12-15 07:56:09 +00:00
|
|
|
open(path: string, filter?: (value: string, index: number, array: string[]) => unknown): string[] {
|
2020-12-15 01:44:28 +00:00
|
|
|
if (!fs.existsSync(path)) fs.mkdirSync(path);
|
2020-10-15 09:23:24 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
let directory = fs.readdirSync(path);
|
2020-10-15 09:23:24 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
if (filter) directory = directory.filter(filter);
|
2020-10-15 09:23:24 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
return directory;
|
|
|
|
},
|
|
|
|
close(path: string) {
|
|
|
|
if (fs.existsSync(path) && fs.readdirSync(path).length === 0)
|
2021-05-08 13:32:45 +00:00
|
|
|
fs.rmdir(path, (error) => {
|
|
|
|
if (error) console.error("[storage.close]", error);
|
|
|
|
});
|
2020-12-15 01:44:28 +00:00
|
|
|
}
|
2020-07-25 08:15:26 +00:00
|
|
|
};
|
|
|
|
|
2020-10-15 09:23:24 +00:00
|
|
|
export default Storage;
|