The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | // `package.json` reading shared by the release scripts. |
| 2 | import * as Data from "effect/Data"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Path from "effect/Path"; |
| 6 | import * as Schema from "effect/Schema"; |
| 7 | |
| 8 | /** The `package.json` fields the release scripts read. */ |
| 9 | export const PackageJson = Schema.Struct({ |
| 10 | name: Schema.String, |
| 11 | version: Schema.String, |
| 12 | private: Schema.optionalKey(Schema.Boolean), |
| 13 | }); |
| 14 | export type PackageJson = typeof PackageJson.Type; |
| 15 | |
| 16 | /** A whole `package.json`, kept as-is so it can be written back unchanged. */ |
| 17 | const RawPackageJson = Schema.Record(Schema.String, Schema.Unknown); |
| 18 | |
| 19 | export const decodePackageJson = Schema.decodeUnknownEffect(Schema.fromJsonString(PackageJson)); |
| 20 | const decodeRaw = Schema.decodeUnknownEffect(Schema.fromJsonString(RawPackageJson)); |
| 21 | |
| 22 | export interface WorkspacePackage { |
| 23 | /** Directory relative to the workspace root, e.g. `packages/alchemy`. */ |
| 24 | readonly dir: string; |
| 25 | readonly manifest: PackageJson; |
| 26 | /** Every field of the manifest, for checks and rewrites. */ |
| 27 | readonly raw: Readonly<Record<string, unknown>>; |
| 28 | } |
| 29 | |
| 30 | /** Non-private packages directly under `directory` (relative to `root`). */ |
| 31 | export const publishablePackages = Effect.fn(function* (root: string, directory: string) { |
| 32 | const fs = yield* FileSystem.FileSystem; |
| 33 | const path = yield* Path.Path; |
| 34 | const entries = (yield* fs.readDirectory(path.join(root, directory))).sort(); |
| 35 | const packages = yield* Effect.forEach(entries, (entry) => |
| 36 | Effect.gen(function* () { |
| 37 | // Skip stray files such as `.DS_Store`. |
| 38 | if ((yield* fs.stat(path.join(root, directory, entry))).type !== "Directory") |
| 39 | return undefined; |
| 40 | const manifestPath = path.join(root, directory, entry, "package.json"); |
| 41 | if (!(yield* fs.exists(manifestPath))) return undefined; |
| 42 | const text = yield* fs.readFileString(manifestPath); |
| 43 | return { |
| 44 | dir: path.join(directory, entry), |
| 45 | manifest: yield* decodePackageJson(text), |
| 46 | raw: yield* decodeRaw(text), |
| 47 | } satisfies WorkspacePackage; |
| 48 | }), |
| 49 | ); |
| 50 | return packages.filter( |
| 51 | (pkg): pkg is WorkspacePackage => pkg !== undefined && pkg.manifest.private !== true, |
| 52 | ); |
| 53 | }); |
| 54 | |
| 55 | export class VersionMismatch extends Data.TaggedError("VersionMismatch")<{ |
| 56 | readonly message: string; |
| 57 | readonly specs: ReadonlyArray<string>; |
| 58 | }> {} |
| 59 | |
| 60 | /** The one version every package carries; they are released in lockstep. */ |
| 61 | export const sharedVersion = Effect.fn(function* (packages: ReadonlyArray<WorkspacePackage>) { |
| 62 | const versions = new Set(packages.map(({ manifest }) => manifest.version)); |
| 63 | if (versions.size !== 1) { |
| 64 | const specs = packages.map(({ manifest }) => `${manifest.name}@${manifest.version}`); |
| 65 | // Name the packages on each version, except where that is most of them. |
| 66 | const groups = [...versions].map((version) => { |
| 67 | const names = packages |
| 68 | .filter(({ manifest }) => manifest.version === version) |
| 69 | .map(({ manifest }) => manifest.name); |
| 70 | return `${version}: ${names.length > 5 ? `${names.length} packages` : names.join(", ")}`; |
| 71 | }); |
| 72 | return yield* new VersionMismatch({ |
| 73 | message: `Packages must share one version; found ${groups.join("; ")}`, |
| 74 | specs, |
| 75 | }); |
| 76 | } |
| 77 | return [...versions][0]!; |
| 78 | }); |