gctGenerator/scripts/inject_codes.js

74 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-07-01 14:26:48 +09:00
const fs = require('fs');
const path = require('path');
2020-07-01 15:14:09 +09:00
const { JSDOM } = require('jsdom');
2020-07-01 13:55:31 +09:00
2020-07-01 14:26:48 +09:00
const jsonFilePath = path.join(__dirname, '../site/.vuepress/data/gameVersions.json');
2020-07-01 13:55:31 +09:00
const codeVersions = ['GMSE01', 'GMSJ01', 'GMSP01', 'GMSJ0A'];
2020-07-01 14:44:30 +09:00
const injectionTag = '<!-- injectionpoint -->';
2020-07-01 13:55:31 +09:00
2020-07-01 15:14:09 +09:00
// Converts the XML source to a JSON object
2020-07-01 14:44:30 +09:00
const parseXml = (xmlString) => {
2020-07-01 15:14:09 +09:00
const codeCollection = new JSDOM(xmlString, {
contentType: 'text/xml',
}).window.document.getElementsByTagName('code');
const parseTextNode = (node, identifier) => node.getElementsByTagName(identifier)[0].textContent;
2020-06-28 06:33:20 +09:00
const codes = [...codeCollection];
2020-07-01 14:44:30 +09:00
return codes.map((code) => ({
2020-06-29 00:35:18 +09:00
author: parseTextNode(code, 'author'),
title: parseTextNode(code, 'title'),
description: parseTextNode(code, 'description'),
version: parseTextNode(code, 'version'),
date: parseTextNode(code, 'date'),
source: parseTextNode(code, 'source').replace(/[\s\n\r\t]+/gm, ''),
2020-06-28 06:33:20 +09:00
}));
};
2020-07-01 15:14:09 +09:00
// Read the current code list
2020-07-01 14:44:30 +09:00
const codeJson = JSON.parse(fs.readFileSync(jsonFilePath));
2020-07-01 13:55:31 +09:00
2020-07-01 15:14:09 +09:00
// Populate all code fields in the codeJSON
2020-07-01 13:55:31 +09:00
for (let i = 0; i < codeVersions.length; i++) {
2020-07-01 14:26:48 +09:00
const xml = fs.readFileSync(path.join(__dirname, `../codes/${codeVersions[i]}.xml`));
2020-07-01 14:44:30 +09:00
codeJson.find((c) => c.identifier === codeVersions[i]).codes = parseXml(xml);
2020-07-01 13:55:31 +09:00
}
2020-07-01 15:14:09 +09:00
// Save the codeJSON with the updated codes
2020-07-01 14:44:30 +09:00
fs.writeFileSync(jsonFilePath, JSON.stringify(codeJson));
2020-07-01 15:14:09 +09:00
// Populate the code reference
2020-07-01 14:44:30 +09:00
for (let i = 0; i < codeVersions.length; i++) {
2020-07-01 15:14:09 +09:00
const filePath = path.join(
__dirname,
`../site/code-reference/${codeVersions[i].toLowerCase()}.md`,
2020-07-01 14:44:30 +09:00
);
2020-07-01 15:14:09 +09:00
// Get the current reference
const reference = fs.readFileSync(filePath).toString();
2020-07-01 14:44:30 +09:00
if (!reference.includes(injectionTag)) {
throw new Error(`No injection tag found in ${codeVersions[i].toLowerCase()}.md`);
}
2020-07-01 15:14:09 +09:00
// Everything afte rthe injection tag is deleted from the file
let fileContent = reference.split(injectionTag)[0] + injectionTag;
const codes = codeJson
.find((c) => c.identifier === codeVersions[i])
.codes.sort((a, b) => (a.title > b.title ? 1 : -1));
2020-07-01 14:44:30 +09:00
2020-07-01 15:14:09 +09:00
// Create a semi-markdown version for all codes
2020-07-01 14:44:30 +09:00
codes.forEach((code) => {
2020-07-01 15:14:09 +09:00
const title = `### ${code.title}`;
const author = `*${code.author.includes(',') ? 'Authors:' : 'Author:'} ${code.author}*`;
const version = `*Version: ${code.version} (${code.date})*`;
2020-07-01 14:44:30 +09:00
const description = code.description;
2020-07-01 15:14:09 +09:00
fileContent += `\n\n${title.trim()}\n\n${version.trim()} \n${author.trim()}\n\n${description.trim()}`;
2020-07-01 14:44:30 +09:00
});
2020-07-01 15:14:09 +09:00
// Save the reference file
fs.writeFileSync(filePath, fileContent);
2020-07-01 14:44:30 +09:00
}