Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Enforce shared JavaScript formatting for repository checks

Manav Rathia45d2d117e3b
diff --git a/.github/checks/format-js/check.mjs b/.github/checks/format-js/check.mjs
new file mode 100644
index 0000000000..88d7c6660c
--- /dev/null
+++ b/.github/checks/format-js/check.mjs
@@ -0,0 +1,44 @@
+import { spawnSync } from "node:child_process";
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join, resolve } from "node:path";
+
+const root = resolve(import.meta.dirname, "../../..");
+const lock = readJSON("web/package-lock.json");
+const version = lock.packages["node_modules/prettier"].version;
+const { plugins, ...options } = readJSON("web/.prettierrc.json");
+const write = process.argv.includes("--write");
+const temporary = mkdtempSync(join(tmpdir(), "ente-prettier-"));
+
+try {
+ const config = join(temporary, "prettier.json");
+ writeFileSync(config, JSON.stringify(options));
+ const { error, status } = spawnSync(
+ "npm",
+ [
+ "exec",
+ "--yes",
+ "--ignore-scripts",
+ "--prefer-offline",
+ `--package=prettier@${version}`,
+ "--",
+ "prettier",
+ "--config",
+ config,
+ write ? "--write" : "--check",
+ ".github/{checks,scripts}/**/*.{js,cjs,mjs}",
+ "**/checks/**/*.{js,cjs,mjs}",
+ ],
+ { cwd: root, stdio: "inherit" },
+ );
+ if (error) throw error;
+ if (status === 1)
+ console.error("Fix: node .github/checks/format-js/check.mjs --write");
+ process.exitCode = status ?? 1;
+} finally {
+ rmSync(temporary, { recursive: true });
+}
+
+function readJSON(path) {
+ return JSON.parse(readFileSync(resolve(root, path), "utf8"));
+}
diff --git a/.github/checks/format-js/test.mjs b/.github/checks/format-js/test.mjs
new file mode 100644
index 0000000000..a3f32040ff
--- /dev/null
+++ b/.github/checks/format-js/test.mjs
@@ -0,0 +1,70 @@
+import assert from "node:assert/strict";
+import { spawnSync } from "node:child_process";
+import {
+ copyFileSync,
+ mkdirSync,
+ mkdtempSync,
+ readFileSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
+import { tmpdir } from "node:os";
+import { dirname, join, resolve } from "node:path";
+import test from "node:test";
+
+test("JS checks share web formatting across repository directories", (t) => {
+ const root = mkdtempSync(join(tmpdir(), "ente-format-js-"));
+ t.after(() => rmSync(root, { recursive: true }));
+ const repository = resolve(import.meta.dirname, "../../..");
+ const script = ".github/checks/format-js/check.mjs";
+ for (const path of [
+ script,
+ "web/package-lock.json",
+ "web/.prettierrc.json",
+ ]) {
+ mkdirSync(dirname(join(root, path)), { recursive: true });
+ copyFileSync(join(repository, path), join(root, path));
+ }
+ const checked = [
+ ".github/checks/example/check.mjs",
+ ".github/scripts/nested/example.cjs",
+ "rust/checks/example/check.js",
+ "nested/project/checks/example/test.mjs",
+ ];
+ const ignored = [
+ "node_modules/example/checks/test.js",
+ "web/apps/example.js",
+ "rust/checks/example/test.py",
+ ];
+ const source = "function example(){return true}\n";
+ for (const path of [...checked, ...ignored]) {
+ mkdirSync(dirname(join(root, path)), { recursive: true });
+ writeFileSync(join(root, path), source);
+ }
+ const run = (...args) =>
+ spawnSync(process.execPath, [join(root, script), ...args], {
+ cwd: join(root, "nested"),
+ encoding: "utf8",
+ });
+
+ let result = run();
+ assert.equal(result.status, 1, result.stdout + result.stderr);
+ for (const path of checked) {
+ assert.ok((result.stdout + result.stderr).includes(path), path);
+ assert.equal(readFileSync(join(root, path), "utf8"), source);
+ }
+ result = run("--write");
+ assert.equal(result.status, 0, result.stdout + result.stderr);
+ for (const path of checked) {
+ assert.equal(
+ readFileSync(join(root, path), "utf8"),
+ "function example() {\n return true;\n}\n",
+ );
+ }
+ for (const path of ignored) {
+ assert.equal(readFileSync(join(root, path), "utf8"), source);
+ }
+ result = run();
+ assert.equal(result.status, 0, result.stdout + result.stderr);
+ assert.match(result.stdout, /All matched files use Prettier code style!/);
+});
diff --git a/.github/checks/run.sh b/.github/checks/run.sh
index a1a7f7be25..f578694780 100755
--- a/.github/checks/run.sh
+++ b/.github/checks/run.sh
@@ -3,6 +3,8 @@ set -euo pipefail
cd "$(dirname "$0")/../.."
+node .github/checks/format-js/check.mjs
+node --test .github/checks/format-js/test.mjs
node .github/checks/documentation-comments/check.mjs
node .github/checks/documentation-comments/test.mjs
ruby .github/checks/workflow-paths/check.rb
diff --git a/.github/workflows/repo-lint.yml b/.github/workflows/repo-lint.yml
index f4232e4a22..fa64c309c7 100644
--- a/.github/workflows/repo-lint.yml
+++ b/.github/workflows/repo-lint.yml
@@ -13,4 +13,11 @@ jobs:
- name: Checkout
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
+ - name: Setup node
+ uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
+ with:
+ node-version: 24
+ cache: "npm"
+ cache-dependency-path: "web/package-lock.json"
+
- run: .github/checks/run.sh