xonecas.io
70160
Ce

cel-tui / files

cel-tui / scripts / prepublish-check.ts

3.6 KB
1#!/usr/bin/env bun
2/**
3 * Pre-publish verification script.
4 *
5 * Checks that:
6 * 1. All workspace package.json versions match each other
7 * 2. bun.lock workspace versions match package.json versions
8 * 3. Internal workspace dependencies point at the current workspace packages
9 *
10 * Run before publishing: bun run scripts/prepublish-check.ts
11 */
12
13import { readFileSync } from "node:fs";
14import { resolve } from "node:path";
15
16const ROOT = resolve(import.meta.dirname, "..");
17const PACKAGES = ["types", "core", "clew", "components"] as const;
18
19type PackageDir = (typeof PACKAGES)[number];
20type PackageJson = {
21  dependencies?: Record<string, string>;
22  name: string;
23  version: string;
24};
25
26let failed = false;
27
28function fail(msg: string) {
29  console.error(`❌ ${msg}`);
30  failed = true;
31}
32
33function pass(msg: string) {
34  console.log(`✅ ${msg}`);
35}
36
37const packageJsonByDir = {} as Record<PackageDir, PackageJson>;
38const packageNames = {} as Record<PackageDir, string>;
39const packageVersions = {} as Record<PackageDir, string>;
40
41for (const pkg of PACKAGES) {
42  const pkgJson = JSON.parse(
43    readFileSync(resolve(ROOT, `packages/${pkg}/package.json`), "utf-8"),
44  ) as PackageJson;
45  packageJsonByDir[pkg] = pkgJson;
46  packageNames[pkg] = pkgJson.name;
47  packageVersions[pkg] = pkgJson.version;
48}
49
50console.log("\n--- Package versions (package.json) ---");
51for (const pkg of PACKAGES) {
52  console.log(`  ${packageNames[pkg]}: ${packageVersions[pkg]}`);
53}
54
55const versions = new Set(Object.values(packageVersions));
56if (versions.size === 1) {
57  pass(`All package.json versions match: ${[...versions][0]}`);
58} else {
59  fail(`Package versions don't match: ${JSON.stringify(packageVersions)}`);
60}
61
62const lockContent = readFileSync(resolve(ROOT, "bun.lock"), "utf-8");
63
64console.log("\n--- Lockfile workspace versions ---");
65for (const pkg of PACKAGES) {
66  const pattern = new RegExp(
67    `"packages/${pkg}":\\s*\\{[^}]*"version":\\s*"([^"]+)"`,
68    "s",
69  );
70  const match = lockContent.match(pattern);
71  if (!match) {
72    fail(`Could not find packages/${pkg} version in bun.lock`);
73    continue;
74  }
75
76  const lockVersion = match[1];
77  console.log(
78    `  ${packageNames[pkg]}: ${lockVersion} (lockfile) vs ${packageVersions[pkg]} (package.json)`,
79  );
80  if (lockVersion === packageVersions[pkg]) {
81    pass(`${packageNames[pkg]} lockfile version matches package.json`);
82  } else {
83    fail(
84      `${packageNames[pkg]} lockfile version MISMATCH: bun.lock has ${lockVersion}, package.json has ${packageVersions[pkg]}. Run: rm bun.lock && bun install`,
85    );
86  }
87}
88
89console.log("\n--- Workspace dependency checks ---");
90const packageDirByName = new Map<string, PackageDir>();
91for (const pkg of PACKAGES) {
92  packageDirByName.set(packageNames[pkg], pkg);
93}
94
95for (const pkg of PACKAGES) {
96  const deps = packageJsonByDir[pkg].dependencies ?? {};
97
98  for (const [dep, value] of Object.entries(deps)) {
99    const targetPkg = packageDirByName.get(dep);
100    if (!targetPkg) {
101      continue;
102    }
103
104    if (value === "workspace:*") {
105      pass(
106        `${packageNames[pkg]} → ${dep} uses workspace:* (will resolve from lockfile)`,
107      );
108      continue;
109    }
110
111    if (value === packageVersions[targetPkg]) {
112      pass(
113        `${packageNames[pkg]} → ${dep} pinned to ${value} (matches current version)`,
114      );
115      continue;
116    }
117
118    fail(
119      `${packageNames[pkg]} → ${dep} is ${value} but current version is ${packageVersions[targetPkg]}`,
120    );
121  }
122}
123
124console.log("");
125if (failed) {
126  console.error(
127    "🚫 Pre-publish checks FAILED. Fix the issues above before publishing.",
128  );
129  process.exit(1);
130} else {
131  console.log("🎉 All pre-publish checks passed. Safe to publish.");
132}