-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
utils.ts
180 lines (151 loc) Β· 4.22 KB
/
utils.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 { assertSnapshot } from "../deps/snapshot.ts";
import lume from "../mod.ts";
import { fromFileUrl, join } from "../deps/path.ts";
import { printError } from "../core/errors.ts";
import type { Page, Site, SiteOptions } from "../core.ts";
const cwUrl = new URL("./", import.meta.url);
const cwd = fromFileUrl(new URL("./", import.meta.url));
export function getPath(path: string): string {
return join(cwd, path);
}
/** Create a new lume site using the "assets" path as cwd */
export function getSite(
options: Partial<SiteOptions> = {},
pluginOptions = {},
preventSave = true,
): Site {
options.cwd = getPath("assets");
options.quiet = true;
const site = lume(options, pluginOptions, false);
// Don't save the site to disk
if (preventSave) {
site.addEventListener("beforeSave", () => false);
}
return site;
}
/** Returns a generated page by src path */
export function getPage(site: Site, path: string) {
const page = site.pages.find((page) => page.src.path === path);
if (!page) {
throw new Error(`Page "${path}" not found`);
}
return page;
}
/** Build a site and print errors */
export async function build(site: Site) {
try {
await site.build();
} catch (error) {
printError(error);
throw error;
}
}
/** Get the version of a dependency */
export async function getDepVersion(
file: string,
name: string,
): Promise<string | undefined> {
const filepath = join(cwd, `../deps/${file}`);
const content = await Deno.readTextFile(filepath);
const match = content.match(`${name}@([^\/]+)`);
if (match) {
return match[1];
}
}
function normalizeContent(
content: string | Uint8Array | undefined,
): string | undefined {
if (content instanceof Uint8Array) {
return `Uint8Array(${content.length})`;
}
if (typeof content === "string") {
// Normalize line ending for Windows
return content
.replaceAll("\r\n", "\n")
.replaceAll(/base64,[^"]+/g, "base64,(...)");
}
}
async function assertPageSnapshot(
context: Deno.TestContext,
page: Page,
) {
let { content, data } = page;
const { dest } = page;
const src = {
path: page.src.path,
ext: page.src.ext,
// Remote base path because it's different in the test environment
remote: page.src.remote?.replace(cwUrl.href, ""),
};
// Remove pagination results details from the data
if (Array.isArray(page.data.results)) {
page.data.results = page.data.results.length;
}
// Remove page reference
page.data.page = undefined;
// Normalize content for Windows
content = normalizeContent(content);
data.content = normalizeContent(
data.content as string | Uint8Array | undefined,
);
// Source maps
if (page.dest.ext === ".map") {
content = "(removed for testing)";
data.content = "(removed for testing)";
}
// Ignore comp object
if (data.comp) {
data.comp = {};
}
// Normalize date
if (data.date instanceof Date) {
data.date = new Date(0);
}
// Sort data alphabetically
const entries = Object.entries(data).sort((a, b) => a[0].localeCompare(b[0]));
data = Object.fromEntries(entries);
await assertSnapshot(context, { src, dest, data, content });
}
export async function assertSiteSnapshot(
context: Deno.TestContext,
site: Site,
) {
const { pages, files } = site;
// Test number of pages
await assertSnapshot(context, pages.length);
// To-do: test site configuration
await assertSnapshot(
context,
{
formats: Array.from(site.formats.entries.values()).map((format) => {
return {
...format,
engine: !!format.engine,
};
}),
},
);
// Sort pages and files alphabetically
pages.sort((a, b) => {
return compare(a.src.path, b.src.path) || compare(a.dest.path, b.dest.path);
});
files.sort((a, b) => {
return compare(a.src, b.src);
});
// Test static files
await assertSnapshot(
context,
files.map((file) => {
// Remote base path because it's different in the test environment
file.remote = file.remote?.replace(cwUrl.href, "");
return file;
}),
);
// Test pages
for (const page of pages) {
await assertPageSnapshot(context, page);
}
}
function compare(a: string, b: string): number {
return a > b ? 1 : a < b ? -1 : 0;
}