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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 |
x3
x3
x3
x3
x3
x3
x9
x9
x8
x8
x9
x9
x9
x9
x1
x1
x9
x9
x3
x3
x1
x1
x3
x8
x8
x4
x4
x4
x4
x6
x6
x6
x6
x4
x5
x5
x1
x1
x9
x9
x3
x11
x11
x1
x1
x11
x11
x11
x11
x11
x3
x12
x16
x16
x16
x16
x16
x8
x8
x8
x8
x6
x6
x8
x16
x16
x12 |
I
I
|
import type { Arg, Arrayable, Promisable } from "@mizu/internal/engine"
import type { Server, ServerGenerateFileSystemOptions as FileSystemOptions, ServerGenerateOptions as GenerateOptions } from "./server.ts"
import { common, dirname, globToRegExp, 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.rm(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, { fs, root })) {
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>
}?,
]
async function* expandGlob(glob: string, { fs, root, directory = root }: { fs: Pick<FileSystemOptions, "readdir" | "stat">; root: string; directory?: string }): AsyncGenerator<{ path: string }> {
if (!await Promise.resolve(fs.stat(directory)).then(() => true).catch(() => false)) {
return
}
for (const entry of await fs.readdir(directory)) {
const file = typeof entry === "object" ? entry.name : entry
const path = join(directory, file)
const stats = await fs.stat(path)
if (stats) {
if (typeof stats.isDirectory === "function" ? stats.isDirectory() : stats.isDirectory) {
yield* expandGlob(glob, { fs, root, directory: path })
} else {
let relative = common([root, directory]).replaceAll("\\", "/")
if (!relative.endsWith("/")) {
relative += "/"
}
if (globToRegExp(glob, { extended: true, globstar: true }).test(path.replaceAll("\\", "/").replace(relative, ""))) {
yield { path }
}
}
}
}
}
|