Skip to content

Commit 107eef2

Browse files
committed
replace shell scripts with node
1 parent 361d2cd commit 107eef2

8 files changed

Lines changed: 315 additions & 121 deletions

File tree

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@
2323
"scripts": {
2424
"build": "tsup",
2525
"dev": "tsup --watch",
26-
"clean": "rm -rf dist",
26+
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
2727
"typecheck": "tsc --noEmit",
28-
"pretest": "test -d tests/data || ./scripts/sync-fixtures.sh",
28+
"pretest": "node scripts/sync.mjs --if-missing fixtures",
2929
"test": "vitest run",
3030
"test:watch": "vitest",
3131
"test:coverage": "vitest run --coverage",
3232
"format": "prettier --write .",
3333
"format:check": "prettier --check .",
34-
"prepare": "test -d tests/data || ./scripts/sync-fixtures.sh && test -d flatbuffers || ./scripts/sync-flatbuffers.sh",
35-
"sync:fixtures": "./scripts/sync-fixtures.sh",
36-
"sync:fbs": "./scripts/sync-flatbuffers.sh",
37-
"generate:fbs": "FLATC=\"$(./scripts/ensure-flatc.sh)\" && \"$FLATC\" -T -o ./src/format/flatbuffers --gen-all ./flatbuffers/all.fbs && node scripts/postgen-fbs.cjs ./src/format/flatbuffers && prettier --write src/format/flatbuffers/generated",
38-
"check:fbs": "rm -rf /tmp/fbs-check && FLATC=\"$(./scripts/ensure-flatc.sh)\" && \"$FLATC\" -T -o /tmp/fbs-check --gen-all ./flatbuffers/all.fbs && node scripts/postgen-fbs.cjs /tmp/fbs-check && prettier --write /tmp/fbs-check/generated && diff -r /tmp/fbs-check/generated src/format/flatbuffers/generated"
34+
"prepare": "node scripts/sync.mjs --if-missing fixtures flatbuffers",
35+
"sync:fixtures": "node scripts/sync.mjs fixtures",
36+
"sync:fbs": "node scripts/sync.mjs flatbuffers",
37+
"generate:fbs": "node scripts/generate-fbs.mjs",
38+
"check:fbs": "node scripts/check-fbs.mjs"
3939
},
4040
"keywords": [
4141
"icechunk",

scripts/check-fbs.mjs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Check that generated FlatBuffer TypeScript is up to date.
4+
* Generates into a temp directory and diffs against the committed files.
5+
*
6+
* Usage:
7+
* node scripts/check-fbs.mjs
8+
*/
9+
import { execSync } from "node:child_process";
10+
import { mkdtempSync, rmSync, readdirSync, readFileSync } from "node:fs";
11+
import { join, dirname } from "node:path";
12+
import { tmpdir } from "node:os";
13+
import { fileURLToPath } from "node:url";
14+
15+
const __dirname = dirname(fileURLToPath(import.meta.url));
16+
const PROJECT_DIR = dirname(__dirname);
17+
18+
const tmp = mkdtempSync(join(tmpdir(), "fbs-check-"));
19+
20+
try {
21+
const flatc = execSync("node scripts/ensure-flatc.mjs", {
22+
cwd: PROJECT_DIR,
23+
encoding: "utf8",
24+
}).trim();
25+
26+
execSync(`"${flatc}" -T -o "${tmp}" --gen-all ./flatbuffers/all.fbs`, {
27+
stdio: "inherit",
28+
cwd: PROJECT_DIR,
29+
});
30+
31+
execSync(`node scripts/postgen-fbs.cjs "${tmp}"`, {
32+
stdio: "inherit",
33+
cwd: PROJECT_DIR,
34+
});
35+
36+
execSync(`npx prettier --write "${join(tmp, "generated")}"`, {
37+
stdio: "inherit",
38+
cwd: PROJECT_DIR,
39+
});
40+
41+
// Compare generated files
42+
const generatedDir = join(tmp, "generated");
43+
const committedDir = join(
44+
PROJECT_DIR,
45+
"src",
46+
"format",
47+
"flatbuffers",
48+
"generated",
49+
);
50+
51+
const genFiles = readdirSync(generatedDir).sort();
52+
const comFiles = readdirSync(committedDir).sort();
53+
54+
if (genFiles.join(",") !== comFiles.join(",")) {
55+
console.error("File list mismatch:");
56+
console.error(" generated:", genFiles.join(", "));
57+
console.error(" committed:", comFiles.join(", "));
58+
process.exit(1);
59+
}
60+
61+
let dirty = false;
62+
for (const file of genFiles) {
63+
const a = readFileSync(join(generatedDir, file), "utf8");
64+
const b = readFileSync(join(committedDir, file), "utf8");
65+
if (a !== b) {
66+
console.error(`DIFF: ${file}`);
67+
dirty = true;
68+
}
69+
}
70+
71+
if (dirty) {
72+
console.error(
73+
"Generated FlatBuffer code is out of date. Run: npm run generate:fbs",
74+
);
75+
process.exit(1);
76+
}
77+
78+
console.log("FlatBuffer generated code is up to date.");
79+
} finally {
80+
rmSync(tmp, { recursive: true, force: true });
81+
}

scripts/ensure-flatc.mjs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Ensure the pinned flatc version is available at .bin/flatc (or .bin/flatc.exe on Windows).
4+
* Downloads it if missing or wrong version. Prints the path on stdout.
5+
*
6+
* Usage:
7+
* node scripts/ensure-flatc.mjs
8+
* # returns the path to the flatc binary
9+
*/
10+
import { execSync } from "node:child_process";
11+
import { existsSync, mkdirSync, chmodSync, rmSync, mkdtempSync } from "node:fs";
12+
import { join, dirname } from "node:path";
13+
import { tmpdir, platform } from "node:os";
14+
import { fileURLToPath } from "node:url";
15+
16+
const FLATC_VERSION = "25.12.19";
17+
18+
const __dirname = dirname(fileURLToPath(import.meta.url));
19+
const PROJECT_DIR = dirname(__dirname);
20+
const BIN_DIR = join(PROJECT_DIR, ".bin");
21+
const isWindows = platform() === "win32";
22+
const FLATC = join(BIN_DIR, isWindows ? "flatc.exe" : "flatc");
23+
24+
// Check if we already have the right version
25+
if (existsSync(FLATC)) {
26+
try {
27+
const out = execSync(`"${FLATC}" --version`, { encoding: "utf8" });
28+
const match = out.match(/(\d+\.\d+\.\d+)/);
29+
if (match && match[1] === FLATC_VERSION) {
30+
process.stdout.write(FLATC);
31+
process.exit(0);
32+
}
33+
console.error(`flatc in .bin/ is v${match?.[1]}, need v${FLATC_VERSION}`);
34+
} catch {
35+
// binary exists but can't run — re-download
36+
}
37+
}
38+
39+
// Determine asset name
40+
let asset;
41+
switch (platform()) {
42+
case "linux":
43+
asset = "Linux.flatc.binary.clang++-18.zip";
44+
break;
45+
case "darwin":
46+
asset = "Mac.flatc.binary.zip";
47+
break;
48+
case "win32":
49+
asset = "Windows.flatc.binary.zip";
50+
break;
51+
default:
52+
console.error(`error: cannot download flatc for ${platform()}`);
53+
console.error(`Install flatc v${FLATC_VERSION} to .bin/flatc manually.`);
54+
process.exit(1);
55+
}
56+
57+
mkdirSync(BIN_DIR, { recursive: true });
58+
59+
const url = `https://github.com/google/flatbuffers/releases/download/v${FLATC_VERSION}/${asset}`;
60+
const tmp = mkdtempSync(join(tmpdir(), "flatc-"));
61+
const zipPath = join(tmp, "flatc.zip");
62+
63+
try {
64+
console.error(`Downloading flatc v${FLATC_VERSION}...`);
65+
66+
// Use curl on unix, PowerShell on Windows
67+
if (isWindows) {
68+
execSync(
69+
`powershell -Command "Invoke-WebRequest -Uri '${url}' -OutFile '${zipPath}'"`,
70+
{ stdio: ["pipe", "pipe", "inherit"] },
71+
);
72+
execSync(
73+
`powershell -Command "Expand-Archive -Path '${zipPath}' -DestinationPath '${BIN_DIR}' -Force"`,
74+
{ stdio: ["pipe", "pipe", "inherit"] },
75+
);
76+
} else {
77+
execSync(`curl -fsSL "${url}" -o "${zipPath}"`, {
78+
stdio: ["pipe", "pipe", "inherit"],
79+
});
80+
execSync(`unzip -o -q "${zipPath}" -d "${BIN_DIR}/"`, {
81+
stdio: ["pipe", "pipe", "inherit"],
82+
});
83+
chmodSync(FLATC, 0o755);
84+
}
85+
86+
process.stdout.write(FLATC);
87+
} finally {
88+
rmSync(tmp, { recursive: true, force: true });
89+
}

scripts/ensure-flatc.sh

Lines changed: 0 additions & 50 deletions
This file was deleted.

scripts/generate-fbs.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Generate TypeScript from FlatBuffer schemas using the pinned flatc binary.
4+
*
5+
* Usage:
6+
* node scripts/generate-fbs.mjs
7+
*/
8+
import { execSync } from "node:child_process";
9+
import { dirname } from "node:path";
10+
import { fileURLToPath } from "node:url";
11+
12+
const __dirname = dirname(fileURLToPath(import.meta.url));
13+
const PROJECT_DIR = dirname(__dirname);
14+
15+
// ensure-flatc prints the binary path to stdout
16+
const flatc = execSync("node scripts/ensure-flatc.mjs", {
17+
cwd: PROJECT_DIR,
18+
encoding: "utf8",
19+
}).trim();
20+
21+
execSync(
22+
`"${flatc}" -T -o ./src/format/flatbuffers --gen-all ./flatbuffers/all.fbs`,
23+
{ stdio: "inherit", cwd: PROJECT_DIR },
24+
);
25+
26+
execSync("node scripts/postgen-fbs.cjs ./src/format/flatbuffers", {
27+
stdio: "inherit",
28+
cwd: PROJECT_DIR,
29+
});
30+
31+
execSync("npx prettier --write src/format/flatbuffers/generated", {
32+
stdio: "inherit",
33+
cwd: PROJECT_DIR,
34+
});

scripts/sync-fixtures.sh

Lines changed: 0 additions & 32 deletions
This file was deleted.

scripts/sync-flatbuffers.sh

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)