forked from simalexan/battlecamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-cards.js
43 lines (36 loc) · 1.3 KB
/
generate-cards.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
const util = require('util');
const fs = require('fs');
const CARDS_DATA = 'src/cards.json';
const CARD_TEMPLATE = 'src/card-template.svg',
OUTPUT = 'cards';
const readFile = util.promisify(fs.readFile),
writeFile = util.promisify(fs.writeFile),
rename = util.promisify(fs.rename);
async function readCards() {
const cardsResponse = await readFile(CARDS_DATA, 'utf8');
return JSON.parse(cardsResponse);
}
async function replaceCardData(fileContents, args, card) {
let replacedContents = fileContents;
args.forEach(argumentName => {
const argumentSearch = `{{${argumentName}}}`;
replacedContents = replacedContents.replace(new RegExp(argumentSearch, 'g'), card[argumentName]);
});
return replacedContents;
}
async function generateCard(card, template, index, output){
const fileName = `${output}/card-${index}.svg`;
let cardContents = await replaceCardData(template, Object.keys(card), card);
let tmpfile = `${fileName}.jstmpreplace`;
await writeFile(tmpfile, cardContents, 'utf8');
await rename(tmpfile, fileName);
}
async function generateCards(){
const cards = await readCards();
const template = await readFile(CARD_TEMPLATE, 'utf8');
cards.forEach(async (card, index) =>{
return await generateCard(card, template, index+1, OUTPUT)
});
return true;
};
generateCards();