/
capture-screenshots.ts
134 lines (117 loc) · 4.27 KB
/
capture-screenshots.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
import * as fs from "fs";
import { platform } from "os";
import { resolve, join, dirname, relative } from "path";
import { Application, TSConfigReader, EntryPointStrategy } from "..";
import { remove } from "../lib/utils";
import { glob } from "../lib/utils/fs";
// The @types package plays badly with non-dom packages.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const puppeteer = require("puppeteer");
const concurrency = 10;
const src = join(__dirname, "../../src/test/renderer/testProject/src");
const baseDirectory = join(__dirname, "../../dist/tmp/capture");
const outputDirectory = join(__dirname, "../../dist/tmp/__screenshots__");
const viewport = { width: 1024, height: 768 };
class PQueue {
private queued: (() => Promise<void>)[] = [];
constructor(private concurrency: number) {}
add(action: () => Promise<void>) {
this.queued.push(action);
}
run() {
return new Promise<void>((resolve, reject) => {
const queue: Promise<void>[] = [];
const doReject = (err: unknown) => {
this.queued.length = 0;
queue.length = 0;
reject(err);
};
const tick = () => {
while (queue.length < this.concurrency) {
const next = this.queued.shift();
if (next) {
const nextPromise = Promise.resolve().then(next);
queue.push(nextPromise);
nextPromise.then(() => {
queue.splice(queue.indexOf(nextPromise), 1);
tick();
}, doReject);
} else {
break;
}
}
if (queue.length === 0) {
resolve();
}
};
tick();
});
}
}
export async function captureRegressionScreenshots() {
const app = new Application();
app.options.addReader(new TSConfigReader());
app.bootstrap({
logger: "console",
readme: join(src, "..", "README.md"),
name: "typedoc",
cleanOutputDir: true,
tsconfig: join(src, "..", "tsconfig.json"),
plugin: [],
entryPoints: [src],
entryPointStrategy: EntryPointStrategy.Expand,
});
const project = app.convert();
if (!project) throw new Error("Failed to convert.");
await remove(outputDirectory);
await app.generateDocs(project, baseDirectory);
await captureScreenshots(baseDirectory, outputDirectory);
}
export async function captureScreenshots(
baseDirectory: string,
outputDirectory: string
) {
const browser = await puppeteer.launch({
args:
platform() === "win32"
? []
: ["--no-sandbox", "--disable-setuid-sandbox"],
});
const queue = new PQueue(concurrency);
for (const file of glob("**/*.html", baseDirectory)) {
queue.add(async () => {
const absPath = resolve(baseDirectory, file);
const outputPath = resolve(
outputDirectory,
relative(baseDirectory, file).replace(".html", "")
);
fs.mkdirSync(dirname(outputPath), { recursive: true });
const page = await browser.newPage();
await page.setViewport(viewport);
await page.goto(`file://${absPath}`, {
waitUntil: "domcontentloaded", // 'load' 'networkidle0' 'networkidle2'
});
await new Promise<void>((res) => setTimeout(() => res(), 100));
await page.screenshot({
path: outputPath + "-light.png",
fullPage: true,
});
await page.evaluate('document.body.classList.add("dark")');
await new Promise<void>((res) => setTimeout(() => res(), 100));
await page.screenshot({
path: outputPath + "-dark.png",
fullPage: true,
});
await page.close();
});
}
await queue.run();
await browser.close();
}
if (require.main == module) {
captureRegressionScreenshots().catch((err) => {
// eslint-disable-next-line no-console
console.error(err);
process.exit(1);
});
}