idraw/scripts/snapshot.ts

41 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2022-12-31 15:21:57 +00:00
import jimp from 'jimp';
import path from 'path';
import { delay } from './util/time';
import { createScreenshot } from './util/screen';
import { removeFullDir } from './util/file';
import { pageList } from './screen.config';
2021-05-23 12:16:15 +00:00
const snapshotDir = path.join(__dirname, '..', '__tests__', 'snapshot');
main();
async function main() {
2021-06-04 06:43:42 +00:00
removeFullDir(snapshotDir);
2021-05-23 12:16:15 +00:00
const middlewares = [];
2021-06-08 11:22:11 +00:00
pageList.forEach((p, i) => {
2021-05-23 12:16:15 +00:00
middlewares.push(async (ctx = {}, next) => {
2023-02-12 05:34:06 +00:00
const { page, port } = ctx as any;
console.log(`[${i + 1}/${pageList.length}] Screen: ${p.path}`);
await page.setViewport({ width: p.w, height: p.h });
2022-04-28 15:46:07 +00:00
const pageUrl = `http://127.0.0.1:${port}/examples/${p.path || ''}`;
2021-06-04 06:43:42 +00:00
const result = await page.goto(pageUrl);
if (result.status() === 404) {
2022-12-31 15:21:57 +00:00
console.error(`404 Not Found: ${pageUrl}`);
throw Error('404 status code found in result');
2021-06-04 06:43:42 +00:00
}
2021-05-23 12:16:15 +00:00
await delay(p.delay || 100);
const buf = await page.screenshot();
(await jimp.read(buf)).scale(1).quality(100).write(createPicPath(p.path));
await next();
});
});
2023-02-12 05:34:06 +00:00
await createScreenshot(middlewares, { baseDir: path.join(__dirname, '..') });
2021-05-23 12:16:15 +00:00
}
function createPicPath(pagePath) {
let picPath = path.join(snapshotDir, pagePath);
2021-06-04 06:43:42 +00:00
picPath = picPath + '.jpg'; // picPath.replace(/\.html$/, '.jpg');
2021-05-23 12:16:15 +00:00
return picPath;
}