import { readdir, readFile } from "fs/promises"; import { join } from "path"; import * as yaml from "js-yaml"; import { plainToClass } from "class-transformer"; import { validate } from "class-validator"; import { init, Page } from "./db"; import { Page as PageConfig } from "./page"; // TODO: configure this thru cmdline or something later let materials_dir = "../material"; let db_file = "test.db"; let db; async function loadPageIntoDb(name: string): Promise { // check if this slug has already been loaded into the database let path = join(materials_dir, name); let rawData = await readFile(path, { encoding: "utf8" }); let parsedData = yaml.load(rawData); let page_config = plainToClass(PageConfig, parsedData); await validate(page_config); console.log("Validated!", page_config); let page = new Page({ slug: page_config.slug, title: page_config.title, }); await page.save(); } async function main() { db = await init(db_file); // TODO: streaming version of readdir when the folder gets big let names = await readdir(materials_dir); await Promise.all(names .filter(name => name.toLowerCase().endsWith(".yml")) .map(loadPageIntoDb) ); } main();