-
Notifications
You must be signed in to change notification settings - Fork 129
/
generate.js
141 lines (131 loc) · 4.02 KB
/
generate.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#! /usr/bin/env node
console.log('generating static content files');
const jsonfile = require('jsonfile');
const slugify = require('slugify');
const fs = require('fs');
const mkdirp = require('mkdirp');
var getDirName = require('path').dirname;
var spellClassArrays = require('./spellClassArrays.js');
function cleanName(str) {
const cleaned = slugify(str.toLowerCase(), { remove: /[*+~.()"!:@/]/g });
return cleaned;
}
function writeFile(path, contents, cb) {
mkdirp(getDirName(path), function (err) {
if (err) {
return cb(err);
}
jsonfile.writeFile(path, contents, cb);
});
}
function fileParser(input, name, listName) {
jsonfile.readFile(input, function (err, obj) {
if (err) {
console.error(err);
}
writeFile(
`${__dirname}/../static/json/${listName}.json`,
obj,
function (err) {
if (err) {
console.log(err);
} else {
console.log(`wrote ${name} list`);
}
}
);
console.log(`writing individual ${name} files:`);
for (item in obj) {
if (name === 'spell') {
obj[item] = spellClassArrays(obj[item]);
}
const itemJSON = obj[item];
const itemName = cleanName(itemJSON.name);
const filename = `${__dirname}/../static/json/${listName}/${itemName}.json`;
writeFile(filename, itemJSON, function (err) {
if (err) {
console.log(err);
}
});
}
});
}
function indexJson(files, listName) {
let list = [];
const path = `${__dirname}/../static/json/${listName}.json`;
for (file in files) {
json = jsonfile.readFileSync(files[file]);
for (item in json) {
const thisItem = json[item];
const slug = slugify(thisItem.name.toLowerCase());
if (listName === 'spell-index') {
list.push({
name: thisItem.name,
slug: slug,
dnd_class: thisItem.class,
school: thisItem.school,
});
} else if (listName === 'monster-index') {
list.push({
name: thisItem.name,
slug: slug,
type: thisItem.type,
challenge_rating: thisItem.challenge_rating,
size: thisItem.size,
hit_points: thisItem.hit_points,
});
} else {
list.push({ name: thisItem.name, slug: slug });
}
}
}
writeFile(path, list, function (err) {
if (err) {
console.log(err);
} else {
console.log(`wrote ${listName}`);
}
});
}
function indexWithParentJson(files, listName) {
let list = [];
const path = `${__dirname}/../static/json/${listName}.json`;
for (file in files) {
json = jsonfile.readFileSync(files[file]);
for (item in json) {
const thisItem = json[item];
const slug = slugify(thisItem.name.toLowerCase());
list.push({ name: thisItem.name, slug: slug, parent: thisItem.parent });
}
}
writeFile(path, list, function (err) {
if (err) {
console.log(err);
} else {
console.log(`wrote ${listName}`);
}
});
}
const monsterfile = 'data/WOTC_5e_SRD_v5.1/monsters.json';
const spellfile = 'data/WOTC_5e_SRD_v5.1/spells.json';
const classfile = 'data/WOTC_5e_SRD_v5.1/classes.json';
const itemfile = 'data/WOTC_5e_SRD_v5.1/magicitems.json';
const sectionfile = 'data/WOTC_5e_SRD_v5.1/sections.json';
const racefile = 'data/WOTC_5e_SRD_v5.1/races.json';
const planefile = 'data/WOTC_5e_SRD_v5.1/planes.json';
// make static files from database-populating json
fileParser(monsterfile, 'monster', 'monsters');
fileParser(spellfile, 'spell', 'spells');
fileParser(classfile, 'class', 'classes');
fileParser(itemfile, 'magicitem', 'magicitems');
fileParser(racefile, 'race', 'races');
fileParser(planefile, 'plane', 'planes');
fileParser(sectionfile, 'section', 'sections');
// make indexes for static files
indexJson([monsterfile], 'monster-index');
indexJson([spellfile], 'spell-index');
indexJson([classfile], 'class-index');
indexJson([itemfile], 'item-index');
indexJson([racefile], 'race-index');
indexJson([planefile], 'plane-index');
indexWithParentJson([sectionfile], 'section-index');