-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
76 lines (62 loc) · 1.56 KB
/
index.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
import { readFileSync, writeFileSync } from "fs"
import { join, parse } from "path"
import { Builder, parseString } from "xml2js"
type Rom = {
$: {
name: string
size: string
crc: string
md5: string
sha1: string
}
}
type Game = {
$: { name: string }
category: [string]
description: [string]
rom: Rom[]
}
type Datfile = {
datafile: {
header: unknown
game: Game[]
}
}
if (process.argv.length !== 3) {
throw new Error("A datfile must be provided as an argument")
}
const filename = process.argv[2]
const xml = readFileSync(filename, { encoding: "utf8" })
const combinedGames: Game[] = []
parseString(xml, (err, dat: Datfile) => {
if (err) throw err
dat.datafile.game.map((game) => {
const { name } = game.$
if (name.match(/ \(Disc \d\)/)) {
const combinedName = name.replace(/ \(Disc \d\)/, "")
const existingGame = combinedGames.find(
(combinedGame) => combinedGame.$.name === combinedName
)
if (existingGame) {
existingGame.rom.push(...game.rom)
} else {
game.$.name = combinedName
game.description = [combinedName]
combinedGames.push(game)
}
} else {
combinedGames.push(game)
}
})
const newDatfile: Datfile = {
datafile: {
header: dat.datafile.header,
game: combinedGames,
},
}
const builder = new Builder()
const newXml = builder.buildObject(newDatfile)
const { dir, ext, name } = parse(filename)
const outputFilename = join(dir, `${name}-combined${ext}`)
writeFileSync(outputFilename, newXml)
})