fix: Fucking promises i need a beer

master
ogomez-at-wiris 2022-02-20 16:55:47 +01:00
parent 0beeb75cf0
commit dc187beb72
2 changed files with 28 additions and 25 deletions

View File

@ -49,4 +49,5 @@ PackService.getInitialList().then(() => {
httpServer.listen(PORT, () =>
console.log(`The server is running on port ${PORT}`)
);
console.log('initialized with ',PackService.getPackList().length)
});

View File

@ -1,47 +1,49 @@
import Pack from "../interfaces/pack";
import fs from "fs";
import * as fsWalk from "@nodelib/fs.walk";
import { resolve } from "path/posix";
const PACK_BASE_PATH = "packs";
export default class PackService {
private static packList: [Pack];
private static packList: Pack[] = [];
public static getPackList = (): Array<Pack> => this.packList;
public static getInitialList = async () => {
const packs = await fs.readdirSync(PACK_BASE_PATH);
public static getInitialList = async (): Promise<void> => {
let packs = await fs.readdirSync(PACK_BASE_PATH);
PackService.packList = await Promise.all(packs.map((pack) => PackService.getPackInfo(pack)))
};
public static getPackInfo = async(packName: string): Promise<Pack> => {
const name = packName;
let size = 0;
let bpm: string = "";
// Setup walk
const noDots = (entry: fsWalk.Entry) => !entry.path.startsWith(".");
packs.forEach(async (pack) => {
const name = pack;
let size = 0;
let bpm: string = "";
const packFiles = await fsWalk.walkSync(`${PACK_BASE_PATH}/${pack}`, {
entryFilter: noDots,
concurrency: 4,
});
packFiles.forEach(async (file) => {
const packFiles = await fsWalk.walkSync(`${PACK_BASE_PATH}/${packName}`, {
entryFilter: noDots,
concurrency: 4,
});
const promises = await Promise.all(
packFiles.map(async (file) => {
size += await fs.statSync(file.path).size;
if (file.name === "bpm.txt") {
bpm = await fs.readFileSync(file.path).toString();
}
});
const currentPack: Pack = {
name: name,
size: size,
bpm: bpm,
};
console.log(currentPack);
if (currentPack.bpm !== '') {
PackService.packList.push(currentPack);
} else {
console.log('found corrupted pack', currentPack.name)
}
});
})
);
const currentPack: Pack = {
name: name,
size: size,
bpm: bpm,
};
return currentPack;
};
}