UNPKG

squint-cli

Version:

Squint makes visual reviews of web app releases easy

72 lines (71 loc) 3.06 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.screenshotCommand = exports.compareCommand = exports.crawlCommand = void 0; const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const utils_1 = require("./utils"); const compare_1 = require("./core/compare"); const crawl_1 = require("./core/crawl"); const screenshot_1 = require("./core/screenshot"); async function crawlCommand(pagePool, config) { const paths = await crawl_1.crawlPaths({ pagePool, // Use new url as the basis for traversal. // This will correctly show new pages that are not yet in the old version urlsToVisit: new Set([config.crawlUrl]), shouldVisit: crawl_1.createShouldVisit(config.crawlFilters, { baseUrl: config.crawlUrl }), ...config, }); [...paths].forEach((urlPath) => console.log(urlPath)); } exports.crawlCommand = crawlCommand; async function compareCommand(pagePool, config) { console.error('Cleaning output directory ..'); await compare_1.clean(config); if (config.singlePage) { console.error(`Comparing ${config.oldUrl} to ${config.newUrl} ..`); await compare_1.compareUrls(pagePool, config.oldUrl, config.newUrl, config); } else { await compareMultiMode(pagePool, config); console.error(`Saved comparison images to ${path_1.default.resolve(config.outDir)}`); } } exports.compareCommand = compareCommand; async function compareMultiMode(pagePool, config) { let paths; if (config.pathsFile) { const content = await fs_1.default.promises.readFile(config.pathsFile, { encoding: 'utf-8' }); paths = content.trim().split('\n').map(line => line.trim()).filter(line => Boolean(line)); } else { paths = await crawl_1.crawlPaths({ pagePool, // Use new url as the basis for traversal. // This will correctly show new pages that are not yet in the old version urlsToVisit: new Set([config.newUrl]), shouldVisit: crawl_1.createShouldVisit(config.crawlFilters, { baseUrl: config.newUrl }), ...config, }); } await utils_1.promiseEachSeries(paths, async (urlPath) => { console.error(`Comparing ${urlPath} ..`); const oldUrl = compare_1.resolveUrl(config.oldUrl, urlPath); const newUrl = compare_1.resolveUrl(config.newUrl, urlPath); await compare_1.compareUrls(pagePool, oldUrl, newUrl, config); }); } async function screenshotCommand(pagePool, config) { try { await fs_1.default.promises.mkdir(path_1.default.dirname(config.outFile), { recursive: true }); } catch (e) { // ignore } await screenshot_1.screenshot(pagePool, config); console.error(`Saved screenshot to ${path_1.default.resolve(config.outFile)}`); } exports.screenshotCommand = screenshotCommand;