-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
115 lines (94 loc) · 3.4 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { ensureDir, exists, type WalkEntry } from "@std/fs";
import { expandGlob } from "@std/fs/expand-glob";
import { copy } from "@std/fs/copy";
const baseFolder = "./THESPECTRUM";
const gamesFolder = "./Games";
const gamesFolderZip = "./Games.zip";
const romUrls = [
"https://github.com/rastersoft/fbzx/raw/refs/heads/master/data/spectrum-roms/128-0.rom",
"https://github.com/rastersoft/fbzx/raw/refs/heads/master/data/spectrum-roms/128-1.rom",
"https://github.com/rastersoft/fbzx/raw/refs/heads/master/data/spectrum-roms/48.rom",
"https://github.com/rastersoft/fbzx/raw/refs/heads/master/data/spectrum-roms/plus3-0.rom",
"https://github.com/rastersoft/fbzx/raw/refs/heads/master/data/spectrum-roms/plus3-1.rom",
"https://github.com/rastersoft/fbzx/raw/refs/heads/master/data/spectrum-roms/plus3-2.rom",
"https://github.com/rastersoft/fbzx/raw/refs/heads/master/data/spectrum-roms/plus3-3.rom",
];
const setup = async () => {
await ensureDir(`./${baseFolder}`);
};
const fetchRoms = async () => {
console.log("Fetching ROMS");
await ensureDir(`./${baseFolder}/roms`);
for (const romUrl of romUrls) {
const fileResponse = await fetch(romUrl);
if (fileResponse.body) {
const filename = romUrl.split("/").pop();
console.log(filename);
const file = await Deno.open(`${baseFolder}/roms/${filename}`, {
write: true,
create: true,
});
await fileResponse.body.pipeTo(file.writable);
}
}
};
const parseGames = async () => {
console.log("Parsing Games folder");
if (await exists(gamesFolder) === false) {
console.error("Games folder is not present");
throw ("No Games folder");
}
if (await exists(gamesFolder) === false && await exists(gamesFolderZip)) {
console.error("Games zip found, you need to unzip");
throw ("Games zip only found");
}
const alphabet = Array.from(
{ length: 26 },
(_, i) => String.fromCharCode(97 + i),
);
const numbers = Array.from(
{ length: 10 },
(_, i) => String.fromCharCode(48 + i),
);
const alphanumeric = [...numbers, ...alphabet];
function sliceIntoChunks(arr: WalkEntry[], chunkSize: number) {
const res = [];
for (let i = 0; i < arr.length; i += chunkSize) {
const chunk = arr.slice(i, i + chunkSize);
res.push(chunk);
}
return res;
}
for (const startingLetter of alphanumeric) {
console.log(`Processing Folder starting with "${startingLetter}"`);
const arr = await Array.fromAsync(
expandGlob(startingLetter + "*", {
root: gamesFolder,
caseInsensitive: true,
}),
);
const gamesStartingWith = arr.filter((g) => g.isDirectory === true);
const sliced = sliceIntoChunks(gamesStartingWith, 256);
console.log(
`Found ${gamesStartingWith.length} starting with "${startingLetter.toUpperCase()}": ${sliced.length} chunk(s)`,
);
let index = 0;
for (const slice of sliced) {
console.log(
`Letter "${startingLetter.toUpperCase()}", chunk ${index + 1}...`,
);
const dir = `${baseFolder}/${startingLetter.toUpperCase()}00${index}/`;
await ensureDir(dir);
await slice.forEach(async (game: WalkEntry) => {
if (game.isDirectory) {
const dirname = game.path.split("\\").pop();
await copy(game.path, `${dir}/${dirname}/`, { overwrite: true });
}
});
index++;
}
}
};
await setup();
await fetchRoms();
await parseGames();