-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_eleventy-test-instance.ts
62 lines (47 loc) · 1.54 KB
/
_eleventy-test-instance.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
import Eleventy from '@11ty/eleventy';
import type UserConfig from '@11ty/eleventy/UserConfig';
import path from 'node:path';
import fs from 'node:fs';
import { VentoPlugin } from '../src/plugin.js';
import type { PluginOptions } from '../src/types/options.js';
interface JsonResult {
url: string | false;
inputPath: string;
outputPath: string | false;
rawInput: string;
content: string;
}
interface TestOptions {
pluginOptions?: PluginOptions;
eleventy?: ConstructorParameters<typeof Eleventy>[2];
}
class EleventyTest extends Eleventy {
buildResults: JsonResult[] = [];
constructor(inputPath: string, options: Partial<TestOptions> = {}) {
const outputPath = path.join(inputPath, '_site');
const maybeConfigPath = path.join(inputPath, 'eleventy.config.js');
// Initialize parent class
super(inputPath, outputPath, {
...options.eleventy,
quietMode: true,
configPath: fs.existsSync(maybeConfigPath) ? maybeConfigPath : undefined,
config(eleventyConfig: UserConfig) {
eleventyConfig.addPlugin(VentoPlugin, options?.pluginOptions);
},
});
}
async rebuild() {
const newResults = (await this.toJSON()) as unknown;
this.buildResults = newResults as JsonResult[];
}
getBuildResultForUrl(url: string) {
return this.buildResults.find((page) => page.url === url);
}
getPageWithInput(content: string) {
return this.buildResults.find((page) => page.rawInput === content);
}
countResultPages(): number {
return this.buildResults.filter(({ outputPath }) => Boolean(outputPath)).length;
}
}
export { EleventyTest };