From 6eba9c5403c619e8b9ae7fa624ca937837ae7083 Mon Sep 17 00:00:00 2001 From: Camillo Bruni Date: Thu, 11 Jun 2026 18:07:44 +0200 Subject: [PATCH 1/5] formatting --- package.json | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index a855732d5..7182ab59d 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "lint:fix": "eslint \"**/*.{js,mjs,jsx,ts,tsx}\" --fix", "pretty:check": "prettier --check ./", "pretty:fix": "prettier --write ./", - "format": "npm run pretty:fix && npm run lint:fix", + "format": "node tests/format.mjs", + "format:all": "node tests/format.mjs --all", "test:chrome": "node tests/run-unittests.mjs --browser chrome", "test:firefox": "node tests/run-unittests.mjs --browser firefox", "test:safari": "node tests/run-unittests.mjs --browser safari", @@ -46,16 +47,16 @@ "eslint-plugin-react": "^7.32.2", "eslint-plugin-vue": "^9.10.0", "expect.js": "^0.3.1", - "mocha": "^10.2.0", - "prettier": "^2.8.3", - "selenium-webdriver": "^4.27.0", - "sinon": "^17.0.1", - "typescript": "^5.0.4", "lws": "^4.2.0", "lws-cors": "^4.2.1", "lws-index": "^3.1.1", "lws-log": "^3.0.0", "lws-static": "^3.1.1", - "prettier-plugin-tailwindcss": "^0.4.1" + "mocha": "^10.2.0", + "prettier": "^2.8.3", + "prettier-plugin-tailwindcss": "^0.4.1", + "selenium-webdriver": "^4.27.0", + "sinon": "^17.0.1", + "typescript": "^5.0.4" } } From 119b6d917751b16e7d08d5c298e5ca23dd23d8f4 Mon Sep 17 00:00:00 2001 From: Camillo Bruni Date: Thu, 11 Jun 2026 18:08:14 +0200 Subject: [PATCH 2/5] fix --- tests/format.mjs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/format.mjs diff --git a/tests/format.mjs b/tests/format.mjs new file mode 100644 index 000000000..edcc608b0 --- /dev/null +++ b/tests/format.mjs @@ -0,0 +1,72 @@ +import { execFileSync } from "child_process"; +import fs from "fs"; + +function formatAll() { + console.log("Formatting all files..."); + execFileSync("npm", ["run", "pretty:fix"], { stdio: "inherit" }); + execFileSync("npm", ["run", "lint:fix"], { stdio: "inherit" }); +} + +function runInBatches(command, args, files, batchSize = 100) { + for (let i = 0; i < files.length; i += batchSize) { + const batch = files.slice(i, i + batchSize); + execFileSync("npx", [command, ...args, ...batch], { stdio: "inherit" }); + } +} + +function runPrettier(files) { + try { + console.log(`Running prettier on ${files.length} file(s)`); + runInBatches("prettier", ["--write", "--ignore-unknown"], files); + } catch (e) { + console.error("Prettier formatting failed"); + process.exit(1); + } +} + +function runEslint(files) { + const jsTsFiles = files.filter((f) => /\.(js|mjs|jsx|ts|tsx)$/.test(f)); + if (jsTsFiles.length === 0) + return; + try { + console.log(`Running eslint on ${jsTsFiles.length} file(s)`); + runInBatches("eslint", ["--fix"], jsTsFiles); + } catch (e) { + console.error("ESLint formatting failed"); + process.exit(1); + } +} + +function getChangedFiles() { + const diffOut = execFileSync("git", ["diff", "--name-only", "--diff-filter=ACMR", "@{upstream}"], { encoding: "utf8" }); + const files = diffOut + .split("\n") + .map((f) => f.trim()) + .filter((f) => f.length > 0 && fs.existsSync(f)); + return [...new Set(files)]; +} + +const isAll = process.argv.includes("--all"); + +if (isAll) { + formatAll(); + process.exit(0); +} + +let changedFiles = []; +try { + changedFiles = getChangedFiles(); +} catch (e) { + console.error("Failed to get changed files from git. Falling back to formatting all files."); + formatAll(); + process.exit(0); +} + +if (changedFiles.length === 0) { + console.log("No files changed compared to upstream."); + process.exit(0); +} +console.log(`Formatting ${changedFiles.length} changed files compared to upstream`); + +runPrettier(changedFiles); +runEslint(changedFiles); From 84456652f5c4243a7c3f5d58b114e48a82efc85d Mon Sep 17 00:00:00 2001 From: Camillo Bruni Date: Thu, 11 Jun 2026 18:11:26 +0200 Subject: [PATCH 3/5] fix --- tests/format.mjs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/tests/format.mjs b/tests/format.mjs index edcc608b0..b04247573 100644 --- a/tests/format.mjs +++ b/tests/format.mjs @@ -1,4 +1,5 @@ import { execFileSync } from "child_process"; +import { parseArgs } from "util"; import fs from "fs"; function formatAll() { @@ -26,8 +27,7 @@ function runPrettier(files) { function runEslint(files) { const jsTsFiles = files.filter((f) => /\.(js|mjs|jsx|ts|tsx)$/.test(f)); - if (jsTsFiles.length === 0) - return; + if (jsTsFiles.length === 0) return; try { console.log(`Running eslint on ${jsTsFiles.length} file(s)`); runInBatches("eslint", ["--fix"], jsTsFiles); @@ -46,9 +46,31 @@ function getChangedFiles() { return [...new Set(files)]; } -const isAll = process.argv.includes("--all"); +let values; +try { + ({ values } = parseArgs({ + options: { + all: { type: "boolean" }, + help: { type: "boolean", short: "h" }, + }, + strict: true, + })); +} catch (err) { + console.error(err.message); + console.error("Run 'node tests/format.mjs --help' for usage."); + process.exit(1); +} + +if (values.help) { + console.log(`Usage: node tests/format.mjs [options] + +Options: + --all Format all files across the repository instead of just changed files + -h, --help Show this help message`); + process.exit(0); +} -if (isAll) { +if (values.all) { formatAll(); process.exit(0); } From 37c22ac6a7b91a2b65f2a69906af44ec8cae5779 Mon Sep 17 00:00:00 2001 From: Camillo Bruni Date: Thu, 11 Jun 2026 18:18:36 +0200 Subject: [PATCH 4/5] pre-format --- tests/format.mjs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/format.mjs b/tests/format.mjs index b04247573..c015d380e 100644 --- a/tests/format.mjs +++ b/tests/format.mjs @@ -8,7 +8,8 @@ function formatAll() { execFileSync("npm", ["run", "lint:fix"], { stdio: "inherit" }); } -function runInBatches(command, args, files, batchSize = 100) { +// Chunk files into batches to avoid "Argument list too long" errors on large changelists. +function execFileSyncInBatches(command, args, files, batchSize = 100) { for (let i = 0; i < files.length; i += batchSize) { const batch = files.slice(i, i + batchSize); execFileSync("npx", [command, ...args, ...batch], { stdio: "inherit" }); @@ -18,7 +19,7 @@ function runInBatches(command, args, files, batchSize = 100) { function runPrettier(files) { try { console.log(`Running prettier on ${files.length} file(s)`); - runInBatches("prettier", ["--write", "--ignore-unknown"], files); + execFileSyncInBatches("prettier", ["--write", "--ignore-unknown"], files); } catch (e) { console.error("Prettier formatting failed"); process.exit(1); @@ -30,7 +31,7 @@ function runEslint(files) { if (jsTsFiles.length === 0) return; try { console.log(`Running eslint on ${jsTsFiles.length} file(s)`); - runInBatches("eslint", ["--fix"], jsTsFiles); + execFileSyncInBatches("eslint", ["--fix"], jsTsFiles); } catch (e) { console.error("ESLint formatting failed"); process.exit(1); @@ -38,6 +39,7 @@ function runEslint(files) { } function getChangedFiles() { + // "--diff-filter=ACMR" => ignore deleted files. const diffOut = execFileSync("git", ["diff", "--name-only", "--diff-filter=ACMR", "@{upstream}"], { encoding: "utf8" }); const files = diffOut .split("\n") From 98604dd711c8126b2b119ff2622810963d962876 Mon Sep 17 00:00:00 2001 From: Camillo Bruni Date: Thu, 11 Jun 2026 18:22:30 +0200 Subject: [PATCH 5/5] format --- tests/format.mjs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/format.mjs b/tests/format.mjs index c015d380e..82b0b1004 100644 --- a/tests/format.mjs +++ b/tests/format.mjs @@ -28,7 +28,8 @@ function runPrettier(files) { function runEslint(files) { const jsTsFiles = files.filter((f) => /\.(js|mjs|jsx|ts|tsx)$/.test(f)); - if (jsTsFiles.length === 0) return; + if (jsTsFiles.length === 0) + return; try { console.log(`Running eslint on ${jsTsFiles.length} file(s)`); execFileSyncInBatches("eslint", ["--fix"], jsTsFiles);