-
Notifications
You must be signed in to change notification settings - Fork 0
/
_switch-backup-rename.js
63 lines (57 loc) · 1.77 KB
/
_switch-backup-rename.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
const fs = require('fs-extra');
const accents = require('remove-accents');
const { getGamesInfos } = require('./utils/fetch-games-data');
const {
getCurrentGameInfo,
sanitizeSerial,
cleanExtension,
cleanFileName
} = require('./utils/filter-helpers');
const { cwd } = require('./utils/system-helpers');
const outFormat = process.argv[2] || '{name}-{short-serial}.{ext}';
let done = false;
getGamesInfos()
.then(gamesInfos => {
fs.readdirSync(cwd()).forEach(file => {
if (file.endsWith('.xci') || file.endsWith('.nsp')) {
const originalExtension = file.substr(file.length - 3);
const originalName = cleanExtension(file);
const currentGameInfo = getCurrentGameInfo(
originalName,
gamesInfos.datas
);
if (currentGameInfo) {
const finalName = outFormat
.replace('{base}', originalName.trim())
.replace('{name}', currentGameInfo.name.trim())
.replace('{short-serial}', sanitizeSerial(currentGameInfo.serial))
.replace('{serial}', currentGameInfo.serial.toLowerCase().trim())
.replace('{ext}', originalExtension);
try {
fs.renameSync(
cwd(file),
cwd(cleanFileName(accents.remove(finalName)))
);
done = true;
} catch (error) {
// continue
}
} else {
console.log(
`- game infos not found in database yet for ${originalName}`
);
}
}
});
if (done) {
console.log('Game Files renamed');
} else {
console.log('Nothing to do');
}
})
.catch(error => {
console.log('something went wrong');
console.log(error);
});
// prevent windows closing
setInterval(function() {}, 3000);