PronounsPage/helpers/convertNounTemplates.ts

34 lines
1.2 KiB
TypeScript

import fs from 'node:fs/promises';
import Suml from 'suml';
import locales from '~~/locale/locales.ts';
import { loadTsv } from '~~/server/loader.ts';
for (const locale of locales) {
const path = `locale/${locale.code}/nouns/nounTemplates.tsv`;
if (!(await fs.stat(path)).isFile) {
continue;
}
const templates = Object.fromEntries((await loadTsv<Record<string, string>>(path))
.map((endings) => {
const singular: Record<string, string[]> = {};
const plural: Record<string, string[]> = {};
for (const k in endings) {
if (!Object.hasOwn(endings, k)) {
continue;
}
const value = endings[k] ? endings[k].split('/') : [];
if (k.endsWith('_pl')) {
plural[k.substring(0, k.length - 3).toLowerCase()] = value;
} else {
singular[k.toLowerCase()] = value;
}
}
return [singular.m, { singular, plural }];
}));
const suml = new Suml();
await fs.appendFile(`locale/${locale.code}/nouns/nounsData.suml`, suml.dump(templates));
}