forked from lbrande/games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateIndex.js
58 lines (46 loc) · 1.22 KB
/
generateIndex.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
#!/usr/bin/env node
const Handlebars = require("handlebars");
const fs = require("fs");
const glob = require("glob");
const map = require("ramda/src/map");
const path = require("path");
let files = glob.sync("**/*.json", { cwd: "./games" });
let public_games = [];
// Create proper data for template
let games = map((file) => {
let dir = path.dirname(file);
let group = dir === "." ? undefined : dir;
let id = path.basename(file, ".json");
let slug = encodeURIComponent(id);
// Load json in order to get more data
let data = require(`./games/${file}`);
let { title, subtitle, designer, publisher } = data.info;
let minPlayers = data.players ? data.players[0].number : 0;
let maxPlayers = data.players
? data.players[data.players.length - 1].number
: 0;
let game = {
id,
slug,
file,
title,
subtitle,
designer,
publisher,
minPlayers,
maxPlayers,
};
if (group) {
game.group = group;
if (group === "public") {
public_games.push(game);
}
}
return game;
}, files);
const template = Handlebars.compile(
fs.readFileSync("./index.js.hb", { encoding: "UTF8" })
);
fs.writeFileSync("./index.js", template({ games, public_games }), {
mode: "644",
});