-
Notifications
You must be signed in to change notification settings - Fork 0
/
saki.ts
74 lines (66 loc) · 2.61 KB
/
saki.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import * as path from "path";
import { copyFileSync, existsSync, readdirSync, rmSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import type { Renderer } from "./ryuji.js";
export class Builder {
build_dir: string;
constructor(build_dir: string="/build") {
this.build_dir = path.join(__dirname, build_dir);
if (existsSync(this.build_dir)) {
//wipe the build directory
rmSync(this.build_dir, {
recursive: true,
});
}
mkdirSync(this.build_dir);
}
static copy_folder(folder_path: string, dest_path: string) {
let children: string[] = readdirSync(folder_path);
for (let i=0; i < children.length; i++) {
let child: string = children[i];
let child_path: string = path.join(folder_path, child);
let copy_path: string = path.join(dest_path, child);
if (child.includes(".")) {
//file
copyFileSync(child_path, copy_path);
} else {
//directory, make directory and recursively copy
mkdirSync(copy_path);
Builder.copy_folder(child_path, path.join(dest_path, child));
}
}
}
serve_static_folder(static_dir: string, serve_from: string="/") {
let static_path: string = path.join(__dirname, static_dir);
let dest_path: string = path.join(this.build_dir, serve_from);
Builder.copy_folder(static_path, dest_path);
}
serve_content(content: string, serve_path: string) {
let dest_path: string = path.join(this.build_dir, serve_path);
if (!serve_path.includes(".")) {
//serve as index.html in serve_path directory
//will not make a new directory if `serve_path` is "/", since the build directory already exists
if (dest_path !== this.build_dir && dest_path !== path.join(this.build_dir, "/")) {
mkdirSync(dest_path, {
recursive: true,
});
}
writeFileSync(path.join(dest_path, "index.html"), content);
} else {
//serve as file regularly
writeFileSync(dest_path, content);
}
}
serve_file(file_path: string, serve_path: string) {
let file_content: string = readFileSync(path.join(__dirname, file_path), "utf-8");
this.serve_content(file_content, serve_path);
}
serve_template(renderer: Renderer, serve_path: string, template_name: string, vars: any) {
let content: string = renderer.render_template(template_name, vars);
this.serve_content(content, serve_path);
}
serve_templates(renderer: Renderer, serve_paths: string[], template_name: string, vars_array: any[]) {
for (let i=0; i < serve_paths.length; i++) {
this.serve_template(renderer, serve_paths[i], template_name, vars_array[i]);
}
}
}