-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv-to-json.js
45 lines (42 loc) · 1.62 KB
/
csv-to-json.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
const fs = require("fs");
const parser = require("csv-parse/sync");
const process = require("process");
const yargs = require("yargs");
const args = yargs(process.argv.slice(2))
.command(
"$0 <filename>",
"Convert a module art CSV file to JSON",
() => {
yargs
.positional("filename", {
describe: "A CSV filename. The first column of the CSV is ignored. Columns 2-6 should be compendium ID, actor ID, actor image path, token image path, and an optional scale-ratio antecedent (consequent of 1), and an optional boolean indicating whether random images are to be enabled.",
});
}
)
.usage("Usage: node $0 <filename>")
.check((args) => typeof args.filename === "string" &&
fs.existsSync(args.filename) &&
fs.statSync(args.filename).isFile())
.help(false)
.version(false)
.parseSync();
const csvData = fs.readFileSync(args.filename, { encoding: "utf-8" });
const jsonData = parser
.parse(csvData)
.slice(1)
.map((row) => ({
pack: row[1],
id: row[2],
actor: row[3],
token:
row[5].trim() || row[6]
? { img: row[4], scale: Number(row[5]) || undefined, randomImg: !!row[6] || undefined }
: row[4],
randomImg: !!row[6],
}))
.reduce((accum, row) => {
accum[row.pack] ??= {};
accum[row.pack][row.id] = { actor: row.actor, token: row.token };
return accum;
}, {});
fs.writeFileSync(args.filename.replace(/\.csv$/, ".json"), JSON.stringify(jsonData, null, 2), { encoding: "utf-8" });