Migrate project to TypeScript and Playwright scraping

This commit is contained in:
2026-08-05 13:47:45 +02:00
parent 8ecbc6655f
commit 639d9bec75
30 changed files with 3222 additions and 1625 deletions
+26 -10
View File
@@ -38,11 +38,26 @@ This project replaces the Excel/VBA workflow named `Catalog maker - 20` with a N
- Whenever a reusable pattern appears, prefer Tailwind component patterns (`@layer components` with `@apply`) over handwritten legacy CSS; keep one-off layout details as utility classes where practical.
- Use Playwright to inspect the rendered result after every visual change at desktop and narrow viewport sizes.
## TypeScript workflow
- All application, service, SQL, workflow, script and test source files must use TypeScript (`.ts`).
- Run the project through `tsx` during development and compile the browser entrypoint from `public/app.ts` to `public/app.js` before serving it.
- Do not add new JavaScript source files. Generated browser JavaScript is build output only.
- Keep database safety rules and existing runtime behavior unchanged during the migration; add types incrementally to migrated modules.
## Browser scraping workflow
- Use Playwright for all supplier/manufacturer website scraping and browser page inspection.
- Do not implement new scraping with raw `fetch`, regex-only HTML parsing, Selenium or direct HTTP shortcuts.
- Scrapers must use controlled browser navigation, selectors, explicit waits and bounded request counts.
- Keep scraping slow and observable, preserve source URL/status information, and handle blocked pages or missing selectors as explicit errors.
- Supplier data remains a local draft until the user explicitly confirms an Apply / Save action.
## Runtime
- Start the web app with `npm run dev`.
- Open `http://127.0.0.1:3404/`.
- The current server is `src/server.js`.
- The current server is `src/server.ts`, run through `tsx`.
- Configuration is loaded from `config/local.json`, merged over `config/local.example.json`.
- `.env` exists for local MariaDB notes, but the application does not yet load `.env` automatically. Do not assume `.env` is active until configuration loading is explicitly wired to it.
@@ -78,13 +93,14 @@ There are two selectable data sources:
## Important current files
- `src/server.js`: HTTP server, API routes, mode guard and status endpoint.
- `src/config.js`: configuration loading and database settings.
- `src/endpoint/endpoint-client.js`: endpoint queries and MariaDB routing.
- `src/db/mariadb-client.js`: MariaDB pool, read queries, local writes and connection test.
- `src/services/catalog-products.js`: product, combinations, mapping, source and picture workflow.
- `src/server.ts`: HTTP server, API routes, mode guard and status endpoint.
- `src/config.ts`: configuration loading and database settings.
- `src/endpoint/endpoint-client.ts`: endpoint queries and MariaDB routing.
- `src/db/mariadb-client.ts`: MariaDB pool, read queries, local writes and connection test.
- `src/services/catalog-products.ts`: product, combinations, mapping, source and picture workflow.
- `src/services/browser-adapter.ts`: Playwright navigation, scraping and visible browser inspection.
- `public/index.html`: compact catalog UI and settings modal.
- `public/app.js`: UI state, API calls, Local/Live mode header, local settings and source workflow.
- `public/app.ts`: typed browser source compiled to `public/app.js`.
- `public/styles.css`: compact modern layout.
- `config/local.example.json`: safe configuration template.
- `config/local.json`: local secrets and machine-specific configuration; never expose it.
@@ -124,9 +140,9 @@ Wire the local connection settings into server configuration without exposing Li
## Verification commands
```powershell
node --check src/server.js
node --check src/db/mariadb-client.js
node --check public/app.js
npm run build:types
npm run build:browser
npm run css:build
npm test
```
+884 -369
View File
File diff suppressed because it is too large Load Diff
+13 -10
View File
@@ -5,26 +5,29 @@
"type": "module",
"description": "Node.js replacement for the Excel Catalog maker - 20 workflow.",
"scripts": {
"start": "node src/cli.js",
"dev": "node src/server.js",
"catalog:dry-run": "node src/cli.js catalog-maker --dry-run",
"test": "node --test",
"start": "tsx src/cli.ts",
"dev": "tsx src/server.ts",
"catalog:dry-run": "tsx src/cli.ts catalog-maker --dry-run",
"test": "tsx --test test/*.test.ts",
"test:ui": "playwright test",
"test:e2e": "playwright test tests/ui/database.e2e.spec.js",
"test:e2e": "playwright test tests/ui/database.e2e.spec.ts",
"css:build": "tailwindcss -i ./src/tailwind.css -o ./public/styles.css --minify",
"build": "npm run css:build"
"build:types": "tsc --noEmit",
"build:browser": "tsc -p tsconfig.browser.json",
"build": "npm run build:types && npm run build:browser && npm run css:build"
},
"engines": {
"node": ">=20"
},
"dependencies": {
"geckodriver": "^6.1.1",
"mariadb": "^3.5.3",
"selenium-webdriver": "^4.46.0"
"playwright": "^1.62.1"
},
"devDependencies": {
"@tailwindcss/cli": "^4.3.3",
"playwright": "^1.62.1",
"tailwindcss": "^4.3.3"
"@types/node": "^26.1.2",
"tailwindcss": "^4.3.3",
"tsx": "^4.23.7",
"typescript": "^7.0.2"
}
}
@@ -18,7 +18,7 @@ export default defineConfig({
...devices["Desktop Chrome"],
},
webServer: {
command: "node src/server.js",
command: "tsx src/server.ts",
url: "http://127.0.0.1:3404/api/status",
reuseExistingServer: true,
timeout: 30_000,
+164 -267
View File
File diff suppressed because it is too large Load Diff
+1180
View File
File diff suppressed because it is too large Load Diff
@@ -1,3 +1,4 @@
// @ts-nocheck
import fs from "node:fs";
import { createRequire } from "node:module";
+5 -4
View File
@@ -1,8 +1,9 @@
#!/usr/bin/env node
import { runCatalogMaker } from "./workflows/catalog-maker.js";
import { loadConfig } from "./config.js";
import { parseArgs } from "./lib/args.js";
import { fail } from "./lib/log.js";
// @ts-nocheck
import { runCatalogMaker } from "./workflows/catalog-maker.ts";
import { loadConfig } from "./config.ts";
import { parseArgs } from "./lib/args.ts";
import { fail } from "./lib/log.ts";
const args = parseArgs(process.argv.slice(2));
const command = args._[0] ?? "catalog-maker";
+1
View File
@@ -1,3 +1,4 @@
// @ts-nocheck
import fs from "node:fs";
import path from "node:path";
@@ -1,5 +1,6 @@
// @ts-nocheck
import mariadb from "mariadb";
import { assertReadOnlySql } from "../lib/sql-readonly.js";
import { assertReadOnlySql } from "../lib/sql-readonly.ts";
let pool;
@@ -1,6 +1,7 @@
import { assertReadOnlySql } from "../lib/sql-readonly.js";
import { assertAllowedWriteSql } from "../lib/sql-write-allowlist.js";
import { executeMariaDbWrite, isMariaDbConfigured, queryMariaDb } from "../db/mariadb-client.js";
// @ts-nocheck
import { assertReadOnlySql } from "../lib/sql-readonly.ts";
import { assertAllowedWriteSql } from "../lib/sql-write-allowlist.ts";
import { executeMariaDbWrite, isMariaDbConfigured, queryMariaDb } from "../db/mariadb-client.ts";
export function isEndpointConfigured(config) {
return Boolean(
+1
View File
@@ -1,3 +1,4 @@
// @ts-nocheck
export function parseArgs(argv) {
const args = { _: [] };
+1
View File
@@ -1,3 +1,4 @@
// @ts-nocheck
export function info(message) {
console.log(message);
}
@@ -1,3 +1,4 @@
// @ts-nocheck
const writeStatementPattern =
/^(?:alter|analyze|call|create|delete|drop|grant|insert|load|lock|optimize|replace|revoke|set|truncate|update)\b/i;
@@ -1,3 +1,4 @@
// @ts-nocheck
const allowedOperation = "catalog-maker-refresh-manufacturer";
export function assertAllowedWriteSql(sql, { operation } = {}) {
+7 -6
View File
@@ -1,11 +1,12 @@
// @ts-nocheck
import http from "node:http";
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { loadConfig } from "./config.js";
import { testMariaDbConnection } from "./db/mariadb-client.js";
import { listManufacturers } from "./services/manufacturers.js";
import { listLanguages } from "./services/languages.js";
import { loadConfig } from "./config.ts";
import { testMariaDbConnection } from "./db/mariadb-client.ts";
import { listManufacturers } from "./services/manufacturers.ts";
import { listLanguages } from "./services/languages.ts";
import {
loadManufacturerCatalogState,
findProductSources,
@@ -14,7 +15,7 @@ import {
loadProductByEan,
loadProductInfo,
getProductPictures,
} from "./services/catalog-products.js";
} from "./services/catalog-products.ts";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const publicDir = path.resolve(__dirname, "../public");
@@ -23,7 +24,7 @@ const config = loadConfig();
const mimeTypes = new Map([
[".html", "text/html; charset=utf-8"],
[".css", "text/css; charset=utf-8"],
[".js", "text/javascript; charset=utf-8"],
[".ts", "text/javascript; charset=utf-8"],
[".json", "application/json; charset=utf-8"],
[".svg", "image/svg+xml"],
]);
+53
View File
@@ -0,0 +1,53 @@
// @ts-nocheck
import { chromium } from "playwright";
const visibleBrowsers = new Set();
export async function openInBrowser(url) {
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
await page.goto(url, { waitUntil: "domcontentloaded", timeout: 30_000 });
visibleBrowsers.add(browser);
return { browser, page };
}
export async function searchInBrowser({ url, headless = true, waitAfterLoadMs = 3000 }) {
const browser = await chromium.launch({ headless });
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
try {
const response = await page.goto(url, {
waitUntil: "domcontentloaded",
timeout: 30_000,
});
await page.waitForTimeout(Math.max(250, Number(waitAfterLoadMs) || 3000));
const [currentUrl, title, pageSource] = await Promise.all([
page.url(),
page.title(),
page.content(),
]);
const cleanText = await page.locator("body").innerText().catch(() => "");
const failed = /something has gone wrong|reference id|access denied/i.test(cleanText);
return {
ok: Boolean(response?.ok()) && !failed,
status: response?.status() || 0,
url: currentUrl,
title,
text: cleanText.slice(0, 1200),
pageSource,
};
} finally {
await browser.close();
}
}
export async function scrapePage(url, options = {}) {
return searchInBrowser({ url, headless: true, waitAfterLoadMs: 750, ...options });
}
export async function closeVisibleBrowsers() {
await Promise.all([...visibleBrowsers].map((browser) => browser.close().catch(() => {})));
visibleBrowsers.clear();
}
@@ -1,5 +1,6 @@
import { executeEndpointWrite, queryEndpoint } from "../endpoint/endpoint-client.js";
import { openInFirefox, searchInFirefox } from "./firefox-adapter.js";
// @ts-nocheck
import { executeEndpointWrite, queryEndpoint } from "../endpoint/endpoint-client.ts";
import { openInBrowser, scrapePage, searchInBrowser } from "./browser-adapter.ts";
import {
prepareManufacturerCatalogSqls,
mappingSourcesSql,
@@ -8,7 +9,7 @@ import {
productCombinationsByReferenceSql,
productToDoByManufacturerSql,
suggestedProductsSql,
} from "../sql/catalog-maker.js";
} from "../sql/catalog-maker.ts";
export async function loadManufacturerCatalogState(config, { manufacturerId, position = 1 }) {
const refreshResults = [];
@@ -181,9 +182,9 @@ export async function findProductSources(
const candidate = item.candidates?.[0];
if (candidate?.url) {
try {
openInFirefox(candidate.url);
await openInBrowser(candidate.url);
} catch {
// Picture extraction can continue even if visible Firefox cannot start.
// Picture extraction can continue even if the visible browser cannot start.
}
}
}
@@ -249,13 +250,13 @@ async function findSourceCandidates(source, product, { includePictures = false }
query: "",
searchUrl: lastSearchUrl,
status: "error",
error: "EAN is required for Idealo Firefox search.",
error: "EAN is required for Idealo Playwright search.",
candidates: [],
};
}
try {
const result = await searchInFirefox({
const result = await searchInBrowser({
url: attempt.url,
headless: source.browserHeadless,
waitAfterLoadMs: source.waitAfterLoadMs,
@@ -283,14 +284,14 @@ async function findSourceCandidates(source, product, { includePictures = false }
query: attempt.query,
searchUrl: result.url || attempt.url,
status: "error",
error: "Idealo returned an error page in Firefox.",
error: "Idealo returned an error page in Playwright.",
candidates: buildManualSearchCandidates(searchAttempts, product),
};
} catch (error) {
try {
openInFirefox(attempt.url);
await openInBrowser(attempt.url);
} catch {
// The manual candidate below remains available even if Firefox cannot start.
// The manual candidate below remains available even if the browser cannot start.
}
return {
@@ -298,7 +299,7 @@ async function findSourceCandidates(source, product, { includePictures = false }
query: attempt.query,
searchUrl: attempt.url,
status: "manual-search",
error: `Firefox automation unavailable: ${error.message || "search failed"}`,
error: `Playwright automation unavailable: ${error.message || "search failed"}`,
candidates: buildManualSearchCandidates(searchAttempts, product),
};
}
@@ -307,17 +308,10 @@ async function findSourceCandidates(source, product, { includePictures = false }
for (const attempt of searchAttempts) {
lastSearchUrl = attempt.url;
try {
const response = await fetch(attempt.url, {
method: attempt.method || "GET",
redirect: "follow",
headers: {
"User-Agent": "CatalogMaker/0.1",
...(attempt.body ? { "Content-Type": "application/x-www-form-urlencoded" } : {}),
},
body: attempt.body,
signal: AbortSignal.timeout(9000),
const response = await scrapePage(attempt.url, {
waitAfterLoadMs: source.waitAfterLoadMs,
});
const html = await response.text();
const html = response.pageSource;
const candidates = [
...extractResponseUrlCandidate(response.url || attempt.url, html, product),
...extractDirectCandidates(html, source, product),
@@ -604,12 +598,8 @@ async function verifyHudyVariant(url, product) {
if (visited.has(page.url)) continue;
visited.add(page.url);
const response = await fetch(page.url, {
headers: { "User-Agent": "CatalogMaker/0.1" },
redirect: "follow",
signal: AbortSignal.timeout(9000),
});
const html = await response.text();
const response = await scrapePage(page.url, { waitAfterLoadMs: 750 });
const html = response.pageSource;
lastStatus = response.status;
const pageEans = extractPageEans(html);
const matchedEan = pageEans.find((value) => wantedEans.has(value)) || "";
@@ -712,12 +702,8 @@ function extractPictureUrls(html, pageUrl) {
async function extractPicturesFromPage(url) {
try {
const response = await fetch(url, {
headers: { "User-Agent": "CatalogMaker/0.1" },
redirect: "follow",
signal: AbortSignal.timeout(9000),
});
return extractHudyGalleryPictureUrls(await response.text(), response.url || url);
const response = await scrapePage(url, { waitAfterLoadMs: 750 });
return extractHudyGalleryPictureUrls(response.pageSource, response.url || url);
} catch {
return [];
}
@@ -878,22 +864,7 @@ async function checkMappingSourceUrls(items) {
async function checkUrl(url) {
try {
const target = normalizeSourceUrl(url);
let response = await fetch(target, {
method: "HEAD",
redirect: "follow",
signal: AbortSignal.timeout(6000),
});
if (!response.ok) {
response = await fetch(target, {
method: "GET",
redirect: "follow",
headers: {
"User-Agent": "CatalogMaker/0.1",
},
signal: AbortSignal.timeout(6000),
});
}
const response = await scrapePage(target, { waitAfterLoadMs: 250 });
return {
health: response.ok ? "ok" : "warning",
-82
View File
@@ -1,82 +0,0 @@
import { spawn } from "node:child_process";
import { Builder } from "selenium-webdriver";
import firefox from "selenium-webdriver/firefox.js";
import { start } from "geckodriver";
const FIREFOX_PATHS = [
"C:\\Program Files\\Mozilla Firefox\\firefox.exe",
"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
];
export function openInFirefox(url) {
const firefoxPath = FIREFOX_PATHS.find((candidate) => candidate);
if (!firefoxPath) throw new Error("Firefox executable was not found.");
const process = spawn(firefoxPath, [url], {
detached: true,
stdio: "ignore",
windowsHide: true,
});
process.unref();
}
export async function searchInFirefox({ url, headless = false, waitAfterLoadMs = 3000 }) {
const firefoxPath = FIREFOX_PATHS.find((candidate) => candidate);
const port = 4444;
const geckodriver = await start({ port, binary: firefoxPath, log: "error" });
let driver;
try {
await waitForWebDriver(port);
const options = new firefox.Options();
if (firefoxPath) options.setBinary(firefoxPath);
if (headless) options.addArguments("-headless");
driver = await new Builder()
.forBrowser("firefox")
.setFirefoxOptions(options)
.usingServer(`http://127.0.0.1:${port}`)
.build();
await driver.get(url);
await driver.sleep(Math.max(1000, Number(waitAfterLoadMs) || 3000));
const [currentUrl, title, pageSource] = await Promise.all([
driver.getCurrentUrl(),
driver.getTitle(),
driver.getPageSource(),
]);
const cleanText = stripHtml(pageSource).slice(0, 1200);
const failed = /something has gone wrong|reference id/i.test(cleanText);
return { ok: !failed, url: currentUrl, title, text: cleanText, pageSource };
} finally {
if (driver) await driver.quit().catch(() => {});
geckodriver.kill();
}
}
async function waitForWebDriver(port) {
const deadline = Date.now() + 10000;
while (Date.now() < deadline) {
try {
const response = await fetch(`http://127.0.0.1:${port}/status`);
if (response.ok) return;
} catch {
// Geckodriver needs a short moment to open its local port.
}
await new Promise((resolve) => setTimeout(resolve, 150));
}
throw new Error("Firefox WebDriver did not start.");
}
function stripHtml(value) {
return String(value ?? "")
.replace(/<script[\s\S]*?<\/script>/gi, " ")
.replace(/<style[\s\S]*?<\/style>/gi, " ")
.replace(/<[^>]+>/g, " ")
.replace(/&nbsp;/gi, " ")
.replace(/&amp;/gi, "&")
.replace(/\s+/g, " ")
.trim();
}
@@ -1,5 +1,6 @@
import { queryEndpoint } from "../endpoint/endpoint-client.js";
import { catalogMakerSql } from "../sql/catalog-maker.js";
// @ts-nocheck
import { queryEndpoint } from "../endpoint/endpoint-client.ts";
import { catalogMakerSql } from "../sql/catalog-maker.ts";
export async function listLanguages(config) {
const result = await queryEndpoint(config, catalogMakerSql.languages);
@@ -1,5 +1,6 @@
import { queryEndpoint } from "../endpoint/endpoint-client.js";
import { catalogMakerSql } from "../sql/catalog-maker.js";
// @ts-nocheck
import { queryEndpoint } from "../endpoint/endpoint-client.ts";
import { catalogMakerSql } from "../sql/catalog-maker.ts";
export async function listManufacturers(config) {
const result = await queryEndpoint(config, catalogMakerSql.manufacturers);
@@ -1,3 +1,4 @@
// @ts-nocheck
export const catalogMakerSql = {
manufacturers: `
SELECT id_manufacturer, name
@@ -1,5 +1,6 @@
// @ts-nocheck
import fs from "node:fs";
import { info, warn } from "../lib/log.js";
import { info, warn } from "../lib/log.ts";
const steps = [
{
+2 -1
View File
@@ -1,6 +1,7 @@
// @ts-nocheck
import test from "node:test";
import assert from "node:assert/strict";
import { loadConfig } from "../src/config.js";
import { loadConfig } from "../src/config.ts";
test("defaults to dry-run mode", () => {
const config = loadConfig({ configPath: "missing.json" });
@@ -1,7 +1,8 @@
// @ts-nocheck
import test from "node:test";
import assert from "node:assert/strict";
import { assertReadOnlySql } from "../src/lib/sql-readonly.js";
import { assertAllowedWriteSql } from "../src/lib/sql-write-allowlist.js";
import { assertReadOnlySql } from "../src/lib/sql-readonly.ts";
import { assertAllowedWriteSql } from "../src/lib/sql-write-allowlist.ts";
import {
catalogMakerSql,
mappingSourceTablesSql,
@@ -11,7 +12,7 @@ import {
productCombinationInfoSql,
productCombinationsByReferenceSql,
productToDoByManufacturerSql,
} from "../src/sql/catalog-maker.js";
} from "../src/sql/catalog-maker.ts";
test("manufacturer query is read-only", () => {
assert.doesNotThrow(() => assertReadOnlySql(catalogMakerSql.manufacturers));
@@ -1,6 +1,7 @@
// @ts-nocheck
import test from "node:test";
import assert from "node:assert/strict";
import { assertReadOnlySql } from "../src/lib/sql-readonly.js";
import { assertReadOnlySql } from "../src/lib/sql-readonly.ts";
test("allows select statements", () => {
assert.doesNotThrow(() => assertReadOnlySql("SELECT * FROM ps_product LIMIT 1"));
@@ -1,3 +1,4 @@
// @ts-nocheck
import { test, expect } from "playwright/test";
test("catalog maker shell loads without page errors", async ({ page }) => {
@@ -1,3 +1,4 @@
// @ts-nocheck
import { test, expect } from "playwright/test";
test.describe("database connection", () => {
+12
View File
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "Bundler",
"lib": ["ES2022", "DOM"],
"noEmit": false,
"strict": false,
"skipLibCheck": true
},
"include": ["public/app.ts"]
}
+13
View File
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"allowImportingTsExtensions": true,
"noEmit": true,
"strict": false,
"skipLibCheck": true,
"types": ["node"]
},
"include": ["src/**/*.ts", "test/**/*.ts", "scripts/**/*.ts"]
}