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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 |
x3
x3
x3
x3
x3
x3
x3
x11
x11
x54
x18
x33
x11
x17
x17
x18
x18
x17
x17
x25
x25
x26
x26
x25
x22
x22
x25
x75
x25
x100
x100
x75
x25
x25
x25
x25
x25
x25
x25
x17
x11
x3
x9
x9
x10
x10
x9
x9
x9
x9
x9
|
|
import type { Arg, Arrayable, Promisable } from "@mizu/internal/engine"
import type { Server, ServerGenerateFileSystemOptions as FileSystemOptions, ServerGenerateOptions as GenerateOptions } from "./server.ts"
import { expandGlob } from "@std/fs"
import { common, dirname, join, resolve } from "@std/path"
import { readAll, readerFromStreamReader } from "@std/io"
import { Buffer } from "@node/buffer"
const encoder = new TextEncoder()
const decoder = new TextDecoder()
export async function generate(server: Server, sources: Array<StringSource | GlobSource | CallbackSource | URLSource>, { output, clean, fs } = {} as Omit<Required<GenerateOptions>, "fs"> & { fs: Required<FileSystemOptions> }): Promise<void> {
output = resolve(output)
if ((await Promise.resolve(fs.stat(output)).then(() => true).catch(() => false)) && clean) {
await fs.rmdir(output, { recursive: true })
}
await fs.mkdir(output, { recursive: true })
for (const [source, destination, options = {}] of sources) {
const path = join(output, destination)
if (source instanceof URL) {
const bytes = await fetch(source).then((response) => response.bytes())
await fs.write(path, await render(server, Buffer.from(bytes), options.render))
}
else if (typeof source === "function") {
let bytes = await source()
if (typeof bytes === "string") {
bytes = Buffer.from(encoder.encode(bytes))
}
await fs.write(path, await render(server, bytes, options.render))
}
else if ("directory" in options) {
const root = `${options.directory}`
const sources = [source].flat()
for (const source of sources) {
for await (const { path: from } of expandGlob(source, { root, includeDirs: false })) {
const path = join(output, from.replace(common([root, from]), ""))
await fs.mkdir(dirname(path), { recursive: true })
await fs.write(path, await render(server, await fs.read(from), options.render))
}
}
}
else {
await fs.write(path, await render(server, Buffer.from(encoder.encode(source as string)), options.render))
}
}
}
async function render(server: Server, content: Arg<NonNullable<FileSystemOptions["write"]>, 1>, render?: Arg<Server["render"], 1>): Promise<Arg<NonNullable<FileSystemOptions["write"]>, 1>> {
if (render) {
if (content instanceof ReadableStream) {
content = Buffer.from(await readAll(readerFromStreamReader(content.getReader())))
}
const rendered = await server.render(decoder.decode(content), render)
content = Buffer.from(encoder.encode(rendered))
}
return content
}
export type StringSource = [
string,
string,
{
render?: Arg<Server["render"], 1>
}?,
]
export type GlobSource = [
Arrayable<string>,
string,
{
directory: string
render?: Arg<Server["render"], 1>
},
]
export type CallbackSource = [
() => Promisable<Arg<NonNullable<FileSystemOptions["write"]>, 1> | string>,
string,
{
render?: Arg<Server["render"], 1>
}?,
]
export type URLSource = [
URL,
string,
{
render?: Arg<Server["render"], 1>
}?,
]
|