mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-11 00:38:28 +03:00
factor out async in reset-db-state
This commit is contained in:
@@ -7,21 +7,29 @@ import path from "path";
|
||||
|
||||
const DATA_DIR = path.join(__dirname, "./mock-db");
|
||||
|
||||
async function ResetState(file: string) {
|
||||
let filename = path.basename(file, ".json");
|
||||
|
||||
await db.get(filename).remove({});
|
||||
|
||||
let fileLoc = path.join(DATA_DIR, file);
|
||||
let data = JSON.parse(fs.readFileSync(fileLoc, "utf-8"));
|
||||
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error(`Panic, ${filename} not JSONArray?`);
|
||||
}
|
||||
|
||||
await db.get(filename).insert(data);
|
||||
}
|
||||
|
||||
export default async function ResetDBState() {
|
||||
const files = fs.readdirSync(DATA_DIR);
|
||||
|
||||
const promises = [];
|
||||
|
||||
for (const file of files) {
|
||||
let filename = path.basename(file, ".json");
|
||||
|
||||
await db.get(filename).remove({});
|
||||
|
||||
let fileLoc = path.join(DATA_DIR, file);
|
||||
let data = JSON.parse(fs.readFileSync(fileLoc).toString());
|
||||
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error(`Panic, ${filename} not JSONArray?`);
|
||||
}
|
||||
|
||||
await db.get(filename).insert(data);
|
||||
promises.push(ResetState(file));
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user