2017-06-03 01:36:17 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const fsextra = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
const util = require('util');
|
|
|
|
const logger = require('winston');
|
2017-06-01 22:00:37 +00:00
|
|
|
|
2017-06-03 01:36:17 +00:00
|
|
|
const sanitizeHtml = require('sanitize-html');
|
2017-06-01 22:00:37 +00:00
|
|
|
|
2017-06-03 01:36:17 +00:00
|
|
|
const blackfriday = require('./blackfriday.js');
|
2017-06-01 22:00:37 +00:00
|
|
|
|
2017-06-03 01:36:17 +00:00
|
|
|
const del = require('delete');
|
|
|
|
const exec = require('sync-exec');
|
|
|
|
|
|
|
|
const inputDirectoryGame = './citra-games-wiki';
|
|
|
|
const inputDirectoryWiki = './citra-games-wiki.wiki';
|
|
|
|
const outputDirectoryMd = '../../site/content/game';
|
2017-06-03 01:49:26 +00:00
|
|
|
const outputDirectoryBoxart = '../../site/static/images/game/boxart';
|
2017-06-03 04:42:54 +00:00
|
|
|
const outputDirectoryIcons = '../../site/static/images/game/icons';
|
2017-06-03 20:10:29 +00:00
|
|
|
const outputDirectoryScreenshots = '../../site/static/images/screenshots0';
|
|
|
|
const outputDirectorySavefiles = '../../site/static/savefiles/';
|
2017-06-01 22:00:37 +00:00
|
|
|
|
|
|
|
// The URL
|
|
|
|
function url(title) {
|
|
|
|
return '/wiki/' + title.replace(/\s+/g, '-').toLowerCase();
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:36:17 +00:00
|
|
|
function gitPull(directory, repository) {
|
|
|
|
if (fs.existsSync(directory)) {
|
|
|
|
logger.info(`Fetching latest from Github : ${directory}`);
|
2017-06-05 20:03:41 +00:00
|
|
|
exec(`cd ${directory} && git pull && cd -`);
|
2017-06-03 01:36:17 +00:00
|
|
|
} else {
|
|
|
|
logger.info(`Cloning repository from Github : ${directory}`);
|
|
|
|
exec(`git clone ${repository}`);
|
|
|
|
}
|
2017-06-01 22:00:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 01:36:17 +00:00
|
|
|
function getDirectories (srcpath) {
|
|
|
|
return fs.readdirSync(srcpath)
|
|
|
|
.filter(file => fs.lstatSync(path.join(srcpath, file)).isDirectory())
|
2017-06-01 22:00:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 20:03:41 +00:00
|
|
|
String.prototype.trimNewline = function() {
|
|
|
|
let string = this.toString();
|
|
|
|
let match = '\r\n'
|
|
|
|
if (string.endsWith('\r\n')) {
|
|
|
|
return string.slice(0, -4);
|
|
|
|
} else if (string.endsWith('\r') || string.endsWith('\n')) {
|
|
|
|
return string.slice(0, -2);
|
|
|
|
} else {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:36:17 +00:00
|
|
|
// Fetch game information stored in Github repository.
|
2017-06-03 01:56:20 +00:00
|
|
|
gitPull(inputDirectoryGame, 'https://github.com/citra-emu/citra-games-wiki.git');
|
2017-06-03 01:36:17 +00:00
|
|
|
|
|
|
|
// Fetch game articles stored in Github wiki.
|
2017-06-03 01:56:20 +00:00
|
|
|
gitPull(inputDirectoryWiki, 'https://github.com/citra-emu/citra-games-wiki.wiki.git');
|
2017-06-01 22:00:37 +00:00
|
|
|
|
2017-06-03 01:36:17 +00:00
|
|
|
// Make sure the output directories in Hugo exist.
|
2017-06-01 22:00:37 +00:00
|
|
|
if (fs.existsSync(outputDirectoryMd) == false) {
|
|
|
|
logger.info(`Creating missing output directory: ${outputDirectoryMd}`);
|
|
|
|
fs.mkdirSync(outputDirectoryMd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.existsSync(outputDirectoryBoxart) == false) {
|
|
|
|
logger.info(`Creating missing output directory: ${outputDirectoryBoxart}`);
|
|
|
|
fs.mkdirSync(outputDirectoryBoxart);
|
|
|
|
}
|
|
|
|
|
2017-06-03 04:42:54 +00:00
|
|
|
if (fs.existsSync(outputDirectoryIcons) == false) {
|
|
|
|
logger.info(`Creating missing output directory: ${outputDirectoryIcons}`);
|
|
|
|
fs.mkdirSync(outputDirectoryIcons);
|
|
|
|
}
|
|
|
|
|
2017-06-03 20:10:29 +00:00
|
|
|
if (fs.existsSync(outputDirectorySavefiles) == false) {
|
|
|
|
logger.info(`Creating missing output directory: ${outputDirectorySavefiles}`);
|
|
|
|
fs.mkdirSync(outputDirectorySavefiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.existsSync(outputDirectoryScreenshots) == false) {
|
|
|
|
logger.info(`Creating missing output directory: ${outputDirectoryScreenshots}`);
|
|
|
|
fs.mkdirSync(outputDirectoryScreenshots);
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:36:17 +00:00
|
|
|
try {
|
|
|
|
// Loop through each game folder.
|
|
|
|
getDirectories(inputDirectoryGame).forEach(function(game) {
|
2017-06-05 01:27:57 +00:00
|
|
|
try {
|
|
|
|
if (game == '.git') { return; }
|
2017-06-03 01:36:17 +00:00
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
logger.info(`Creating Hugo files for ${game}`);
|
2017-06-03 01:36:17 +00:00
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
// Copy the boxart for the game.
|
|
|
|
let boxartPath = `${inputDirectoryGame}/${game}/boxart.png`;
|
|
|
|
if (fs.existsSync(boxartPath)) {
|
|
|
|
fsextra.copySync(boxartPath, `${outputDirectoryBoxart}/${game}.png`);
|
|
|
|
}
|
2017-06-03 04:49:49 +00:00
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
// Copy the icon for the game.
|
|
|
|
let iconPath = `${inputDirectoryGame}/${game}/icon.png`;
|
|
|
|
if (fs.existsSync(iconPath)) {
|
|
|
|
fsextra.copySync(iconPath, `${outputDirectoryIcons}/${game}.png`);
|
|
|
|
}
|
2017-06-03 20:10:29 +00:00
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
// Copy the savefiles for the game.
|
|
|
|
let inputDirectorySavefilesGame = `${inputDirectoryGame}/${game}/savefiles/`;
|
|
|
|
let outputDirectorySavefilesGame = `${outputDirectorySavefiles}/${game}/`;
|
|
|
|
let savefileMetadataContents = [];
|
2017-06-03 20:10:29 +00:00
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
if (fs.existsSync(inputDirectorySavefilesGame)) {
|
|
|
|
// Create the savefile directory for each game.
|
|
|
|
if (fs.existsSync(outputDirectorySavefilesGame) == false) {
|
|
|
|
fs.mkdirSync(outputDirectorySavefilesGame);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy all savefiles into the output folder and store their contents.
|
|
|
|
fs.readdirSync(inputDirectorySavefilesGame).forEach(file => {
|
|
|
|
if (path.extname(file) == '.zip') {
|
|
|
|
fsextra.copySync(`${inputDirectorySavefilesGame}/${file}`, `${outputDirectorySavefilesGame}/${file.replace('.zip', '.zip')}`);
|
|
|
|
} else if (path.extname(file) == '.dat') {
|
|
|
|
// Store the contents of the file in memory for adding it into the markdown later.
|
2017-06-05 20:03:41 +00:00
|
|
|
savefileMetadataContents.push({
|
|
|
|
filename: file.replace('.dat', '.zip'),
|
|
|
|
contents: fs.readFileSync(`${inputDirectorySavefilesGame}/${file}`, 'utf8').trimNewline()
|
|
|
|
});
|
2017-06-05 01:27:57 +00:00
|
|
|
}
|
|
|
|
});
|
2017-06-03 20:10:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
// Copy the screenshots for the game.
|
|
|
|
let inputDirectoryScreenshotsGame = `${inputDirectoryGame}/${game}/screenshots/`;
|
|
|
|
let outputDirectoryScreenshotsGame = `${outputDirectoryScreenshots}/${game}/`;
|
2017-06-03 20:10:29 +00:00
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
if (fs.existsSync(inputDirectoryScreenshotsGame)) {
|
|
|
|
// Create the savefile directory for each game.
|
|
|
|
if (fs.existsSync(outputDirectoryScreenshotsGame) == false) {
|
|
|
|
fs.mkdirSync(outputDirectoryScreenshotsGame);
|
|
|
|
}
|
2017-06-03 20:10:29 +00:00
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
// Copy all screenshots into the output folder.
|
|
|
|
fs.readdirSync(inputDirectoryScreenshotsGame).forEach(file => {
|
|
|
|
if (path.extname(file) == '.png') {
|
|
|
|
fsextra.copySync(`${inputDirectoryScreenshotsGame}/${file}`, `${outputDirectoryScreenshotsGame}/${file}`);
|
|
|
|
}
|
|
|
|
});
|
2017-06-03 20:10:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
// Create the markdown file to be displayed in Hugo.
|
|
|
|
let title = game.replace(/-/g, ' ').slice(0, -3);
|
|
|
|
var stats = fs.statSync(`${inputDirectoryGame}/${game}/game.dat`);
|
|
|
|
let modified = new Date(util.inspect(stats.mtime));
|
2017-06-03 20:10:29 +00:00
|
|
|
|
2017-06-05 20:03:41 +00:00
|
|
|
let datContents = fs.readFileSync(`${inputDirectoryGame}/${game}/game.dat`, 'utf8').trimNewline();
|
2017-06-03 01:36:17 +00:00
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
var wikiContents = "";
|
|
|
|
let wikiPathGame = `${inputDirectoryWiki}/${game}.md`;
|
|
|
|
if (fs.existsSync(wikiPathGame)) {
|
2017-06-05 20:03:41 +00:00
|
|
|
wikiContents = fs.readFileSync(wikiPathGame, 'utf8').trimNewline();
|
2017-06-05 01:27:57 +00:00
|
|
|
} else {
|
2017-06-05 20:03:41 +00:00
|
|
|
wikiContents = "## No wiki exists yet for this game.";
|
2017-06-05 01:27:57 +00:00
|
|
|
}
|
2017-06-03 01:36:17 +00:00
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
// Fix Blackfriday markdown rendering differences.
|
|
|
|
wikiContents = blackfriday.fixLists(wikiContents);
|
|
|
|
wikiContents = blackfriday.fixLinks(wikiContents);
|
2017-06-03 01:36:17 +00:00
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
// Read all savefiles from array and copy them into the markdown.
|
|
|
|
savefileMetadataContents.forEach(function(savefile) {
|
|
|
|
let modified = new Date(util.inspect(stats.mtime));
|
2017-06-05 20:03:41 +00:00
|
|
|
datContents += `\r\n\r\n[[ savefiles ]]\r\nfilename = "${savefile.filename}"\r\ndate = "${modified.toISOString()}"\r\n${savefile.contents}`;
|
2017-06-05 01:27:57 +00:00
|
|
|
});
|
2017-06-03 20:10:29 +00:00
|
|
|
|
2017-06-05 01:27:57 +00:00
|
|
|
let output = `+++\r\ndate = "${modified.toISOString()}"\r\n${datContents}\r\n+++\r\n\r\n${wikiContents}\r\n`;
|
|
|
|
fs.writeFileSync(`${outputDirectoryMd}/${game}.md`, output);
|
|
|
|
} catch (ex) {
|
|
|
|
logger.error(`${game} failed to generate: ${ex}`);
|
2017-06-05 20:03:41 +00:00
|
|
|
logger.error(ex);
|
2017-06-05 01:27:57 +00:00
|
|
|
}
|
2017-06-03 01:36:17 +00:00
|
|
|
});
|
|
|
|
} catch (ex) {
|
|
|
|
logger.error(ex);
|
|
|
|
}
|