ffxiv-wiki-scripts/bin/one-time/hrothgar-viera-headwear

76 lines
2.6 KiB
JavaScript
Executable file

#!/usr/bin/env -S node --env-file-if-exists=.env
// Adds infobox parameters for Viera and Hrothgar hat compatibility based on
// data compiled by Crye. Expects there to be a `hats.csv` file in the repo root
// which is an export of the "HEAD" sheet of this Google Sheets document:
// https://docs.google.com/spreadsheets/d/13s_qxWThsensslulLfAxN0-hzQZVCqS9fKaImlLXbVU/edit?gid=38787585#gid=38787585
import {readFileSync} from 'node:fs';
import {join} from 'node:path';
import {getMediawikiClient} from '../../lib/config.js';
import {diff} from '../../lib/util/diff.js';
import {addParameterAtBottom, setExistingParameter} from '../../lib/util/template-parameters.js';
const mw = await getMediawikiClient();
// parse CSV, naively assume there are no quotes or backslashes (because i checked)
const data = readFileSync(join(import.meta.dirname, '../../hats.csv'), 'utf-8').split(/\r?\n/g).filter(l => l).map(line => line && line.split(','));
const headers = data.splice(0, 1)[0];
const COL_ITEM = headers.findIndex(field => field.toLowerCase() === 'item');
const COL_VIERA = headers.findIndex(field => field.toLowerCase().includes('viera'));
const COL_HROTH = headers.findIndex(field => field.toLowerCase().includes('hrothgar'));
const start = parseInt(process.env.START ?? '0', 10);
if (start) {
console.log('Starting from item index', start);
data.splice(0, start);
}
console.log('Processing', data.length, 'items');
for (const [i, line] of Object.entries(data)) {
const item = line[COL_ITEM];
const params = {
'display-on-viera': line[COL_VIERA].toLowerCase() === 'yes' ? 'y' : 'n',
'display-on-hrothgar': line[COL_HROTH].toLowerCase() === 'yes' ? 'y' : 'n',
}
console.log(+i + start, 'of', data.length + start, item, params);
// read and apply updates
const pageContent = await mw.readPage(item);
let newPageContent = pageContent;
try {
for (const [paramName, paramValue] of Object.entries(params)) {
newPageContent = setExistingParameter(newPageContent, paramName, paramValue)
?? addParameterAtBottom(newPageContent, paramName, paramValue);
if (!newPageContent) {
console.log(pageContent);
throw new Error('adding parameter to the page failed');
}
}
} catch (error) {
console.error(error);
console.log('skipping this item');
continue;
}
if (newPageContent === pageContent) {
// no changes
console.log('No changes, skipping');
continue;
}
diff(pageContent, newPageContent);
// write
try {
await mw.editPage(item, newPageContent, 'Add Hrothgar/Viera headwear data');
} catch (error) {
console.error('Failed to write:', error);
process.exit(1);
}
console.log('Written.');
}
console.log('done!');