-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (70 loc) · 2.71 KB
/
index.js
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
const path = require('path');
const fs = require('fs');
const fsp = require('fs').promises;
const filenamify = require('filenamify');
const DiscHeaderParser = require('./src/WiiGCNDiscHeaderParser').Parser;
const DiscHeaderReader = require('./src/WiiGCNDiscHeaderReader').Reader;
const { ParseWiiTDB } = require('./src/WiiTDBParser');
const WiiTDBDownloader = require('./src/WiiTDBDownloader').Downloader;
const myArgs = process.argv.slice(2);
console.log(myArgs);
const root = myArgs[0];
const gameFileRegex = /^game\.\w+$/;
const sortByImagePriority = (a, b) => {
if (gameFileRegex.test(a)) {
return -1;
}
if (gameFileRegex.test(b)) {
return 1;
}
return a.localeCompare(b);
};
const isDiscFile = (file) => {
const fileLc = file.toLowerCase();
return fileLc.endsWith('.iso') || fileLc.endsWith('.wbfs') || fileLc.endsWith('.gcm');
};
const normalizeDirectories = (wiiTDB) => {
fsp.readdir(root)
.then((directories) => {
directories.forEach((isodir) => {
const pathtoisodir = path.join(root, isodir);
fsp.readdir(pathtoisodir)
.then((isofiles) => {
const onlyIso = isofiles
.map((file) => file)
.filter((file) => isDiscFile(file))
.sort(sortByImagePriority);
if (!onlyIso.length) {
console.error(isodir);
return;
}
const isoName = onlyIso[0];
const pathToIso = path.join(pathtoisodir, isoName);
DiscHeaderReader(pathToIso)
.then((data) => {
const header = DiscHeaderParser(data);
const newTitle = `${filenamify(wiiTDB[header.titleId])} [${header.titleId}]`;
const newpathtoisodir = path.join(root, newTitle);
if (pathtoisodir !== newpathtoisodir) {
console.log('Renaming', {
original: pathtoisodir,
new: newpathtoisodir,
});
const result = fs.renameSync(pathtoisodir, newpathtoisodir);
console.log(result);
}
if (header.isGameCubeDisc && onlyIso.length === 1 && !gameFileRegex.test(isoName)) {
const extension = /\.\w+$/.exec(isoName)[0];
const newName = `game${extension}`;
console.log('renaming iso');
const updatedPathToIso = path.join(newpathtoisodir, isoName);
fs.renameSync(updatedPathToIso, path.join(newpathtoisodir, newName));
}
});
});
});
});
};
WiiTDBDownloader('db/wiitdb.xml')
.then((wiitdbPath) => ParseWiiTDB(wiitdbPath)
.then((wiiTDB) => normalizeDirectories(wiiTDB)));