All files / render / server / generate.ts

100.00% Branches 10/10
100.00% Lines 51/51
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

























































































































// Imports
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"

/** Text encoder. */
const encoder = new TextEncoder()

/** Text decoder. */
const decoder = new TextDecoder()

/** Used by {@linkcode Server.generate()} to generate content. */
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> {
  // Prepare output directory
  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 })

  // Generate files
  for (const [source, destination, options = {}] of sources) {
    const path = join(output, destination)
    // Copy content from URL
    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))
    } // Copy content from callback
    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))
    } // Copy content from local files
    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))
        }
      }
    } // Copy content from string
    else {
      await fs.write(path, await render(server, Buffer.from(encoder.encode(source as string)), options.render))
    }
  }
}

/** Used by {@linkcode generate()} to render content. */
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
}

/** String source. */
export type StringSource = [
  /** File content. */
  string,
  /** Destination path (including filename). */
  string,
  /** Options. */
  {
    /** Whether to render content with {@linkcode Static.render()}. */
    render?: Arg<Server["render"], 1>
  }?,
]

/**
 * Glob source.
 *
 * The presence of the `directory` option is used to distinguish this type from {@linkcode StringSource}.
 */
export type GlobSource = [
  /** A list of file globs. */
  Arrayable<string>,
  /** Destination path (excluding filename). */
  string,
  /** Options. */
  {
    /** Source directory. */
    directory: string
    /** Whether to render content with {@linkcode Static.render()}. */
    render?: Arg<Server["render"], 1>
  },
]

/** Callback source. */
export type CallbackSource = [
  /** A callback that returns file content. */
  () => Promisable<Arg<NonNullable<FileSystemOptions["write"]>, 1> | string>,
  /** Destination path (including filename). */
  string,
  /** Options. */
  {
    /** Whether to render content with {@linkcode Static.render()}. */
    render?: Arg<Server["render"], 1>
  }?,
]

/** URL source. */
export type URLSource = [
  /** Source URL. */
  URL,
  /** Destination path (including filename). */
  string,
  /** Options. */
  {
    /** Whether to render content with {@linkcode Static.render()}. */
    render?: Arg<Server["render"], 1>
  }?,
]