deno.land / x / lume@v2.1.4 / core / writer.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { posix } from "../deps/path.ts";import { emptyDir, ensureDir } from "../deps/fs.ts";import { concurrent } from "./utils/concurrent.ts";import { sha1 } from "./utils/digest.ts";import { log } from "./utils/log.ts";import binaryLoader from "./loaders/binary.ts";
import type { Page, StaticFile } from "./file.ts";
export interface Options { dest: string;}
/** Generic interface for Writer */export interface Writer { savePages(pages: Page[]): Promise<Page[]>; copyFiles(files: StaticFile[]): Promise<StaticFile[]>; clear(): Promise<void>; removeFiles(files: string[]): Promise<void>;}
/** * Class to write the generated pages and static files * in the dest folder. */export class FSWriter implements Writer { dest: string;
#outputs = new Map<string, [number, string, string]>(); #saveCount = 0;
constructor(options: Options) { this.dest = options.dest; }
/** * Save the pages in the dest folder * Returns an array of pages that have been saved */ async savePages(pages: Page[]): Promise<Page[]> { const savedPages: Page[] = []; ++this.#saveCount;
await concurrent( pages, async (page) => { if (await this.savePage(page)) { savedPages.push(page); } }, );
return savedPages; }
/** * Save a page in the dest folder * Returns a boolean indicating if the page has saved */ async savePage(page: Page): Promise<boolean> { const { sourcePath, outputPath, content } = page; // Ignore empty pages if (!content) { log.warn( `[Lume] <cyan>Skipped page</cyan> ${page.data.url} (file content is empty)`, ); return false; }
const filename = posix.join(this.dest, outputPath); const id = filename.toLowerCase(); const hash = await sha1(content); const previous = this.#outputs.get(id); this.#outputs.set(id, [this.#saveCount, sourcePath, hash]);
if (previous) { const [previousCount, previousSourcePath, previousHash] = previous;
if (previousCount === this.#saveCount) { throw new Error( `The pages ${sourcePath} and ${previousSourcePath} have the same output path "${outputPath}". Use distinct 'url' values to resolve the conflict.`, ); }
// The page content didn't change if (previousHash === hash) { return false; } }
log.info(`🔥 ${page.data.url} <- <gray>${sourcePath}</gray>`);
await ensureDir(posix.dirname(filename));
page.content instanceof Uint8Array ? await Deno.writeFile(filename, page.content) : await Deno.writeTextFile(filename, page.content as string);
return true; }
/** * Copy the static files in the dest folder */ async copyFiles(files: StaticFile[]): Promise<StaticFile[]> { const copyFiles: StaticFile[] = [];
await concurrent( files, async (file) => { if (await this.copyFile(file)) { copyFiles.push(file); } }, );
return copyFiles; }
/** * Copy a static file in the dest folder * Returns a boolean indicating if the file has saved */ async copyFile(file: StaticFile): Promise<boolean> { const { entry } = file;
if (entry.flags.has("saved")) { return false; }
entry.flags.add("saved"); const pathTo = posix.join(this.dest, file.outputPath);
try { await ensureDir(posix.dirname(pathTo));
if (entry.flags.has("remote")) { await Deno.writeFile( pathTo, (await entry.getContent(binaryLoader)).content as Uint8Array, ); } else { // Copy file https://github.com/denoland/deno/issues/19425 Deno.writeFileSync(pathTo, Deno.readFileSync(entry.src)); } log.info( `🔥 ${file.outputPath} <- <gray>${ entry.flags.has("remote") ? entry.src : entry.path }</gray>`, ); return true; } catch { // Ignored }
return false; }
/** Empty the dest folder */ async clear() { await emptyDir(this.dest); this.#outputs.clear(); }
async removeFiles(files: string[]) { await concurrent( files, async (file) => { try { const outputPath = posix.join(this.dest, file); this.#outputs.delete(outputPath.toLowerCase()); await Deno.remove(outputPath); } catch { // Ignored } }, ); }}
lume

Version Info

Tagged at
7 months ago