2021-08-28 10:52:47 +00:00
|
|
|
import { readdir, readFile } from "fs/promises";
|
|
|
|
import { join } from "path";
|
|
|
|
import * as yaml from "js-yaml";
|
2021-08-28 18:03:38 +00:00
|
|
|
import { plainToClass } from "class-transformer";
|
|
|
|
import { validate } from "class-validator";
|
2021-08-28 10:52:47 +00:00
|
|
|
|
2021-08-28 18:03:38 +00:00
|
|
|
import { init, Page } from "./db";
|
|
|
|
import { Page as PageConfig } from "./page";
|
2021-08-28 10:52:47 +00:00
|
|
|
|
2021-08-28 18:03:38 +00:00
|
|
|
// 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<void> {
|
|
|
|
// 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);
|
2021-08-28 10:52:47 +00:00
|
|
|
|
2021-08-28 18:03:38 +00:00
|
|
|
let page = new Page({
|
|
|
|
slug: page_config.slug,
|
|
|
|
title: page_config.title,
|
|
|
|
});
|
|
|
|
await page.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
db = await init(db_file);
|
2021-08-28 10:52:47 +00:00
|
|
|
|
2021-08-28 18:03:38 +00:00
|
|
|
// TODO: streaming version of readdir when the folder gets big
|
2021-08-28 10:52:47 +00:00
|
|
|
let names = await readdir(materials_dir);
|
|
|
|
|
|
|
|
await Promise.all(names
|
|
|
|
.filter(name => name.toLowerCase().endsWith(".yml"))
|
2021-08-28 18:03:38 +00:00
|
|
|
.map(loadPageIntoDb)
|
2021-08-28 10:52:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|