load
This commit is contained in:
parent
74660120bb
commit
9d7e8e4511
2 changed files with 25 additions and 9 deletions
|
@ -5,29 +5,47 @@ import { plainToClass } from "class-transformer";
|
|||
import { validate } from "class-validator";
|
||||
|
||||
import { init, Page } from "./db";
|
||||
import { Page as PageConfig } from "./page";
|
||||
import { Page as PageConfig, Exercise as ExerciseConfig } from "./page";
|
||||
|
||||
// TODO: configure this thru cmdline or something later
|
||||
let materials_dir = "../material";
|
||||
let db_file = "test.db";
|
||||
|
||||
let db;
|
||||
let SLUG_RE = /([A-Za-z\-\_]+)/;
|
||||
|
||||
async function loadPageIntoDb(name: string): Promise<void> {
|
||||
// check if this slug has already been loaded into the database
|
||||
let slug_match = name.match(SLUG_RE);
|
||||
let slug = slug_match[0];
|
||||
console.log("SLUG", slug);
|
||||
|
||||
// if this slug has already been loaded into the database, don't do anything
|
||||
let hasPage = await Page.count({ where: { slug }}) > 0;
|
||||
if (hasPage) { return; }
|
||||
|
||||
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_cfg = plainToClass(PageConfig, parsedData);
|
||||
await validate(page_cfg);
|
||||
|
||||
// save page
|
||||
let page = new Page({
|
||||
slug: page_config.slug,
|
||||
title: page_config.title,
|
||||
slug,
|
||||
title: page_cfg.title,
|
||||
});
|
||||
await page.save();
|
||||
|
||||
// save exercises
|
||||
async function loadExerciseIntoDb(ex_cfg: ExerciseConfig): Promise<void> {
|
||||
console.log("pog", ex_cfg);
|
||||
}
|
||||
|
||||
let exercises = page_cfg.exercises;
|
||||
if (exercises != null) {
|
||||
await Promise.all(exercises
|
||||
.map(loadExerciseIntoDb));
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
export class Page {
|
||||
public slug: string;
|
||||
|
||||
public title: string;
|
||||
public type: string;
|
||||
|
||||
|
|
Loading…
Reference in a new issue