fix: Fucking promises i need a beer
parent
0beeb75cf0
commit
dc187beb72
|
@ -49,4 +49,5 @@ PackService.getInitialList().then(() => {
|
||||||
httpServer.listen(PORT, () =>
|
httpServer.listen(PORT, () =>
|
||||||
console.log(`The server is running on port ${PORT}`)
|
console.log(`The server is running on port ${PORT}`)
|
||||||
);
|
);
|
||||||
|
console.log('initialized with ',PackService.getPackList().length)
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,47 +1,49 @@
|
||||||
import Pack from "../interfaces/pack";
|
import Pack from "../interfaces/pack";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import * as fsWalk from "@nodelib/fs.walk";
|
import * as fsWalk from "@nodelib/fs.walk";
|
||||||
|
import { resolve } from "path/posix";
|
||||||
|
|
||||||
const PACK_BASE_PATH = "packs";
|
const PACK_BASE_PATH = "packs";
|
||||||
|
|
||||||
export default class PackService {
|
export default class PackService {
|
||||||
private static packList: [Pack];
|
private static packList: Pack[] = [];
|
||||||
|
|
||||||
public static getPackList = (): Array<Pack> => this.packList;
|
public static getPackList = (): Array<Pack> => this.packList;
|
||||||
|
|
||||||
public static getInitialList = async () => {
|
public static getInitialList = async (): Promise<void> => {
|
||||||
const packs = await fs.readdirSync(PACK_BASE_PATH);
|
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
|
// Setup walk
|
||||||
const noDots = (entry: fsWalk.Entry) => !entry.path.startsWith(".");
|
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;
|
size += await fs.statSync(file.path).size;
|
||||||
if (file.name === "bpm.txt") {
|
if (file.name === "bpm.txt") {
|
||||||
bpm = await fs.readFileSync(file.path).toString();
|
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;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue