From eeb49bdf29f79ad420a37e09af0c04cc4ac27513 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Fri, 13 Nov 2026 02:48:13 -0500 Subject: [PATCH 01/10] fix(cli): stop installing dprint-node with every CLI install ts-poet requires dprint-node (about 25 MB of platform binaries) at module load. Vendor ts-poet's printer as codegen-ts/src/poet.ts (Apache-2.0, licence kept) and format with the same dprint TypeScript plugin, 0.87.1, through its WebAssembly build (@dprint/typescript + @dprint/formatter). Output is byte-identical: every codegen golden and conformance fixture is unchanged. poet.ts is an engine module, so owned and ejected templates take Code from @metaobjectsdev/codegen-ts/engine and class identity holds. The config loader still maps a legacy bare ts-poet import to that entry. check-peer-ranges now refuses dprint-node, ts-poet and @types/* in a published package's dependencies or required peers unless allowlisted. No published package declared @types/*; @types/node arrives only through the libSQL driver, which is out of scope. Fresh CLI install: 151 MB (134,525,165 bytes) -> 133 MB (115,177,311 bytes). --- CHANGELOG.md | 18 + bun.lock | 11 +- docs/features/own-your-codegen.md | 7 +- .../registry.json | 1 + scripts/check-peer-ranges.ts | 78 ++- server/typescript/packages/cli/package.json | 1 + .../cli/src/lib/load-metaobjects-config.ts | 35 +- .../packages/cli/src/lib/template-eject.ts | 11 +- .../test/gen-split-tree-single-import.test.ts | 17 +- .../packages/cli/test/template-eject.test.ts | 7 +- .../packages/codegen-ts-angular/package.json | 3 +- .../src/templates/form-file.ts | 2 +- .../src/templates/grid-file.ts | 2 +- .../src/templates/service-file.ts | 2 +- .../packages/codegen-ts-react/package.json | 3 +- .../src/templates/form-file.ts | 2 +- .../packages/codegen-ts-tanstack/package.json | 3 +- .../src/templates/columns-file.ts | 2 +- .../src/templates/grid-hook-file.ts | 2 +- .../src/templates/hooks-file.ts | 2 +- .../packages/codegen-ts/LICENSE.ts-poet | 204 ++++++ .../packages/codegen-ts/package.json | 6 +- .../packages/codegen-ts/src/engine-surface.ts | 1 + .../packages/codegen-ts/src/engine.ts | 7 +- .../packages/codegen-ts/src/index.ts | 10 +- .../packages/codegen-ts/src/names.ts | 2 +- .../packages/codegen-ts/src/poet.ts | 652 ++++++++++++++++++ .../src/templates/drizzle-schema.ts | 2 +- .../src/templates/duration-wire-helper.ts | 2 +- .../src/templates/entity-constants.ts | 2 +- .../codegen-ts/src/templates/entity-file.ts | 2 +- .../src/templates/entity-meta-file.ts | 2 +- .../src/templates/enum-int-codec.ts | 2 +- .../codegen-ts/src/templates/enums-file.ts | 2 +- .../src/templates/filter-allowlist.ts | 2 +- .../codegen-ts/src/templates/filter-type.ts | 2 +- .../src/templates/inferred-types.ts | 2 +- .../src/templates/projection-decl.ts | 2 +- .../codegen-ts/src/templates/queries-file.ts | 2 +- .../codegen-ts/src/templates/queries.ts | 2 +- .../src/templates/relations-block.ts | 2 +- .../src/templates/routes-file-hono.ts | 2 +- .../codegen-ts/src/templates/routes-file.ts | 2 +- .../src/templates/tph-discriminator.ts | 2 +- .../src/templates/value-object-file.ts | 2 +- .../codegen-ts/src/templates/view-decl.ts | 2 +- .../src/templates/zod-validators.ts | 2 +- .../codegen-ts/test/names-flattened.test.ts | 2 +- 48 files changed, 1016 insertions(+), 117 deletions(-) create mode 100644 server/typescript/packages/codegen-ts/LICENSE.ts-poet create mode 100644 server/typescript/packages/codegen-ts/src/poet.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index f3a4d4e40b..84a81be64d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,24 @@ loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersi lint gate), and a fresh npm install of the CLI drops from 245 MB to 151 MB of `node_modules`. `scripts/check-peer-ranges.ts` now fails a publishable package that requires `@biomejs/biome` through `dependencies` or a non-optional peer. +- **`@metaobjectsdev/cli` no longer installs `dprint-node`.** The TypeScript generators printed + code with `ts-poet`, which loads `dprint-node` on import, and dprint-node ships all seven of + its platform binaries in one package (24 MB, of which a machine runs one). `codegen-ts` now + vendors the part of ts-poet the generators use (`src/poet.ts`, Apache-2.0, licence in + `LICENSE.ts-poet`) and runs the same dprint TypeScript plugin as WebAssembly + (`@dprint/typescript`, pinned to 0.87.1, the version dprint-node 1.0.8 compiles in, 5 MB) with + ts-poet's configuration, so generated code is byte-identical: every codegen golden and + conformance fixture is unchanged. `codegen-ts-react`, `codegen-ts-tanstack` and + `codegen-ts-angular` drop their `ts-poet` dependency and import the printer from + `@metaobjectsdev/codegen-ts/engine`, which an ejected template also uses; a bare `ts-poet` + import in an older owned generator is still pointed at that entry by `meta gen`. A fresh npm + install of the CLI drops from 151 MB (134,525,165 bytes) to 133 MB (115,177,311 bytes) of + `node_modules`. `scripts/check-peer-ranges.ts` now also fails a publishable package that + requires `dprint-node`, `ts-poet` or any `@types/*` package through `dependencies` or a + non-optional peer; a type package a published `.d.ts` imports goes in its `ALLOWED_TYPES_DEPS` + with the reason. No `@metaobjectsdev/*` package declares `@types/node`: the 3 MB copy in the + install comes through the libSQL driver (`@libsql/isomorphic-ws` → `@types/ws`), which is + unchanged here. ### Deprecated diff --git a/bun.lock b/bun.lock index 5f8e86e3c9..70203375a5 100644 --- a/bun.lock +++ b/bun.lock @@ -166,6 +166,7 @@ "pg": "^8.0.0", "pg-mem": "^3.0.4", "prisma": "7.10.1", + "ts-poet": "^6.10.0", "typescript": "^5.6.0", "zod": "^4.4.3", }, @@ -183,12 +184,13 @@ "dependencies": { "@biomejs/js-api": "^0.7.0", "@biomejs/wasm-nodejs": "^1.9.4", + "@dprint/formatter": "^0.6.0", + "@dprint/typescript": "0.87.1", "@metaobjectsdev/docs-site": "workspace:*", "@metaobjectsdev/metadata": "workspace:*", "@metaobjectsdev/migrate-ts": "workspace:*", "@metaobjectsdev/render": "workspace:*", "@toon-format/toon": "^2.3.0", - "ts-poet": "^6.10.0", }, "devDependencies": { "@biomejs/biome": "^1.9.0", @@ -214,7 +216,6 @@ "dependencies": { "@metaobjectsdev/codegen-ts": "workspace:*", "@metaobjectsdev/metadata": "workspace:*", - "ts-poet": "^6.10.0", }, "devDependencies": { "@biomejs/biome": "^1.9.0", @@ -228,7 +229,6 @@ "dependencies": { "@metaobjectsdev/codegen-ts": "workspace:*", "@metaobjectsdev/metadata": "workspace:*", - "ts-poet": "^6.10.0", }, "devDependencies": { "@biomejs/biome": "^1.9.0", @@ -243,7 +243,6 @@ "dependencies": { "@metaobjectsdev/codegen-ts": "workspace:*", "@metaobjectsdev/metadata": "workspace:*", - "ts-poet": "^6.10.0", }, "devDependencies": { "@biomejs/biome": "^1.9.0", @@ -572,6 +571,10 @@ "@csstools/css-tokenizer": ["@csstools/css-tokenizer@4.0.2", "", {}, "sha512-OoKoR0f76dCY666JlcbhmVTs2drYj1GUXZTYTcbUgJjh9Nv41aFfZ21bPQTERm5+L5cBDo466NltB2lplS5GBw=="], + "@dprint/formatter": ["@dprint/formatter@0.6.0", "", {}, "sha512-otnYh2jmGWbekYkAd4xiF416lpdZwNCbhkDrLQcAanvvutp0PhTNSJrwJ4DKBEu6xtP36NUX70t9YQw3oC/ONg=="], + + "@dprint/typescript": ["@dprint/typescript@0.87.1", "", {}, "sha512-XdbJCIbUdqijEgKOuFCSRwJAjHp5zUKZVzxrNn5xU9vn6Uy71JkvZT9Esw9hCEjno/vPmJ6IFhVGdoGlEzN2vQ=="], + "@electric-sql/pglite": ["@electric-sql/pglite@0.5.16", "", {}, "sha512-Nj8KcIsJL748W408U2N8q4+9IlONdpRIU8MJypIAAINMJ5nqKyqpX7Mgg/5XKkXiAib3AoZGR6hnJFlrlLBrcg=="], "@electric-sql/pglite-socket": ["@electric-sql/pglite-socket@0.1.3", "", { "peerDependencies": { "@electric-sql/pglite": "0.4.3" }, "bin": { "pglite-server": "dist/scripts/server.js" } }, "sha512-LAciWM0M1dCL8hlsxu2venbVZcdxema0BtDfpWYVqr+Y468UADw0pFWidhKw1M8sfJ8rdLT71tjMmnirf/IZRQ=="], diff --git a/docs/features/own-your-codegen.md b/docs/features/own-your-codegen.md index 9f2ef99357..f98ab492f5 100644 --- a/docs/features/own-your-codegen.md +++ b/docs/features/own-your-codegen.md @@ -108,7 +108,8 @@ holds), the naming tables and the type-mapping tables (`names.ts`, `naming.ts`, `column-mapper.ts`), the primary-key and TPH walks (`pk-resolver.ts`, `tph.ts`), and the projection and report lowering that `meta migrate` and `meta verify` run. An owned file imports the engine from the package's engine entry, `@metaobjectsdev/codegen-ts/engine`, -which also re-exports `ts-poet`'s `code`, `imp` and `joinCode`. The list is +which also exports the code printer's `code`, `imp` and `joinCode` (`poet.ts`, the package's +vendored copy of `ts-poet`). The list is `ENGINE_MODULES`, exported from that entry; a module that is not on it and that a generator reaches is copied. An owned generator therefore imports no packaged module that returns emitted text, and a test ejects each generator alone @@ -117,8 +118,8 @@ writes what the packaged generator writes, byte for byte. A copy differs from the packaged module in its **import lines only**. A relative import of an engine module becomes an import of `@metaobjectsdev/codegen-ts/engine`, a relative import of -another template module becomes the path to your copy of it, and `ts-poet` becomes the -engine's own (a copy must build `Code` with the same `ts-poet` the engine uses). Nothing else +another template module becomes the path to your copy of it, and the printer stays the +engine's (a copy must build `Code` with the same printer the engine uses). Nothing else is touched, so "what did upstream change?" is a diff of two files whose body is the same. - **Two generators that call one module share one copy.** `entity` and `queries` both call diff --git a/fixtures/generator-registry-conformance/registry.json b/fixtures/generator-registry-conformance/registry.json index f01caa68ef..1b1a0d2627 100644 --- a/fixtures/generator-registry-conformance/registry.json +++ b/fixtures/generator-registry-conformance/registry.json @@ -376,6 +376,7 @@ "codegen-ts/src/naming/name-claims.ts", "codegen-ts/src/owned-runtime.ts", "codegen-ts/src/pk-resolver.ts", + "codegen-ts/src/poet.ts", "codegen-ts/src/projection/build-projection-views.ts", "codegen-ts/src/projection/extract-view-spec.ts", "codegen-ts/src/projection/projection-detector.ts", diff --git a/scripts/check-peer-ranges.ts b/scripts/check-peer-ranges.ts index 5033d3f29d..45b10b4eaf 100755 --- a/scripts/check-peer-ranges.ts +++ b/scripts/check-peer-ranges.ts @@ -26,14 +26,23 @@ * it reads MANIFESTS, not `node_modules`, so it needs no network and cannot be fooled * by a warm lockfile. * - * Second rule, same walk: no publishable package may REQUIRE the native Biome - * formatter (`@biomejs/biome`, which pulls a ~94 MB per-platform `@biomejs/cli-*` - * binary) through `dependencies` or a non-optional peer. npm installs required peers - * automatically, so a single generator package declaring it put the binary into every - * `@metaobjectsdev/cli` install, though nothing at run time uses it: codegen formats - * through the WebAssembly build (`@biomejs/wasm-nodejs` + `@biomejs/js-api`, see - * `codegen-ts/src/format.ts`). Development use belongs in `devDependencies`; a genuine - * run-time need must be an optional peer (`peerDependenciesMeta`). + * Second rule, same walk: no publishable package may REQUIRE an install-time package + * that nothing uses at run time, through `dependencies` or a non-optional peer (npm + * installs required peers automatically, so one declaration lands it in every + * `@metaobjectsdev/cli` install): + * + * - the native Biome formatter (`@biomejs/biome`, which pulls a ~94 MB per-platform + * `@biomejs/cli-*` binary). Codegen formats through the WebAssembly build + * (`@biomejs/wasm-nodejs` + `@biomejs/js-api`, see `codegen-ts/src/format.ts`). + * - `dprint-node` (~25 MB: all seven platform binaries in one package) and `ts-poet`, + * which `require`s it on load. codegen-ts vendors the printer (`codegen-ts/src/poet.ts`) + * and runs dprint as WebAssembly (`@dprint/typescript`). + * - any `@types/*` package. Type definitions do nothing at run time; one belongs in + * `dependencies` only when a published `.d.ts` imports it, so a consumer's compile + * needs it — and then it goes in `ALLOWED_TYPES_DEPS` below, with that reason. + * + * Development use belongs in `devDependencies`; a genuine run-time need for the first two + * must be an optional peer (`peerDependenciesMeta`). */ import { readdirSync, readFileSync, existsSync } from "node:fs"; import { join } from "node:path"; @@ -44,13 +53,29 @@ const ROOTS = ["server/typescript/packages", "client/web/packages"]; /** A version no real release will reach. If the range still matches, it has no ceiling. */ const BEYOND_ANY_REAL_VERSION = "9999.0.0"; -/** The native formatter and its per-platform binaries. */ -const isNativeFormatter = (name: string): boolean => - name === "@biomejs/biome" || name.startsWith("@biomejs/cli-"); +/** Packages no publishable package may require at install time, with why. */ +function installOnly(name: string): string | undefined { + if (name === "@biomejs/biome" || name.startsWith("@biomejs/cli-")) { + return "the native Biome formatter (~94 MB binary); codegen formats through @biomejs/wasm-nodejs"; + } + if (name === "dprint-node" || name === "ts-poet") { + return "dprint-node's seven native binaries (~25 MB; ts-poet requires it on load); codegen-ts vendors the printer and runs dprint as WebAssembly"; + } + if (name.startsWith("@types/")) { + return "type definitions, unused at run time; list it in ALLOWED_TYPES_DEPS only if a published .d.ts imports it"; + } + return undefined; +} + +/** + * `@types/*` run-time dependencies that are deliberate: `" "` → why a + * consumer's compile needs it (a published `.d.ts` that imports it). Empty today. + */ +const ALLOWED_TYPES_DEPS: Record = {}; interface Violation { pkg: string; peer: string; range: string } const violations: Violation[] = []; -const nativeFormatter: { pkg: string; dep: string; field: string }[] = []; +const installOnlyDeps: { pkg: string; dep: string; field: string; why: string }[] = []; let checked = 0; for (const root of ROOTS) { @@ -64,13 +89,16 @@ for (const root of ROOTS) { peerDependenciesMeta?: Record; }; if (pkg.private === true) continue; // never published — its peers bind nobody - for (const dep of Object.keys(pkg.dependencies ?? {})) { - if (isNativeFormatter(dep)) nativeFormatter.push({ pkg: pkg.name, dep, field: "dependencies" }); - } - for (const dep of Object.keys(pkg.peerDependencies ?? {})) { - if (isNativeFormatter(dep) && pkg.peerDependenciesMeta?.[dep]?.optional !== true) { - nativeFormatter.push({ pkg: pkg.name, dep, field: "peerDependencies (required)" }); - } + const required = [ + ...Object.keys(pkg.dependencies ?? {}).map((dep) => ({ dep, field: "dependencies" })), + ...Object.keys(pkg.peerDependencies ?? {}) + .filter((dep) => pkg.peerDependenciesMeta?.[dep]?.optional !== true) + .map((dep) => ({ dep, field: "peerDependencies (required)" })), + ]; + for (const { dep, field } of required) { + const why = installOnly(dep); + if (why === undefined || ALLOWED_TYPES_DEPS[`${pkg.name} ${dep}`] !== undefined) continue; + installOnlyDeps.push({ pkg: pkg.name, dep, field, why }); } for (const [peer, range] of Object.entries(pkg.peerDependencies ?? {})) { checked++; @@ -96,13 +124,13 @@ if (violations.length > 0) { process.exit(1); } -if (nativeFormatter.length > 0) { - console.error("✖ native Biome formatter required at install time:\n"); - for (const v of nativeFormatter) console.error(` ${v.pkg}\n ${v.field}: "${v.dep}"`); +if (installOnlyDeps.length > 0) { + console.error("✖ a package nothing uses at run time is required at install time:\n"); + for (const v of installOnlyDeps) console.error(` ${v.pkg}\n ${v.field}: "${v.dep}" — ${v.why}`); console.error( - `\nnpm installs it (a ~94 MB native binary) into every adopter's tree, and codegen` + - `\nformats through @biomejs/wasm-nodejs instead. Move it to devDependencies, or, if` + - `\nshipped code really needs it, mark the peer optional in peerDependenciesMeta.\n`, + `\nnpm installs it into every adopter's tree. Move it to devDependencies, or, if shipped` + + `\ncode really needs it, mark the peer optional in peerDependenciesMeta (an @types/*` + + `\npackage a published .d.ts imports goes in ALLOWED_TYPES_DEPS instead).\n`, ); process.exit(1); } diff --git a/server/typescript/packages/cli/package.json b/server/typescript/packages/cli/package.json index bfeb8bcc5e..a112ff4acb 100644 --- a/server/typescript/packages/cli/package.json +++ b/server/typescript/packages/cli/package.json @@ -84,6 +84,7 @@ "pg": "^8.0.0", "pg-mem": "^3.0.4", "prisma": "7.10.1", + "ts-poet": "^6.10.0", "typescript": "^5.6.0", "zod": "^4.4.3" } diff --git a/server/typescript/packages/cli/src/lib/load-metaobjects-config.ts b/server/typescript/packages/cli/src/lib/load-metaobjects-config.ts index 938853d818..8eee582e84 100644 --- a/server/typescript/packages/cli/src/lib/load-metaobjects-config.ts +++ b/server/typescript/packages/cli/src/lib/load-metaobjects-config.ts @@ -44,6 +44,7 @@ const _require = createRequire(import.meta.url); // self-referential symlink). const CODEGEN_TS_PKG = "@metaobjectsdev/codegen-ts"; const TS_POET_PKG = "ts-poet"; +const CODEGEN_TS_ENGINE = `${CODEGEN_TS_PKG}/engine`; const CLI_PKG_PATHS: Record = { [CODEGEN_TS_PKG]: { dist: "node_modules/@metaobjectsdev/codegen-ts/dist/index.js", @@ -384,26 +385,6 @@ export async function loadMetaobjectsConfig(projectRoot: string): Promise "@metaobjectsdev/codegen-ts/engine" +// ./names.js, ../poet.js an ENGINE module -> "@metaobjectsdev/codegen-ts/engine" // ./zod-validators.js, ../cube/x.js an EMIT module -> the copy beside it // @metaobjectsdev/codegen-ts/templates/x another package's emit -> the copy beside it -// ts-poet the package's own Code -> "@metaobjectsdev/codegen-ts/engine" // -// The last row is the 0.21.6 split-tree defect: `Code` objects are recognised by `instanceof`, -// so a copy must build them with the same ts-poet the engine uses. +// `poet.ts` is the code printer, and it is engine for the 0.21.6 split-tree defect: `Code` +// objects are recognised by `instanceof`, so a copy must build them with the engine's printer. // // Imports are found by scanning the leading import block of each module — the only place these // files declare a module specifier. A scanner and not a regex over the whole file because the @@ -53,7 +52,7 @@ export const OWNED_TEMPLATES_DIR = "codegen/templates"; const OWNED_GENERATORS_DIR = "codegen/generators"; /** The package that holds the engine. */ const CORE_PACKAGE = `${SCOPE}codegen-ts`; -/** The entry an owned copy imports the core engine from, and the ts-poet it shares. */ +/** The entry an owned copy imports the core engine (code printer included) from. */ const ENGINE_ENTRY = `${CORE_PACKAGE}/engine`; /** `@metaobjectsdev/codegen-ts-react` → `codegen-ts-react`, the directory spelling `ENGINE_MODULES` uses. */ @@ -311,8 +310,6 @@ export function templateClosure( next = engine.has(keyOf(target)) ? engineEntryOf(target.pkg.name) : relativeSpecifier(here, destination(target)); - } else if (spec === "ts-poet") { - next = ENGINE_ENTRY; } if (!next.startsWith(".") && !next.startsWith("node:")) imported.add(packageOfSpecifier(next)); return next; diff --git a/server/typescript/packages/cli/test/gen-split-tree-single-import.test.ts b/server/typescript/packages/cli/test/gen-split-tree-single-import.test.ts index a9199f2351..0afb0bec66 100644 --- a/server/typescript/packages/cli/test/gen-split-tree-single-import.test.ts +++ b/server/typescript/packages/cli/test/gen-split-tree-single-import.test.ts @@ -19,10 +19,10 @@ // // THE FIX (two halves, one lane each below): // 1. templates — the reference generators import `code`/`joinCode` from -// @metaobjectsdev/codegen-ts (re-exported from ITS ts-poet instance), so every +// @metaobjectsdev/codegen-ts (re-exported from ITS printer), so every // cross-boundary Code shares one class identity by construction. -// 2. loader — loadMetaobjectsConfig aliases bare "ts-poet" to the copy adjacent to -// the resolved @metaobjectsdev/codegen-ts (completing the existing alias map), +// 2. loader — loadMetaobjectsConfig aliases bare "ts-poet" to the printer codegen-ts +// vendors (its `/engine` entry, completing the existing alias map), // repairing EXISTING scaffolded projects — whose owned generators still say // `from "ts-poet"` — without re-scaffolding. // @@ -86,13 +86,12 @@ const CONFIG = [ `});`, ].join("\n"); -/** The ts-poet copy that @metaobjectsdev/codegen-ts itself loads, plus its runtime - * dependency closure (dprint-node — required eagerly by ts-poet's Code.js — and - * dprint-node's detect-libc), so the planted copy is loadable in isolation. */ +/** A real ts-poet (this package's devDependency — codegen-ts vendors its printer and no + * longer depends on it) plus its runtime dependency closure (dprint-node — required + * eagerly by ts-poet's Code.js — and dprint-node's detect-libc), so the planted copy is + * loadable in isolation, exactly as an adopter's own ts-poet devDependency is. */ function tsPoetCopySources(): Array<{ pkg: string; dir: string }> { - const req = createRequire(import.meta.url); - const codegenTsPkg = req.resolve("@metaobjectsdev/codegen-ts/package.json"); - const tsPoetPkg = createRequire(codegenTsPkg).resolve("ts-poet/package.json"); + const tsPoetPkg = createRequire(import.meta.url).resolve("ts-poet/package.json"); const dprintPkg = createRequire(tsPoetPkg).resolve("dprint-node/package.json"); const detectLibcPkg = createRequire(dprintPkg).resolve("detect-libc/package.json"); return [ diff --git a/server/typescript/packages/cli/test/template-eject.test.ts b/server/typescript/packages/cli/test/template-eject.test.ts index 8e02e16f98..a1365b882a 100644 --- a/server/typescript/packages/cli/test/template-eject.test.ts +++ b/server/typescript/packages/cli/test/template-eject.test.ts @@ -122,10 +122,11 @@ const packages = (): PackageSource[] => [ { name: CORE, srcRoot: join(root, "core/src") }, { name: UI, srcRoot: join(root, "ui/src") }, ]; -const ENGINE = new Set(["codegen-ts/src/names.ts"]); +const ENGINE = new Set(["codegen-ts/src/names.ts", "codegen-ts/src/poet.ts"]); function fixture(): void { write("core/src/names.ts", "export const n = 1;\n"); + write("core/src/poet.ts", "export const code = 1;\n"); write( "core/src/reference/gen.ts", [ @@ -140,7 +141,7 @@ function fixture(): void { write( "core/src/templates/a.ts", [ - 'import { code } from "ts-poet";', + 'import { code } from "../poet.js";', 'import { n } from "../names.js";', 'import { b } from "./b.js";', "export const renderA = [code, n, b];", @@ -214,7 +215,7 @@ describe("templateClosure — what is copied, where, and what each copy imports" expect(files["codegen/generators/gen.ts"]).toBe( readFileSync(join(root, "core/src/reference/gen.ts"), "utf8").replace("../cube/c.js", "../templates/cube/c.js"), ); - // An engine module → the engine entry; ts-poet → the package's own, never a second copy; + // An engine module → the engine entry (the code printer too: never a second copy); // a sibling stays a sibling. expect(files["codegen/templates/a.ts"]).toBe( [ diff --git a/server/typescript/packages/codegen-ts-angular/package.json b/server/typescript/packages/codegen-ts-angular/package.json index 1d456ab319..416f7de048 100644 --- a/server/typescript/packages/codegen-ts-angular/package.json +++ b/server/typescript/packages/codegen-ts-angular/package.json @@ -47,8 +47,7 @@ }, "dependencies": { "@metaobjectsdev/metadata": "workspace:*", - "@metaobjectsdev/codegen-ts": "workspace:*", - "ts-poet": "^6.10.0" + "@metaobjectsdev/codegen-ts": "workspace:*" }, "devDependencies": { "@biomejs/biome": "^1.9.0", diff --git a/server/typescript/packages/codegen-ts-angular/src/templates/form-file.ts b/server/typescript/packages/codegen-ts-angular/src/templates/form-file.ts index 434235b9f8..6b431865fc 100644 --- a/server/typescript/packages/codegen-ts-angular/src/templates/form-file.ts +++ b/server/typescript/packages/codegen-ts-angular/src/templates/form-file.ts @@ -14,7 +14,7 @@ // (ERR_UNKNOWN_ATTR); the line here used to claim it turned generation ON, // while the factory beside it read it as an opt-OUT. Neither is true now. -import { code, imp } from "ts-poet"; +import { code, imp } from "@metaobjectsdev/codegen-ts"; import { MetaField, MetaObject } from "@metaobjectsdev/metadata"; import { IDENTITY_SUBTYPE_PRIMARY, diff --git a/server/typescript/packages/codegen-ts-angular/src/templates/grid-file.ts b/server/typescript/packages/codegen-ts-angular/src/templates/grid-file.ts index e550b9443f..88cda58e74 100644 --- a/server/typescript/packages/codegen-ts-angular/src/templates/grid-file.ts +++ b/server/typescript/packages/codegen-ts-angular/src/templates/grid-file.ts @@ -3,7 +3,7 @@ // metadata child. Mirrors the TanStack columns-file.ts shape (column id + // header + meta.view) but in Angular-native shape (EntityGridColumn[]). -import { code, imp } from "ts-poet"; +import { code, imp } from "@metaobjectsdev/codegen-ts"; import type { MetaField, MetaObject } from "@metaobjectsdev/metadata"; import { LAYOUT_SUBTYPE_DATA_GRID, diff --git a/server/typescript/packages/codegen-ts-angular/src/templates/service-file.ts b/server/typescript/packages/codegen-ts-angular/src/templates/service-file.ts index bc72742ff0..04c3e75ecd 100644 --- a/server/typescript/packages/codegen-ts-angular/src/templates/service-file.ts +++ b/server/typescript/packages/codegen-ts-angular/src/templates/service-file.ts @@ -11,7 +11,7 @@ // update(id, patch) → PATCH /api//:id // delete(id) → DELETE /api//:id -import { code, imp } from "ts-poet"; +import { code, imp } from "@metaobjectsdev/codegen-ts"; import type { MetaObject } from "@metaobjectsdev/metadata"; import { type RenderContext, diff --git a/server/typescript/packages/codegen-ts-react/package.json b/server/typescript/packages/codegen-ts-react/package.json index 57ab1b9b44..46b4a5da56 100644 --- a/server/typescript/packages/codegen-ts-react/package.json +++ b/server/typescript/packages/codegen-ts-react/package.json @@ -46,8 +46,7 @@ }, "dependencies": { "@metaobjectsdev/metadata": "workspace:*", - "@metaobjectsdev/codegen-ts": "workspace:*", - "ts-poet": "^6.10.0" + "@metaobjectsdev/codegen-ts": "workspace:*" }, "devDependencies": { "@biomejs/biome": "^1.9.0", diff --git a/server/typescript/packages/codegen-ts-react/src/templates/form-file.ts b/server/typescript/packages/codegen-ts-react/src/templates/form-file.ts index bd49fe392e..d829a4f1db 100644 --- a/server/typescript/packages/codegen-ts-react/src/templates/form-file.ts +++ b/server/typescript/packages/codegen-ts-react/src/templates/form-file.ts @@ -24,7 +24,7 @@ // (ERR_UNKNOWN_ATTR); the doc line here used to claim it turned generation ON, // while the filter beside it read it as an opt-OUT. Neither is true now. -import { code, imp, type Code } from "ts-poet"; +import { code, imp, type Code } from "@metaobjectsdev/codegen-ts/engine"; import { MetaField, MetaObject, MetaView } from "@metaobjectsdev/metadata"; import { IDENTITY_SUBTYPE_PRIMARY, diff --git a/server/typescript/packages/codegen-ts-tanstack/package.json b/server/typescript/packages/codegen-ts-tanstack/package.json index 6b6a795934..7d8cedc8fb 100644 --- a/server/typescript/packages/codegen-ts-tanstack/package.json +++ b/server/typescript/packages/codegen-ts-tanstack/package.json @@ -45,8 +45,7 @@ }, "dependencies": { "@metaobjectsdev/metadata": "workspace:*", - "@metaobjectsdev/codegen-ts": "workspace:*", - "ts-poet": "^6.10.0" + "@metaobjectsdev/codegen-ts": "workspace:*" }, "devDependencies": { "@biomejs/biome": "^1.9.0", diff --git a/server/typescript/packages/codegen-ts-tanstack/src/templates/columns-file.ts b/server/typescript/packages/codegen-ts-tanstack/src/templates/columns-file.ts index 39dfadc423..93170f240c 100644 --- a/server/typescript/packages/codegen-ts-tanstack/src/templates/columns-file.ts +++ b/server/typescript/packages/codegen-ts-tanstack/src/templates/columns-file.ts @@ -1,4 +1,4 @@ -import { code, imp, joinCode, type Code } from "ts-poet"; +import { code, imp, joinCode, type Code } from "@metaobjectsdev/codegen-ts/engine"; import type { MetaObject, MetaField } from "@metaobjectsdev/metadata"; import { LAYOUT_SUBTYPE_DATA_GRID, diff --git a/server/typescript/packages/codegen-ts-tanstack/src/templates/grid-hook-file.ts b/server/typescript/packages/codegen-ts-tanstack/src/templates/grid-hook-file.ts index bb1c0bd27d..1b73bd095e 100644 --- a/server/typescript/packages/codegen-ts-tanstack/src/templates/grid-hook-file.ts +++ b/server/typescript/packages/codegen-ts-tanstack/src/templates/grid-hook-file.ts @@ -8,7 +8,7 @@ // const grid = useSubscriberDefaultGrid(); // -import { code, imp, joinCode, type Code } from "ts-poet"; +import { code, imp, joinCode, type Code } from "@metaobjectsdev/codegen-ts/engine"; import type { MetaObject, MetaLayout } from "@metaobjectsdev/metadata"; import { LAYOUT_SUBTYPE_DATA_GRID } from "@metaobjectsdev/metadata"; import type { RenderContext } from "@metaobjectsdev/codegen-ts/engine"; diff --git a/server/typescript/packages/codegen-ts-tanstack/src/templates/hooks-file.ts b/server/typescript/packages/codegen-ts-tanstack/src/templates/hooks-file.ts index 29b84ad8e4..40ed4e8fe1 100644 --- a/server/typescript/packages/codegen-ts-tanstack/src/templates/hooks-file.ts +++ b/server/typescript/packages/codegen-ts-tanstack/src/templates/hooks-file.ts @@ -1,4 +1,4 @@ -import { code, imp, joinCode, Import, type Code } from "ts-poet"; +import { code, imp, joinCode, Import, type Code } from "@metaobjectsdev/codegen-ts/engine"; import type { MetaObject } from "@metaobjectsdev/metadata"; import type { RenderContext, RelationEntry } from "@metaobjectsdev/codegen-ts/engine"; diff --git a/server/typescript/packages/codegen-ts/LICENSE.ts-poet b/server/typescript/packages/codegen-ts/LICENSE.ts-poet new file mode 100644 index 0000000000..1f30f693e7 --- /dev/null +++ b/server/typescript/packages/codegen-ts/LICENSE.ts-poet @@ -0,0 +1,204 @@ + + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/server/typescript/packages/codegen-ts/package.json b/server/typescript/packages/codegen-ts/package.json index 0f66e9dc72..b6defc66b4 100644 --- a/server/typescript/packages/codegen-ts/package.json +++ b/server/typescript/packages/codegen-ts/package.json @@ -107,7 +107,8 @@ "src", "templates", "README.md", - "LICENSE" + "LICENSE", + "LICENSE.ts-poet" ], "scripts": { "build": "tsc -p .", @@ -145,7 +146,8 @@ "@biomejs/js-api": "^0.7.0", "@biomejs/wasm-nodejs": "^1.9.4", "@toon-format/toon": "^2.3.0", - "ts-poet": "^6.10.0" + "@dprint/formatter": "^0.6.0", + "@dprint/typescript": "0.87.1" }, "peerDependencies": { "drizzle-orm": ">=0.38.0 <1.0.0", diff --git a/server/typescript/packages/codegen-ts/src/engine-surface.ts b/server/typescript/packages/codegen-ts/src/engine-surface.ts index b5df86a8af..bc8daf8765 100644 --- a/server/typescript/packages/codegen-ts/src/engine-surface.ts +++ b/server/typescript/packages/codegen-ts/src/engine-surface.ts @@ -48,6 +48,7 @@ export const ENGINE_MODULES: readonly string[] = [ "codegen-ts/src/naming/name-claims.ts", "codegen-ts/src/owned-runtime.ts", "codegen-ts/src/pk-resolver.ts", + "codegen-ts/src/poet.ts", "codegen-ts/src/projection/build-projection-views.ts", "codegen-ts/src/projection/extract-view-spec.ts", "codegen-ts/src/projection/projection-detector.ts", diff --git a/server/typescript/packages/codegen-ts/src/engine.ts b/server/typescript/packages/codegen-ts/src/engine.ts index 2ce675ed6f..d30ca921bb 100644 --- a/server/typescript/packages/codegen-ts/src/engine.ts +++ b/server/typescript/packages/codegen-ts/src/engine.ts @@ -4,8 +4,8 @@ // `meta eject` copies a reference generator and the template modules it calls into the // project, and points every import of an engine module here. This entry is that surface and // nothing else: one `export *` per module in `ENGINE_MODULES` (engine-surface.ts), which a -// test holds to that list, plus the ts-poet a copied template module must build its `Code` -// with (the 0.21.6 split-tree defect: `Code` is recognised by `instanceof`). +// test holds to that list. One of them is `poet.ts`, the code printer a copied template module +// must build its `Code` with (the 0.21.6 split-tree defect: `Code` is recognised by `instanceof`). // // It is a subpath and not the package root on purpose. These are the generator base, the // model walks and the naming and type-mapping tables, several with short generic names @@ -35,6 +35,7 @@ export * from "./naming.js"; export * from "./naming/name-claims.js"; export * from "./owned-runtime.js"; export * from "./pk-resolver.js"; +export * from "./poet.js"; export * from "./projection/build-projection-views.js"; export * from "./projection/extract-view-spec.js"; export * from "./projection/projection-detector.js"; @@ -55,5 +56,3 @@ export * from "./templates/find-templates.js"; export * from "./tph.js"; export * from "./view-context.js"; export { ENGINE_MODULES } from "./engine-surface.js"; -export { code, imp, joinCode, Import } from "ts-poet"; -export type { Code } from "ts-poet"; diff --git a/server/typescript/packages/codegen-ts/src/index.ts b/server/typescript/packages/codegen-ts/src/index.ts index f86f0e8959..3a4289606d 100644 --- a/server/typescript/packages/codegen-ts/src/index.ts +++ b/server/typescript/packages/codegen-ts/src/index.ts @@ -193,9 +193,9 @@ export { renderSharedEnumsFile, SHARED_ENUMS_BASENAME } from "./templates/enums- export { resolveReferenceRoot, readReferenceTemplate, REFERENCE_GENERATOR_NAMES, makeReferenceReader } from "./reference-templates.js"; export type { ReferenceGeneratorName } from "./reference-templates.js"; -// ts-poet composition primitives, re-exported from THIS package's own ts-poet -// instance. The ADR-0034 owned/scaffolded generators MUST import these from here -// rather than from a bare "ts-poet": ts-poet recognizes nested Code/Import +// Code-printing primitives (src/poet.ts, vendored from ts-poet), re-exported from THIS +// package's one copy. The ADR-0034 owned/scaffolded generators MUST import these from here +// rather than from a bare "ts-poet": the printer recognizes nested Code/Import // placeholders by `instanceof`, so a Code built by this package's render* // primitives is only recognized by a `code`/`joinCode` from the SAME physical // ts-poet copy. With a globally-installed or linked CLI, the project tree and the @@ -204,8 +204,8 @@ export type { ReferenceGeneratorName } from "./reference-templates.js"; // with its own import header (duplicate `import { eq } from "drizzle-orm"`, // TS2300 on the adopter's first tsc). Gated by // cli/test/gen-split-tree-single-import.test.ts. -export { code, imp, joinCode } from "ts-poet"; -export type { Code } from "ts-poet"; +export { code, imp, joinCode } from "./poet.js"; +export type { Code } from "./poet.js"; export { renderFindByIdFn, diff --git a/server/typescript/packages/codegen-ts/src/names.ts b/server/typescript/packages/codegen-ts/src/names.ts index deda48e409..05f1cb14ac 100644 --- a/server/typescript/packages/codegen-ts/src/names.ts +++ b/server/typescript/packages/codegen-ts/src/names.ts @@ -30,7 +30,7 @@ import { type MetaObject, type MetaSource, } from "@metaobjectsdev/metadata"; -import { code, imp, type Code } from "ts-poet"; +import { code, imp, type Code } from "./poet.js"; import { crossEntitySpecifier } from "./import-path.js"; import type { RenderContext } from "./render-context.js"; import { effectivePackage } from "./docs-paths.js"; diff --git a/server/typescript/packages/codegen-ts/src/poet.ts b/server/typescript/packages/codegen-ts/src/poet.ts new file mode 100644 index 0000000000..6d6aed1a06 --- /dev/null +++ b/server/typescript/packages/codegen-ts/src/poet.ts @@ -0,0 +1,652 @@ +// The code-printing primitives every TypeScript generator builds its output with: `code`, +// `imp`, `joinCode` and the `Code` / `Import` nodes they produce. +// +// Vendored from ts-poet 6.12.0 (https://github.com/stephenh/ts-poet, commit bda410a1), +// Copyright Stephen Haberman, Apache License 2.0 — the licence text ships with this package +// as `LICENSE.ts-poet`. Only the part the generators use is kept: no `saveFiles`, +// `conditionalOutput` or the CommonJS import fix-ups, and `toString` takes `path`, +// `importExtensions` and `format` only. +// +// WHY VENDORED: ts-poet `require`s `dprint-node` when it loads, and dprint-node ships all +// seven of its platform binaries in one package (~25 MB on every CLI install, of which a +// machine can run one). This copy formats with the SAME dprint TypeScript plugin, built to +// WebAssembly (`@dprint/typescript`, pinned to 0.87.1 — the plugin dprint-node 1.0.8 +// compiles in), configured exactly as ts-poet configured dprint-node, so the generated text +// is byte-identical to what ts-poet printed. Bump that pin only together with the goldens. +// +// ONE COPY PER PROCESS: a `Code` placeholder is recognised by `instanceof`, so every module +// that composes `Code` must import it from here (inside this package) or from +// `@metaobjectsdev/codegen-ts/engine` (everywhere else) — the 0.21.6 split-tree defect. + +import { readFileSync } from "node:fs"; +import { createRequire } from "node:module"; +import * as path from "node:path"; +import { createFromBuffer, type Formatter } from "@dprint/formatter"; + +/** Options for `toString`, i.e. for the top-level, per-file output. */ +export interface ToStringOpts { + /** The intended file name of this code; imports from this file itself are dropped. */ + path?: string; + /** How to handle file extensions in imports: kept (`true`), dropped (`false`), or rewritten to `.js` / `.ts`. */ + importExtensions?: boolean | "ts" | "js"; + /** Whether to run the dprint pass (default `true`). */ + format?: boolean; +} + +// --------------------------------------------------------------------------- +// nodes +// --------------------------------------------------------------------------- + +export abstract class Node { + /** Return the unformatted code for this node. */ + abstract toCodeString(): string; + + /** Any potentially string/Import/Code nested nodes within us. */ + abstract get childNodes(): unknown[]; +} + +// No member is `private`: a private member makes a class NOMINAL in TypeScript, and this +// package's own src/ and the published dist/ declare two `Code` types that a reference +// template mixes (it reaches templates by relative path and the engine by package entry). +// ts-poet never hit this because both sides resolved one `.d.ts`. The members marked +// `@internal` are still not API. +export class Code extends Node { + // Used by joinCode + public trim = false; + /** @internal */ + oneline = false; + // Cache the unformatted results of `toString` since we're immutable. + /** @internal */ + code: string | undefined; + /** @internal */ + codeWithImports: string | undefined; + + constructor( + /** @internal */ + readonly literals: readonly string[], + /** @internal */ + readonly placeholders: unknown[], + ) { + super(); + } + + /** Returns the formatted code, with imports. */ + override toString(opts: ToStringOpts = {}): string { + this.codeWithImports ??= this.generateCodeWithImports(opts); + return opts.format === false ? this.codeWithImports : maybePretty(this.codeWithImports); + } + + asOneline(): Code { + this.oneline = true; + return this; + } + + get childNodes(): unknown[] { + return this.placeholders; + } + + /** Returns the unformatted, import-less code. Internal; `toString` is the public API. */ + toCodeString(): string { + return (this.code ??= this.generateCode()); + } + + /** @internal */ + deepFindAll(): [Import[], Def[]] { + const imports: Import[] = []; + const defs: Def[] = []; + const todo: unknown[] = [this]; + let i = 0; + while (i < todo.length) { + const placeholder = todo[i++]; + if (placeholder instanceof Node) { + todo.push(...placeholder.childNodes); + } else if (Array.isArray(placeholder)) { + todo.push(...placeholder); + } + if (placeholder instanceof Import) { + imports.push(placeholder); + } else if (placeholder instanceof Def) { + defs.push(placeholder); + } + } + return [imports, defs]; + } + + /** @internal */ + generateCode(): string { + const { literals, placeholders } = this; + let result = ""; + // interleave the literals with the placeholders + for (let i = 0; i < placeholders.length; i++) { + result += literals[i] + deepGenerate(placeholders[i]); + } + // add the last literal + result += literals[literals.length - 1]; + if (this.trim) { + result = result.trim(); + } + if (this.oneline) { + result = result.replace(/\n/g, ""); + } + return result; + } + + /** @internal */ + generateCodeWithImports(opts: ToStringOpts): string { + const { path: filePath = "", importExtensions = true } = opts; + const ourModulePath = filePath.replace(/\.[tj]sx?/, ""); + const [imports, defs] = this.deepFindAll(); + assignAliasesIfNeeded(defs, imports, ourModulePath); + const importPart = emitImports(imports, ourModulePath, importExtensions); + const bodyPart = this.generateCode(); + return importPart + "\n" + bodyPart; + } +} + +function deepGenerate(object: unknown): string { + let result = ""; + const todo: unknown[] = [object]; + let i = 0; + while (i < todo.length) { + const current = todo[i++]; + if (Array.isArray(current)) { + todo.push(...current); + } else if (current instanceof Node) { + result += current.toCodeString(); + } else if (current === null) { + result += "null"; + } else if (current !== undefined) { + result += isPlainObject(current) ? JSON.stringify(current) : String(current); + } else { + result += "undefined"; + } + } + return result; +} + +/** Finds any namespace collisions of a named import colliding with a def and gives the import an alias. */ +function assignAliasesIfNeeded(defs: Def[], imports: Import[], ourModulePath: string): void { + // Keep track of used (whether declared or imported) symbols + const usedSymbols = new Set(); + // Mark all locally-defined symbols as used + defs.forEach((d) => usedSymbols.add(d.symbol)); + // A mapping of original to assigned alias, i.e. Foo@foo --> Foo2 + const assignedAliases: Record = {}; + let j = 1; + imports.forEach((i) => { + if ( + i instanceof ImportsName && + // Don't bother aliasing imports from our own module + !(sameModule(i.source, ourModulePath) || (i.definedIn && sameModule(i.definedIn, ourModulePath))) + ) { + const key = `${i.symbol}@${i.source}`; + if (usedSymbols.has(i.symbol)) { + let alias = assignedAliases[key]; + if (!alias) { + alias = `${i.symbol}${j++}`; + assignedAliases[key] = alias; + } + // Move the original symbol over + if (alias !== i.symbol) { + i.sourceSymbol = i.symbol; + } + i.symbol = alias; + } else { + usedSymbols.add(i.symbol); + assignedAliases[key] = i.symbol; + } + } + }); +} + +/** + * A symbol defined in the current file, so an import of the same name from another file is + * aliased instead of colliding. + */ +export class Def extends Node { + constructor(public symbol: string) { + super(); + } + + toCodeString(): string { + return this.symbol; + } + + get childNodes(): Node[] { + return []; + } +} + +/** A literal source representation of the provided object. */ +export class Literal extends Node { + private readonly tokens: (string | Node)[]; + + constructor(object: unknown) { + super(); + this.tokens = flatten(object); + } + + get childNodes(): unknown[] { + return this.tokens; + } + + toCodeString(): string { + return this.tokens.map((node) => (typeof node === "string" ? node : node.toCodeString())).join(" "); + } +} + +function flatten(o: unknown): (string | Node)[] { + if (typeof o === "undefined") { + return ["undefined"]; + } + if (typeof o === "object" && o != null) { + if (o instanceof Node) { + return [o]; + } else if (Array.isArray(o)) { + const nodes: (string | Node)[] = ["["]; + for (let i = 0; i < o.length; i++) { + if (i !== 0) nodes.push(","); + nodes.push(...flatten(o[i])); + } + nodes.push("]"); + return nodes; + } else if (isPlainObject(o)) { + const nodes: (string | Node)[] = ["{"]; + const entries = Object.entries(o); + for (let i = 0; i < entries.length; i++) { + if (i !== 0) nodes.push(","); + const [key, value] = entries[i]!; + nodes.push(JSON.stringify(key), ":", ...flatten(value)); + } + nodes.push("}"); + return nodes; + } + } + return [JSON.stringify(o)]; +} + +// is-plain-object , +// Copyright (c) 2014-2017, Jon Schlinkert. Released under the MIT License. +function isPlainObject(o: unknown): boolean { + if (o === null || o === undefined) return false; + if (!isObject(o)) return false; + // If has modified constructor + const ctor: unknown = (o as { constructor?: unknown }).constructor; + if (ctor === undefined) return true; + // If has modified prototype + const proto: unknown = (ctor as { prototype?: unknown }).prototype; + if (!isObject(proto)) return false; + // If constructor does not have an Object-specific method + if (!Object.prototype.hasOwnProperty.call(proto, "isPrototypeOf")) return false; + // Most likely a plain Object + return true; +} + +function isObject(o: unknown): boolean { + return Object.prototype.toString.call(o) === "[object Object]"; +} + +// --------------------------------------------------------------------------- +// imports +// --------------------------------------------------------------------------- + +const typeImportMarker = "(?:t:)?"; +const modulePattern = `.+`; +const identPattern = `(?:(?:[a-zA-Z][_a-zA-Z0-9]*)|(?:[_a-zA-Z][_a-zA-Z0-9]+))`; +const importType = "[*@+=]"; +const importPattern = `^(${typeImportMarker}${identPattern})(\\.${identPattern})?(${importType})(${modulePattern})`; +// This is for doing `SourceSymbol:ImportSymbol` +const sourceIdentPattern = `(?:(?:${identPattern}:)?)`; +const sourceImportPattern = `^(${typeImportMarker}${sourceIdentPattern}${identPattern})(\\.${identPattern})?(@)(${modulePattern})`; + +/** A symbol and its origin, either via import or implicit/local declaration. */ +export abstract class Import extends Node { + /** + * Parses a symbol spec: `symbolName importType modulePath`, where `importType` is + * `@` (named: `Foo@bar` → `import { Foo } from 'bar'`, `t:Foo@bar` for `import type`, + * `Foo:Bar@baz` for `import { Foo as Bar }`), `*` (star: `Foo*foo` → `import * as Foo from 'foo'`), + * `=` (default: `Foo=foo` → `import Foo from 'foo'`) or `+` (side effect: `Foo+foo` → + * `import 'foo'`). A spec that matches none of these is an implicit, untracked symbol. + */ + static from(spec: string): Import { + let matched = spec.match(importPattern); + if (matched === null) { + matched = spec.match(sourceImportPattern); + } + if (matched != null) { + const modulePath = matched[4]!; + const childSymbol = matched[2]?.substring(1) || undefined; // I.e. `Transaction` in `Knex.Transaction` + const kind = matched[3] || "@"; + const symbolName = matched[1] || ""; + switch (kind) { + case "*": + return Import.importsAll(symbolName, modulePath); + case "@": { + const isTypeImport = symbolName.startsWith("t:"); + const exportedNames = (isTypeImport ? symbolName.substring(2) : symbolName).split(":"); + const exportedName = exportedNames.pop()!; + const sourceExportedName = exportedNames[0]; + return Import.importsName(exportedName, modulePath, isTypeImport, sourceExportedName, childSymbol); + } + case "=": + return Import.importsDefault(symbolName, modulePath); + case "+": + return Import.sideEffect(symbolName, modulePath); + default: + throw new Error("Invalid import kind character"); + } + } + return Import.implicit(spec); + } + + static fromMaybeString(spec: string | Import): Import { + return typeof spec === "string" ? Import.from(spec) : spec; + } + + /** + * Defined if the symbol is typically imported from a barrel/etc. path, but is technically + * defined in another file — so no import is emitted when it is used in that file. + */ + definedIn?: string; + + protected constructor(public symbol: string) { + super(); + } + + toCodeString(): string { + return this.symbol; + } + + get childNodes(): unknown[] { + return []; + } + + abstract source: string | undefined; + + /** `import * as Engine from 'templates';` */ + static importsAll(localName: string, from: string): Import { + return new ImportsAll(localName, from); + } + + /** `import { Engine } from 'templates';` / `import { Foo as Bar } from 'templates';` */ + static importsName( + exportedName: string, + from: string, + typeImport: boolean, + sourceSymbol?: string, + childSymbol?: string, + ): Import { + return new ImportsName(exportedName, from, sourceSymbol, typeImport, childSymbol); + } + + /** `import 'mocha'` */ + static sideEffect(symbolName: string, from: string): Import { + return new SideEffect(symbolName, from); + } + + /** An implied symbol that does no tracking of imports. */ + static implicit(name: string): Import { + return new Implicit(name); + } + + /** `import Engine from 'engine';` */ + static importsDefault(exportedName: string, from: string): Import { + return new ImportsDefault(exportedName, from); + } +} + +/** Non-imported symbol. */ +export class Implicit extends Import { + constructor(symbol: string) { + super(symbol); + } + + source = undefined; +} + +/** Common base class for imported symbols. */ +export abstract class Imported extends Import { + /** The symbol is the imported symbol, i.e. `BarClass`, and source is the path it comes from. */ + protected constructor( + public override symbol: string, + public source: string, + ) { + super(source); + } +} + +/** `import { Engine } from 'templates'` or `import { Engine as Engine2 } from 'templates'`. */ +export class ImportsName extends Imported { + constructor( + symbol: string, + source: string, + /** The original symbol when renaming, i.e. `Engine` in `Engine as Engine2`. */ + public sourceSymbol?: string, + /** Whether this is an `import type` import. */ + public typeImport?: boolean, + /** The `Transaction` in a `Knex.Transaction`. */ + public childSymbol?: string, + ) { + super(symbol, source); + } + + toImportPiece(skipTypeImport = false): string { + const maybeTypePrefix = this.typeImport && !skipTypeImport ? "type " : ""; + return maybeTypePrefix + (this.sourceSymbol ? `${this.sourceSymbol} as ${this.symbol}` : this.symbol); + } + + override toCodeString(): string { + return this.childSymbol ? `${this.symbol}.${this.childSymbol}` : this.symbol; + } +} + +/** `import Engine from 'engine';` */ +export class ImportsDefault extends Imported { + constructor(symbol: string, source: string) { + super(symbol, source); + } +} + +/** `import * as Engine from 'templates';` */ +export class ImportsAll extends Imported { + constructor(symbol: string, source: string) { + super(symbol, source); + } +} + +/** A symbol brought in as a side effect of an import: `Foo+mocha` adds `import 'mocha'`. */ +export class SideEffect extends Imported { + constructor(symbol: string, source: string) { + super(symbol, source); + } +} + +/** Generates the `import ...` lines for the given `imports`. */ +function emitImports(imports: Import[], ourModulePath: string, importExtensions: boolean | "js" | "ts"): string { + if (imports.length == 0) { + return ""; + } + let result = ""; + + // Group the imports by source module they're imported from + const importsByModule = groupBy( + imports.filter( + (it) => + it.source !== undefined && + // Ignore imports that are in our own file + !(it instanceof ImportsName && it.definedIn && sameModule(it.definedIn, ourModulePath)), + ), + (it) => it.source!, + ); + + // Output each source module as one line + Object.entries(importsByModule).forEach(([modulePath, imports]) => { + // Skip imports from the current module + if (sameModule(ourModulePath, modulePath)) return; + const importPath = maybeAdjustExtension(maybeRelativePath(ourModulePath, modulePath), importExtensions); + + // Output star imports individually + unique(filterInstances(imports, ImportsAll).map((i) => i.symbol)).forEach((symbol) => { + result += `import * as ${symbol} from '${importPath}';\n`; + }); + + // Partition named imported into `import type` vs. regular imports + const symbolImports = filterInstances(imports, ImportsName).filter((it) => !it.typeImport); + const typeImports = filterInstances(imports, ImportsName).filter( + (it) => !!it.typeImport && !symbolImports.some((s) => s.symbol === it.symbol), + ); + // Use `import type { ... }` when every named import from the module is a type + const useSingleTypeImport = typeImports.length > 0 && symbolImports.length === 0; + const namedImports = useSingleTypeImport + ? unique(typeImports.map((it) => it.toImportPiece(true))) + : unique([...typeImports, ...symbolImports].map((it) => it.toImportPiece())); + const defaultImports = unique(filterInstances(imports, ImportsDefault).map((it) => it.symbol)); + if (namedImports.length > 0 || defaultImports.length > 0) { + // Output named imports as a group + const namesPart = namedImports.length > 0 ? [`{ ${namedImports.join(", ")} }`] : []; + const defPart = defaultImports.length > 0 ? [defaultImports[0]] : []; + result += `import${useSingleTypeImport ? " type" : ""} ${[...defPart, ...namesPart].join(", ")} from '${importPath}';\n`; + } + }); + + const sideEffectImports = groupBy(filterInstances(imports, SideEffect), (a) => a.source); + Object.keys(sideEffectImports).forEach((it) => (result += `import '${it}';\n`)); + + return result; +} + +function groupBy(list: T[], fn: (x: T) => string): Record { + const result: Record = {}; + list.forEach((o) => { + (result[fn(o)] ??= []).push(o); + }); + return result; +} + +function filterInstances(list: readonly unknown[], t: abstract new (...args: never[]) => U): U[] { + return list.filter((e): e is U => e instanceof t); +} + +function unique(list: T[]): T[] { + return [...new Set(list)]; +} + +function maybeRelativePath(outputPath: string, importPath: string): string { + if (!importPath.startsWith("./")) { + return importPath; + } + importPath = path.normalize(importPath); + outputPath = path.normalize(outputPath); + const outputPathDir = path.dirname(outputPath); + if (outputPathDir === importPath) { + // importing a file that is named the same thing as the directory the file doing the importing is in. + // e.g. importing `./foo` from `./foo/index.ts` reads `../foo`, not `./` + return ".." + path.sep + path.basename(importPath); + } + let relativePath = path.relative(outputPathDir, importPath).split(path.sep).join(path.posix.sep); + if (!relativePath.startsWith(".")) { + // ensure the js compiler recognizes this is a relative path. + relativePath = "./" + relativePath; + } + return relativePath; +} + +const extensionRegex = /\.[tj]sx?/; + +function maybeAdjustExtension(p: string, importExtensions: boolean | "js" | "ts"): string { + if (importExtensions === true) return p; + if (importExtensions === false) return p.replace(extensionRegex, ""); + if (importExtensions === "js") return p.replace(/\.ts$/, ".js").replace(/\.tsx$/, ".jsx"); + return p.replace(/\.js$/, ".ts").replace(/\.jsx$/, ".tsx"); +} + +/** Checks if `path1 === path2` despite minor path differences like `./foo` and `foo`. */ +function sameModule(path1: string, path2: string): boolean { + // TypeScript: import paths ending in .js and .ts are resolved to the .ts file. + const [basePath1, basePath2] = [path1, path2].map((p) => p.replace(extensionRegex, "")) as [string, string]; + return basePath1 === basePath2 || path.resolve(basePath1) === path.resolve(basePath2); +} + +// --------------------------------------------------------------------------- +// formatting +// --------------------------------------------------------------------------- + +// ts-poet's dprint configuration, unchanged: "prettier-ish", plus forced braces and new +// lines because the pre-formatted output is all bunched together. `deno: true` is the base +// dprint-node applied before these overrides. +const DPRINT_CONFIG = { + deno: true, + useTabs: false, + useBraces: "always", + singleBodyPosition: "nextLine", + "arrowFunction.useParentheses": "force", + "module.sortImportDeclarations": "caseSensitive", + lineWidth: 120, + preferSingleLine: true, +}; + +let formatter: Formatter | undefined; + +function dprint(): Formatter { + if (formatter === undefined) { + const wasm = readFileSync(createRequire(import.meta.url).resolve("@dprint/typescript/plugin.wasm")); + formatter = createFromBuffer(wasm); + formatter.setConfig({}, DPRINT_CONFIG); + } + return formatter; +} + +function maybePretty(input: string): string { + try { + return dprint().formatText({ filePath: "file.ts", fileText: input.trim() }); + } catch { + return input; // assume it's invalid syntax and ignore + } +} + +// --------------------------------------------------------------------------- +// combinators +// --------------------------------------------------------------------------- + +/** A template literal to format code and auto-organize imports. */ +export function code(literals: TemplateStringsArray, ...placeholders: unknown[]): Code { + return new Code( + literals, + placeholders.map((p) => (isPlainObject(p) ? literalOf(p) : p)), + ); +} + +export function literalOf(object: unknown): Node { + return new Literal(object); +} + +export function arrayOf(...elements: unknown[]): Node { + return literalOf(elements); +} + +export function joinCode(chunks: Code[], opts: { on?: string; trim?: boolean } = {}): Code { + const { on = "", trim = true } = opts; + const literals: string[] = [""]; + for (let i = 0; i < chunks.length - 1; i++) { + literals.push(on); + } + literals.push(""); + if (trim) { + chunks.forEach((c) => (c.trim = true)); + } + return new Code(literals, chunks); +} + +/** Creates an import that will be auto-imported at the top of the output file. */ +export function imp(spec: string, opts: { definedIn?: string } = {}): Import { + const sym = Import.from(spec); + if (opts.definedIn) { + sym.definedIn = opts.definedIn; + } + return sym; +} + +/** Defines `symbol` as being locally defined in the file, to avoid import collisions. */ +export function def(symbol: string): Def { + return new Def(symbol); +} diff --git a/server/typescript/packages/codegen-ts/src/templates/drizzle-schema.ts b/server/typescript/packages/codegen-ts/src/templates/drizzle-schema.ts index 517f7c8715..080176b84a 100644 --- a/server/typescript/packages/codegen-ts/src/templates/drizzle-schema.ts +++ b/server/typescript/packages/codegen-ts/src/templates/drizzle-schema.ts @@ -2,7 +2,7 @@ // with FK .references() on FK columns derived from relationship children, // plus the relations() block auto-emitted at the end. -import { code, imp, joinCode, type Code } from "ts-poet"; +import { code, imp, joinCode, type Code } from "../poet.js"; import { MetaObject, MetaField, MetaIndex, isMetaObject } from "@metaobjectsdev/metadata"; import { IDENTITY_ATTR_FIELDS, IDENTITY_ATTR_GENERATION, IDENTITY_ATTR_SEQUENCE_START, IDENTITY_ATTR_SEQUENCE_NAME, diff --git a/server/typescript/packages/codegen-ts/src/templates/duration-wire-helper.ts b/server/typescript/packages/codegen-ts/src/templates/duration-wire-helper.ts index 7ddd1d38c7..561824a180 100644 --- a/server/typescript/packages/codegen-ts/src/templates/duration-wire-helper.ts +++ b/server/typescript/packages/codegen-ts/src/templates/duration-wire-helper.ts @@ -15,7 +15,7 @@ // interval has its own range, which Postgres enforces. The emitted code calls `BigInt(...)` and // writes no bigint literal, so it needs the ES2020 `BigInt` global and nothing newer. -import { code, imp, type Code } from "ts-poet"; +import { code, imp, type Code } from "../poet.js"; import { DURATION_PATTERN } from "@metaobjectsdev/metadata"; import type { Dialect } from "../metaobjects-config.js"; import { DURATION_COLUMN_FN } from "../column-mapper.js"; diff --git a/server/typescript/packages/codegen-ts/src/templates/entity-constants.ts b/server/typescript/packages/codegen-ts/src/templates/entity-constants.ts index 1e26a0f469..21041b1df8 100644 --- a/server/typescript/packages/codegen-ts/src/templates/entity-constants.ts +++ b/server/typescript/packages/codegen-ts/src/templates/entity-constants.ts @@ -26,7 +26,7 @@ // per-attribute access (placeholder, rules, etc.) by hand — the helper // picks them up from this object automatically. -import { code, joinCode, type Code } from "ts-poet"; +import { code, joinCode, type Code } from "../poet.js"; import { MetaObject } from "@metaobjectsdev/metadata"; import { resolveTableName } from "@metaobjectsdev/metadata"; import { physicalNameExpr } from "../names.js"; diff --git a/server/typescript/packages/codegen-ts/src/templates/entity-file.ts b/server/typescript/packages/codegen-ts/src/templates/entity-file.ts index 9989e060cb..c30f4e1aca 100644 --- a/server/typescript/packages/codegen-ts/src/templates/entity-file.ts +++ b/server/typescript/packages/codegen-ts/src/templates/entity-file.ts @@ -8,7 +8,7 @@ import { CodegenError } from "../errors.js"; import { flattenedMembersOf } from "../flattened.js"; -import { code, imp, joinCode, type Code } from "ts-poet"; +import { code, imp, joinCode, type Code } from "../poet.js"; import type { MetaObject, MetaField } from "@metaobjectsdev/metadata"; import { SOURCE_ROLE_REPLICA } from "@metaobjectsdev/metadata"; import type { RenderContext } from "../render-context.js"; diff --git a/server/typescript/packages/codegen-ts/src/templates/entity-meta-file.ts b/server/typescript/packages/codegen-ts/src/templates/entity-meta-file.ts index 8f8d0877aa..1b5c3743c7 100644 --- a/server/typescript/packages/codegen-ts/src/templates/entity-meta-file.ts +++ b/server/typescript/packages/codegen-ts/src/templates/entity-meta-file.ts @@ -20,7 +20,7 @@ // consumer having wired an extra generator, since the entity generator is // scaffold-and-own (ADR-0034) and cannot be changed from the package. -import { code, type Code } from "ts-poet"; +import { code, type Code } from "../poet.js"; import type { MetaObject } from "@metaobjectsdev/metadata"; import { GENERATED_HEADER, GENERATED_EDIT_NOTE } from "../constants.js"; import { renderEntityConstants } from "./entity-constants.js"; diff --git a/server/typescript/packages/codegen-ts/src/templates/enum-int-codec.ts b/server/typescript/packages/codegen-ts/src/templates/enum-int-codec.ts index 6f2d1ecdb6..94de947410 100644 --- a/server/typescript/packages/codegen-ts/src/templates/enum-int-codec.ts +++ b/server/typescript/packages/codegen-ts/src/templates/enum-int-codec.ts @@ -18,7 +18,7 @@ * declaration emitted after its use is a TDZ ReferenceError at import: both are worse than * the missing-codec bug, because neither lets the module load at all. */ -import { code, imp, type Code } from "ts-poet"; +import { code, imp, type Code } from "../poet.js"; import type { EnumIntCustomType } from "../column-mapper.js"; /** diff --git a/server/typescript/packages/codegen-ts/src/templates/enums-file.ts b/server/typescript/packages/codegen-ts/src/templates/enums-file.ts index 90724dbae2..9cac1aa206 100644 --- a/server/typescript/packages/codegen-ts/src/templates/enums-file.ts +++ b/server/typescript/packages/codegen-ts/src/templates/enums-file.ts @@ -10,7 +10,7 @@ // @provided enums are NEVER materialized here (they live in hand-written code, // imported from the configured providedEnumModule). -import { code, imp, joinCode, type Code } from "ts-poet"; +import { code, imp, joinCode, type Code } from "../poet.js"; import type { MetaRoot } from "@metaobjectsdev/metadata"; import { GENERATED_HEADER, GENERATED_EDIT_NOTE } from "../constants.js"; import { materializedSharedEnums, type SharedEnum } from "../enum-shared.js"; diff --git a/server/typescript/packages/codegen-ts/src/templates/filter-allowlist.ts b/server/typescript/packages/codegen-ts/src/templates/filter-allowlist.ts index b3b17b1a04..57708d764a 100644 --- a/server/typescript/packages/codegen-ts/src/templates/filter-allowlist.ts +++ b/server/typescript/packages/codegen-ts/src/templates/filter-allowlist.ts @@ -1,5 +1,5 @@ import { flatSurfaceFields } from "../flattened.js"; -import { code, type Code } from "ts-poet"; +import { code, type Code } from "../poet.js"; import { MetaField, MetaObject } from "@metaobjectsdev/metadata"; import { FIELD_ATTR_FILTERABLE, diff --git a/server/typescript/packages/codegen-ts/src/templates/filter-type.ts b/server/typescript/packages/codegen-ts/src/templates/filter-type.ts index cce239fe81..a2425af8d9 100644 --- a/server/typescript/packages/codegen-ts/src/templates/filter-type.ts +++ b/server/typescript/packages/codegen-ts/src/templates/filter-type.ts @@ -3,7 +3,7 @@ // datetimes get eq/ne/gt/gte/lt/lte/in. An enum's operand type is its member union. import { flatSurfaceFields } from "../flattened.js"; -import { code, type Code } from "ts-poet"; +import { code, type Code } from "../poet.js"; import { MetaField, MetaObject } from "@metaobjectsdev/metadata"; import { FIELD_ATTR_FILTERABLE, diff --git a/server/typescript/packages/codegen-ts/src/templates/inferred-types.ts b/server/typescript/packages/codegen-ts/src/templates/inferred-types.ts index 5395c85792..3e8d4146ba 100644 --- a/server/typescript/packages/codegen-ts/src/templates/inferred-types.ts +++ b/server/typescript/packages/codegen-ts/src/templates/inferred-types.ts @@ -8,7 +8,7 @@ // without the superfluous `| null` that Drizzle nullable columns introduce). import { flatSurfaceFields } from "../flattened.js"; -import { code, imp, joinCode, type Code } from "ts-poet"; +import { code, imp, joinCode, type Code } from "../poet.js"; import type { MetaObject, MetaField } from "@metaobjectsdev/metadata"; import { FIELD_SUBTYPE_ENUM, diff --git a/server/typescript/packages/codegen-ts/src/templates/projection-decl.ts b/server/typescript/packages/codegen-ts/src/templates/projection-decl.ts index 141631e62d..e2c9dcf50d 100644 --- a/server/typescript/packages/codegen-ts/src/templates/projection-decl.ts +++ b/server/typescript/packages/codegen-ts/src/templates/projection-decl.ts @@ -10,7 +10,7 @@ // - Zod Insert/Update schemas // - Insert/Update types -import { code, imp, joinCode, type Code, type Import } from "ts-poet"; +import { code, imp, joinCode, type Code, type Import } from "../poet.js"; import { MetaField, MetaObject, type MetaRoot, isMetaObject, FIELD_ATTR_OBJECT_REF, stripPackage, resolveColumnName, resolveTableSchema, diff --git a/server/typescript/packages/codegen-ts/src/templates/queries-file.ts b/server/typescript/packages/codegen-ts/src/templates/queries-file.ts index f28d2767ac..455d0c68d7 100644 --- a/server/typescript/packages/codegen-ts/src/templates/queries-file.ts +++ b/server/typescript/packages/codegen-ts/src/templates/queries-file.ts @@ -1,7 +1,7 @@ // Queries file composer — composes all CRUD function renderers (from queries.ts) into // a complete .queries.ts file with @generated header and correct imports. -import { code, joinCode, imp, type Code } from "ts-poet"; +import { code, joinCode, imp, type Code } from "../poet.js"; import { MetaObject, OBJECT_ATTR_DISCRIMINATOR, diff --git a/server/typescript/packages/codegen-ts/src/templates/queries.ts b/server/typescript/packages/codegen-ts/src/templates/queries.ts index e9364108ca..4b2d942ae8 100644 --- a/server/typescript/packages/codegen-ts/src/templates/queries.ts +++ b/server/typescript/packages/codegen-ts/src/templates/queries.ts @@ -1,7 +1,7 @@ // Queries template — individual CRUD function renderers. // Each returns a ts-poet Code block; composed into a file by queries-file.ts. -import { code, imp, type Code } from "ts-poet"; +import { code, imp, type Code } from "../poet.js"; import { type MetaObject, IDENTITY_REFERENCE_ATTR_REFERENCES, diff --git a/server/typescript/packages/codegen-ts/src/templates/relations-block.ts b/server/typescript/packages/codegen-ts/src/templates/relations-block.ts index 8c6694a6c7..11fff7a213 100644 --- a/server/typescript/packages/codegen-ts/src/templates/relations-block.ts +++ b/server/typescript/packages/codegen-ts/src/templates/relations-block.ts @@ -1,7 +1,7 @@ // Relations block template — emits the Drizzle relations() block for one entity. // Derived from the RelationMap pre-pass (relation-resolver.ts). -import { code, imp, joinCode, type Code } from "ts-poet"; +import { code, imp, joinCode, type Code } from "../poet.js"; import type { MetaObject } from "@metaobjectsdev/metadata"; import { CARDINALITY_ONE, CARDINALITY_MANY } from "@metaobjectsdev/metadata"; import { type RenderContext } from "../render-context.js"; diff --git a/server/typescript/packages/codegen-ts/src/templates/routes-file-hono.ts b/server/typescript/packages/codegen-ts/src/templates/routes-file-hono.ts index f828e14e1a..ee84a4654c 100644 --- a/server/typescript/packages/codegen-ts/src/templates/routes-file-hono.ts +++ b/server/typescript/packages/codegen-ts/src/templates/routes-file-hono.ts @@ -24,7 +24,7 @@ // isProjection(entity) → mountReadOnlyCrudRoutes (GET list + GET :id) // vanilla / write-through entity → mountCrudRoutes (all 5 CRUD verbs) -import { code, imp } from "ts-poet"; +import { code, imp } from "../poet.js"; import type { MetaObject } from "@metaobjectsdev/metadata"; import { type RenderContext } from "../render-context.js"; import { entityModuleSpecifier, siblingSpecifier } from "../import-path.js"; diff --git a/server/typescript/packages/codegen-ts/src/templates/routes-file.ts b/server/typescript/packages/codegen-ts/src/templates/routes-file.ts index 855bda278e..e89dfa3461 100644 --- a/server/typescript/packages/codegen-ts/src/templates/routes-file.ts +++ b/server/typescript/packages/codegen-ts/src/templates/routes-file.ts @@ -16,7 +16,7 @@ // the existing queries-file template). The entity's Drizzle table const is // imported alongside the Zod schemas + constants from the sibling Entity.ts. -import { code, imp, joinCode, type Code } from "ts-poet"; +import { code, imp, joinCode, type Code } from "../poet.js"; import type { MetaObject } from "@metaobjectsdev/metadata"; import { TYPE_FIELD, diff --git a/server/typescript/packages/codegen-ts/src/templates/tph-discriminator.ts b/server/typescript/packages/codegen-ts/src/templates/tph-discriminator.ts index e52f795910..02585c02df 100644 --- a/server/typescript/packages/codegen-ts/src/templates/tph-discriminator.ts +++ b/server/typescript/packages/codegen-ts/src/templates/tph-discriminator.ts @@ -14,7 +14,7 @@ // shape — covered by FR-014 fixture `tph-discriminator-string-no-subtypes`), // returns null too: there are no subtype names to union. -import { code, joinCode, imp, type Code } from "ts-poet"; +import { code, joinCode, imp, type Code } from "../poet.js"; import { type MetaObject, type MetaField, diff --git a/server/typescript/packages/codegen-ts/src/templates/value-object-file.ts b/server/typescript/packages/codegen-ts/src/templates/value-object-file.ts index b375d296f7..b63955b5f7 100644 --- a/server/typescript/packages/codegen-ts/src/templates/value-object-file.ts +++ b/server/typescript/packages/codegen-ts/src/templates/value-object-file.ts @@ -13,7 +13,7 @@ // false. The entity may still have read-only source.* children (those are // handled by the projection path before this one is reached). -import { joinCode, type Code } from "ts-poet"; +import { joinCode, type Code } from "../poet.js"; import type { MetaObject } from "@metaobjectsdev/metadata"; import { OBJECT_SUBTYPE_VALUE } from "@metaobjectsdev/metadata"; import type { RenderContext } from "../render-context.js"; diff --git a/server/typescript/packages/codegen-ts/src/templates/view-decl.ts b/server/typescript/packages/codegen-ts/src/templates/view-decl.ts index 34cdc510f5..01f3ef64e9 100644 --- a/server/typescript/packages/codegen-ts/src/templates/view-decl.ts +++ b/server/typescript/packages/codegen-ts/src/templates/view-decl.ts @@ -7,7 +7,7 @@ // attempt DDL (the SQL view is created/owned by migrate-ts). Both hosts emit the // SAME declaration shape, so it lives here once (extracted from projection-decl). -import { code, imp, joinCode, type Code, type Import } from "ts-poet"; +import { code, imp, joinCode, type Code, type Import } from "../poet.js"; import { dialectModule } from "../dialect-module.js"; import type { Dialect } from "../metaobjects-config.js"; import { diff --git a/server/typescript/packages/codegen-ts/src/templates/zod-validators.ts b/server/typescript/packages/codegen-ts/src/templates/zod-validators.ts index 46536317c5..a38f97bdee 100644 --- a/server/typescript/packages/codegen-ts/src/templates/zod-validators.ts +++ b/server/typescript/packages/codegen-ts/src/templates/zod-validators.ts @@ -11,7 +11,7 @@ import { flatSurfaceFields } from "../flattened.js"; import { entityTypeNames } from "../entity-type-names.js"; -import { code, joinCode, imp, type Code } from "ts-poet"; +import { code, joinCode, imp, type Code } from "../poet.js"; import { MetaObject, MetaField, type MetaRoot, stripPackage } from "@metaobjectsdev/metadata"; import { FIELD_SUBTYPE_STRING, FIELD_SUBTYPE_INT, FIELD_SUBTYPE_LONG, FIELD_SUBTYPE_CURRENCY, diff --git a/server/typescript/packages/codegen-ts/test/names-flattened.test.ts b/server/typescript/packages/codegen-ts/test/names-flattened.test.ts index c1edf8f912..41f7cf5f2c 100644 --- a/server/typescript/packages/codegen-ts/test/names-flattened.test.ts +++ b/server/typescript/packages/codegen-ts/test/names-flattened.test.ts @@ -29,7 +29,7 @@ import { import { resolveObjectNames, type FieldNames } from "../src/names.js"; import { renderNamesDecl } from "../src/templates/names-decl.js"; import { renderProjectionDecl } from "../src/templates/projection-decl.js"; -import { code } from "ts-poet"; +import { code } from "../src/poet.js"; const CORPUS = resolve(import.meta.dir, "../../../../../fixtures/naming-conformance/physical-names"); From 893f3be0b036512d4f674781e4015fb0ce6d04ee Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Fri, 13 Nov 2026 02:51:23 -0500 Subject: [PATCH 02/10] docs(changelog): note the standalone binary no longer crashes on dprint-node --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84a81be64d..9b07ebc28a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,7 +85,10 @@ loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersi non-optional peer; a type package a published `.d.ts` imports goes in its `ALLOWED_TYPES_DEPS` with the reason. No `@metaobjectsdev/*` package declares `@types/node`: the 3 MB copy in the install comes through the libSQL driver (`@libsql/isomorphic-ws` → `@types/ws`), which is - unchanged here. + unchanged here. This also repairs the standalone binary (`bun run build:binary`): `--compile` + cannot embed dprint-node's native `.node` file, so a binary built from 1.2.0 crashed with + `Cannot find module './dprint-node.linux-x64-gnu.node'` on every command that loads the + codegen engine, `meta migrate` and `meta verify --db` included. ### Deprecated From b999caef0e6da937e37c0cde26a4a8b6be0879fd Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Fri, 13 Nov 2026 03:10:50 -0500 Subject: [PATCH 03/10] fix(codegen-ts): address review of the vendored printer - A formatter that fails to load now throws instead of silently emitting unformatted code; only a format error falls back. - The CLI pins @metaobjectsdev/codegen-ts/engine to the same build (dist vs src) as the package root, so both reach one poet.ts. - The engine entry re-exports only code/imp/joinCode/Import/Code from the printer, as it did from ts-poet. - Restore ts-poet's toString() call for non-plain placeholders. - Refresh the index.ts comment and narrow the CHANGELOG claims about legacy ts-poet imports and the standalone binary. --- CHANGELOG.md | 9 ++++++--- .../cli/src/lib/load-metaobjects-config.ts | 10 +++++++++- .../packages/codegen-ts/src/engine.ts | 9 ++++++--- .../packages/codegen-ts/src/index.ts | 20 +++++++++---------- .../packages/codegen-ts/src/poet.ts | 7 +++++-- 5 files changed, 36 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b07ebc28a..080ba4aa8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,8 +77,10 @@ loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersi ts-poet's configuration, so generated code is byte-identical: every codegen golden and conformance fixture is unchanged. `codegen-ts-react`, `codegen-ts-tanstack` and `codegen-ts-angular` drop their `ts-poet` dependency and import the printer from - `@metaobjectsdev/codegen-ts/engine`, which an ejected template also uses; a bare `ts-poet` - import in an older owned generator is still pointed at that entry by `meta gen`. A fresh npm + `@metaobjectsdev/codegen-ts/engine`, which an ejected template also uses. A bare `ts-poet` + import in an older owned generator is still pointed at that entry by `meta gen`, which + carries `code`, `imp`, `joinCode`, `Import` and the `Code` type; ts-poet's `saveFiles`, + `conditionalOutput` and its `toString` import options are not carried over. A fresh npm install of the CLI drops from 151 MB (134,525,165 bytes) to 133 MB (115,177,311 bytes) of `node_modules`. `scripts/check-peer-ranges.ts` now also fails a publishable package that requires `dprint-node`, `ts-poet` or any `@types/*` package through `dependencies` or a @@ -88,7 +90,8 @@ loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersi unchanged here. This also repairs the standalone binary (`bun run build:binary`): `--compile` cannot embed dprint-node's native `.node` file, so a binary built from 1.2.0 crashed with `Cannot find module './dprint-node.linux-x64-gnu.node'` on every command that loads the - codegen engine, `meta migrate` and `meta verify --db` included. + codegen engine, `meta migrate` and `meta verify --db` included. Those schema commands, the + binary's documented scope, now run; `meta gen` is still not supported inside the binary. ### Deprecated diff --git a/server/typescript/packages/cli/src/lib/load-metaobjects-config.ts b/server/typescript/packages/cli/src/lib/load-metaobjects-config.ts index 8eee582e84..2c086258cf 100644 --- a/server/typescript/packages/cli/src/lib/load-metaobjects-config.ts +++ b/server/typescript/packages/cli/src/lib/load-metaobjects-config.ts @@ -64,6 +64,14 @@ const CLI_PKG_PATHS: Record = { dist: "node_modules/@metaobjectsdev/migrate-ts/dist/index.js", src: "node_modules/@metaobjectsdev/migrate-ts/src/index.ts", }, + // Listed, not derived: the engine must resolve to the same build (dist vs src) as the root + // above, because both reach the vendored printer (src/poet.ts) and `Code` is recognised by + // `instanceof`. The derived-subpath path would follow Bun's "bun" condition to src/ while + // the root prefers dist/, splitting the class in two. + [CODEGEN_TS_ENGINE]: { + dist: "node_modules/@metaobjectsdev/codegen-ts/dist/engine.js", + src: "node_modules/@metaobjectsdev/codegen-ts/src/engine.ts", + }, "@metaobjectsdev/codegen-ts/generators": { dist: "node_modules/@metaobjectsdev/codegen-ts/dist/generators/index.js", src: "node_modules/@metaobjectsdev/codegen-ts/src/generators/index.ts", @@ -452,7 +460,7 @@ export async function loadMetaobjectsConfig(projectRoot: string): Promise Date: Fri, 13 Nov 2026 03:18:54 -0500 Subject: [PATCH 04/10] refactor: drop unused printer helpers; reuse the workspace walk in the peer guard Remove asOneline/oneline, Import.fromMaybeString and arrayOf from the vendored printer (no callers). check-peer-ranges enumerates packages through publish-set.mjs's enumerateWorkspacePackages instead of its own readdir walk. --- scripts/check-peer-ranges.ts | 53 +++++++++---------- .../packages/codegen-ts/src/poet.ts | 18 ------- 2 files changed, 24 insertions(+), 47 deletions(-) diff --git a/scripts/check-peer-ranges.ts b/scripts/check-peer-ranges.ts index 45b10b4eaf..3856588108 100755 --- a/scripts/check-peer-ranges.ts +++ b/scripts/check-peer-ranges.ts @@ -44,11 +44,10 @@ * Development use belongs in `devDependencies`; a genuine run-time need for the first two * must be an optional peer (`peerDependenciesMeta`). */ -import { readdirSync, readFileSync, existsSync } from "node:fs"; import { join } from "node:path"; +import { enumerateWorkspacePackages } from "./publish-set.mjs"; const REPO = join(import.meta.dir, ".."); -const ROOTS = ["server/typescript/packages", "client/web/packages"]; /** A version no real release will reach. If the range still matches, it has no ceiling. */ const BEYOND_ANY_REAL_VERSION = "9999.0.0"; @@ -78,33 +77,29 @@ const violations: Violation[] = []; const installOnlyDeps: { pkg: string; dep: string; field: string; why: string }[] = []; let checked = 0; -for (const root of ROOTS) { - for (const dir of readdirSync(join(REPO, root))) { - const manifest = join(REPO, root, dir, "package.json"); - if (!existsSync(manifest)) continue; - const pkg = JSON.parse(readFileSync(manifest, "utf8")) as { - name: string; private?: boolean; - dependencies?: Record; - peerDependencies?: Record; - peerDependenciesMeta?: Record; - }; - if (pkg.private === true) continue; // never published — its peers bind nobody - const required = [ - ...Object.keys(pkg.dependencies ?? {}).map((dep) => ({ dep, field: "dependencies" })), - ...Object.keys(pkg.peerDependencies ?? {}) - .filter((dep) => pkg.peerDependenciesMeta?.[dep]?.optional !== true) - .map((dep) => ({ dep, field: "peerDependencies (required)" })), - ]; - for (const { dep, field } of required) { - const why = installOnly(dep); - if (why === undefined || ALLOWED_TYPES_DEPS[`${pkg.name} ${dep}`] !== undefined) continue; - installOnlyDeps.push({ pkg: pkg.name, dep, field, why }); - } - for (const [peer, range] of Object.entries(pkg.peerDependencies ?? {})) { - checked++; - if (Bun.semver.satisfies(BEYOND_ANY_REAL_VERSION, range)) { - violations.push({ pkg: pkg.name, peer, range }); - } +for (const { pkg: manifest } of enumerateWorkspacePackages(REPO)) { + const pkg = manifest as { + name: string; private?: boolean; + dependencies?: Record; + peerDependencies?: Record; + peerDependenciesMeta?: Record; + }; + if (pkg.private === true) continue; // never published — its peers bind nobody + const required = [ + ...Object.keys(pkg.dependencies ?? {}).map((dep) => ({ dep, field: "dependencies" })), + ...Object.keys(pkg.peerDependencies ?? {}) + .filter((dep) => pkg.peerDependenciesMeta?.[dep]?.optional !== true) + .map((dep) => ({ dep, field: "peerDependencies (required)" })), + ]; + for (const { dep, field } of required) { + const why = installOnly(dep); + if (why === undefined || ALLOWED_TYPES_DEPS[`${pkg.name} ${dep}`] !== undefined) continue; + installOnlyDeps.push({ pkg: pkg.name, dep, field, why }); + } + for (const [peer, range] of Object.entries(pkg.peerDependencies ?? {})) { + checked++; + if (Bun.semver.satisfies(BEYOND_ANY_REAL_VERSION, range)) { + violations.push({ pkg: pkg.name, peer, range }); } } } diff --git a/server/typescript/packages/codegen-ts/src/poet.ts b/server/typescript/packages/codegen-ts/src/poet.ts index 400e835d8d..e12429a68c 100644 --- a/server/typescript/packages/codegen-ts/src/poet.ts +++ b/server/typescript/packages/codegen-ts/src/poet.ts @@ -53,8 +53,6 @@ export abstract class Node { export class Code extends Node { // Used by joinCode public trim = false; - /** @internal */ - oneline = false; // Cache the unformatted results of `toString` since we're immutable. /** @internal */ code: string | undefined; @@ -76,11 +74,6 @@ export class Code extends Node { return opts.format === false ? this.codeWithImports : maybePretty(this.codeWithImports); } - asOneline(): Code { - this.oneline = true; - return this; - } - get childNodes(): unknown[] { return this.placeholders; } @@ -125,9 +118,6 @@ export class Code extends Node { if (this.trim) { result = result.trim(); } - if (this.oneline) { - result = result.replace(/\n/g, ""); - } return result; } @@ -339,10 +329,6 @@ export abstract class Import extends Node { return Import.implicit(spec); } - static fromMaybeString(spec: string | Import): Import { - return typeof spec === "string" ? Import.from(spec) : spec; - } - /** * Defined if the symbol is typically imported from a barrel/etc. path, but is technically * defined in another file — so no import is emitted when it is used in that file. @@ -623,10 +609,6 @@ export function literalOf(object: unknown): Node { return new Literal(object); } -export function arrayOf(...elements: unknown[]): Node { - return literalOf(elements); -} - export function joinCode(chunks: Code[], opts: { on?: string; trim?: boolean } = {}): Code { const { on = "", trim = true } = opts; const literals: string[] = [""]; From 4f2a95026405c840c347bd44f8d47241e0ba8416 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Fri, 13 Nov 2026 14:47:28 -0500 Subject: [PATCH 05/10] fix(codegen-ts): import-path.ts takes the vendored printer; refresh install numbers after #567 --- CHANGELOG.md | 2 +- server/typescript/packages/codegen-ts/src/import-path.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 080ba4aa8c..853d446c6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -81,7 +81,7 @@ loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersi import in an older owned generator is still pointed at that entry by `meta gen`, which carries `code`, `imp`, `joinCode`, `Import` and the `Code` type; ts-poet's `saveFiles`, `conditionalOutput` and its `toString` import options are not carried over. A fresh npm - install of the CLI drops from 151 MB (134,525,165 bytes) to 133 MB (115,177,311 bytes) of + install of the CLI drops from 151 MB (134,641,989 bytes) to 133 MB (115,302,944 bytes) of `node_modules`. `scripts/check-peer-ranges.ts` now also fails a publishable package that requires `dprint-node`, `ts-poet` or any `@types/*` package through `dependencies` or a non-optional peer; a type package a published `.d.ts` imports goes in its `ALLOWED_TYPES_DEPS` diff --git a/server/typescript/packages/codegen-ts/src/import-path.ts b/server/typescript/packages/codegen-ts/src/import-path.ts index 83413ad6f8..c31b3e0e90 100644 --- a/server/typescript/packages/codegen-ts/src/import-path.ts +++ b/server/typescript/packages/codegen-ts/src/import-path.ts @@ -5,7 +5,7 @@ import { relative as posixRelative } from "node:path/posix"; import { PACKAGE_SEPARATOR, type MetaData } from "@metaobjectsdev/metadata"; -import { imp, Import } from "ts-poet"; +import { imp, Import } from "./poet.js"; import { withExt, withExtIfRelative, type ExtStyle, type RenderContext } from "./render-context.js"; export type OutputLayout = "flat" | "package"; @@ -77,7 +77,7 @@ export function valueObjectModuleSpecifier( } /** - * The ts-poet import of `` (the interface with `suffix` "", the Zod schema with + * The printer import of `` (the interface with `suffix` "", the Zod schema with * "InsertSchema") for the value object `field`'s `@objectRef` names, from a file in `fromPkg`. * Bound under the render context's local name: the value object's own name, or its * package-qualified alias when another object shares the short name From 2f6f74b4d840e92452a6829ea79ec60e3f038960 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Fri, 13 Nov 2026 14:59:16 -0500 Subject: [PATCH 06/10] no-mistakes(review): guard: refuse install-only deps via optionalDependencies; add real-check tests --- scripts/check-peer-ranges.test.ts | 62 ++++++++++++++ scripts/check-peer-ranges.ts | 129 +++++++++++++++++------------- 2 files changed, 136 insertions(+), 55 deletions(-) create mode 100644 scripts/check-peer-ranges.test.ts diff --git a/scripts/check-peer-ranges.test.ts b/scripts/check-peer-ranges.test.ts new file mode 100644 index 0000000000..e370a44eea --- /dev/null +++ b/scripts/check-peer-ranges.test.ts @@ -0,0 +1,62 @@ +// The install-time guard (scripts/check-peer-ranges.ts, second rule): a publishable +// package cannot land dprint-node, ts-poet or a run-time @types/* in an adopter's +// tree through ANY manifest field npm installs by default. npm installs +// `optionalDependencies` by default (it skips an entry only when that install +// fails), so the guard must refuse them there exactly as it does under +// `dependencies` — otherwise a published manifest reintroduces the ~25 MB +// formatter while the gate stays green. The escape hatch stays an optional peer +// (`peerDependenciesMeta`), which npm leaves to the adopter. All three cases are +// pinned by driving `peerRangeReport` — the same walk the CLI runs — against a +// synthetic workspace. +import { afterAll, describe, expect, test } from "bun:test"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { peerRangeReport } from "./check-peer-ranges"; + +const created: string[] = []; +afterAll(() => { for (const d of created) rmSync(d, { recursive: true, force: true }); }); + +/** A throwaway workspace whose single publishable package has this manifest body. */ +function workspaceWith(pkg: Record): string { + const root = mkdtempSync(join(tmpdir(), "check-peer-ranges-")); + created.push(root); + const dir = join(root, "server/typescript/packages/fixture"); + mkdirSync(dir, { recursive: true }); + writeFileSync(join(dir, "package.json"), JSON.stringify({ name: "@metaobjectsdev/fixture", version: "1.2.0", ...pkg })); + return root; +} + +describe("install-time guard — optionalDependencies are installed by default", () => { + test("dprint-node under optionalDependencies is refused, naming the field", () => { + const report = peerRangeReport(workspaceWith({ optionalDependencies: { "dprint-node": "^1.0.8" } })); + expect(report.installOnlyDeps).toEqual([ + expect.objectContaining({ pkg: "@metaobjectsdev/fixture", dep: "dprint-node", field: "optionalDependencies" }), + ]); + }); + + test("a run-time @types/* under optionalDependencies is refused too", () => { + const report = peerRangeReport(workspaceWith({ optionalDependencies: { "@types/node": "^22.0.0" } })); + expect(report.installOnlyDeps).toEqual([ + expect.objectContaining({ dep: "@types/node", field: "optionalDependencies" }), + ]); + }); + + test("a clean package passes: real dep, bounded peer counted", () => { + const report = peerRangeReport(workspaceWith({ + dependencies: { "drizzle-orm": "^0.36.0" }, + peerDependencies: { react: "^18.0.0" }, + })); + expect(report.installOnlyDeps).toEqual([]); + expect(report.unboundedPeers).toEqual([]); + expect(report.peerRanges).toBe(1); + }); + + test("the documented escape hatch holds: an optional peer is not refused", () => { + const report = peerRangeReport(workspaceWith({ + peerDependencies: { "dprint-node": "^1.0.8" }, + peerDependenciesMeta: { "dprint-node": { optional: true } }, + })); + expect(report.installOnlyDeps).toEqual([]); + }); +}); diff --git a/scripts/check-peer-ranges.ts b/scripts/check-peer-ranges.ts index 3856588108..cbc6800ad8 100755 --- a/scripts/check-peer-ranges.ts +++ b/scripts/check-peer-ranges.ts @@ -27,9 +27,10 @@ * by a warm lockfile. * * Second rule, same walk: no publishable package may REQUIRE an install-time package - * that nothing uses at run time, through `dependencies` or a non-optional peer (npm - * installs required peers automatically, so one declaration lands it in every - * `@metaobjectsdev/cli` install): + * that nothing uses at run time, through any field npm installs by default — + * `dependencies`, `optionalDependencies` (npm skips those only when the install fails) + * or a non-optional peer (npm installs required peers automatically) — so one + * declaration lands it in every `@metaobjectsdev/cli` install: * * - the native Biome formatter (`@biomejs/biome`, which pulls a ~94 MB per-platform * `@biomejs/cli-*` binary). Codegen formats through the WebAssembly build @@ -47,8 +48,6 @@ import { join } from "node:path"; import { enumerateWorkspacePackages } from "./publish-set.mjs"; -const REPO = join(import.meta.dir, ".."); - /** A version no real release will reach. If the range still matches, it has no ceiling. */ const BEYOND_ANY_REAL_VERSION = "9999.0.0"; @@ -73,61 +72,81 @@ function installOnly(name: string): string | undefined { const ALLOWED_TYPES_DEPS: Record = {}; interface Violation { pkg: string; peer: string; range: string } -const violations: Violation[] = []; -const installOnlyDeps: { pkg: string; dep: string; field: string; why: string }[] = []; -let checked = 0; +export interface InstallOnlyDep { pkg: string; dep: string; field: string; why: string } -for (const { pkg: manifest } of enumerateWorkspacePackages(REPO)) { - const pkg = manifest as { - name: string; private?: boolean; - dependencies?: Record; - peerDependencies?: Record; - peerDependenciesMeta?: Record; - }; - if (pkg.private === true) continue; // never published — its peers bind nobody - const required = [ - ...Object.keys(pkg.dependencies ?? {}).map((dep) => ({ dep, field: "dependencies" })), - ...Object.keys(pkg.peerDependencies ?? {}) - .filter((dep) => pkg.peerDependenciesMeta?.[dep]?.optional !== true) - .map((dep) => ({ dep, field: "peerDependencies (required)" })), - ]; - for (const { dep, field } of required) { - const why = installOnly(dep); - if (why === undefined || ALLOWED_TYPES_DEPS[`${pkg.name} ${dep}`] !== undefined) continue; - installOnlyDeps.push({ pkg: pkg.name, dep, field, why }); - } - for (const [peer, range] of Object.entries(pkg.peerDependencies ?? {})) { - checked++; - if (Bun.semver.satisfies(BEYOND_ANY_REAL_VERSION, range)) { - violations.push({ pkg: pkg.name, peer, range }); +/** + * The walk behind both rules, over the workspace at `repo`. Returns what the CLI + * prints and its exit decision, so the gate's own test can drive the real check + * against a synthetic workspace (production callers pass nothing but the repo root). + */ +export function peerRangeReport(repo: string): { + peerRanges: number; + unboundedPeers: Violation[]; + installOnlyDeps: InstallOnlyDep[]; +} { + const violations: Violation[] = []; + const installOnlyDeps: InstallOnlyDep[] = []; + let checked = 0; + + for (const { pkg: manifest } of enumerateWorkspacePackages(repo)) { + const pkg = manifest as { + name: string; private?: boolean; + dependencies?: Record; + optionalDependencies?: Record; + peerDependencies?: Record; + peerDependenciesMeta?: Record; + }; + if (pkg.private === true) continue; // never published — its peers bind nobody + const installed = [ + ...Object.keys(pkg.dependencies ?? {}).map((dep) => ({ dep, field: "dependencies" })), + ...Object.keys(pkg.optionalDependencies ?? {}).map((dep) => ({ dep, field: "optionalDependencies" })), + ...Object.keys(pkg.peerDependencies ?? {}) + .filter((dep) => pkg.peerDependenciesMeta?.[dep]?.optional !== true) + .map((dep) => ({ dep, field: "peerDependencies (required)" })), + ]; + for (const { dep, field } of installed) { + const why = installOnly(dep); + if (why === undefined || ALLOWED_TYPES_DEPS[`${pkg.name} ${dep}`] !== undefined) continue; + installOnlyDeps.push({ pkg: pkg.name, dep, field, why }); + } + for (const [peer, range] of Object.entries(pkg.peerDependencies ?? {})) { + checked++; + if (Bun.semver.satisfies(BEYOND_ANY_REAL_VERSION, range)) { + violations.push({ pkg: pkg.name, peer, range }); + } } } + return { peerRanges: checked, unboundedPeers: violations, installOnlyDeps }; } -if (violations.length > 0) { - console.error("✖ unbounded peer ranges — each accepts any future major:\n"); - for (const v of violations) { - console.error(` ${v.pkg}\n "${v.peer}": "${v.range}"`); +if (import.meta.main) { + const { peerRanges, unboundedPeers, installOnlyDeps } = peerRangeReport(join(import.meta.dir, "..")); + + if (unboundedPeers.length > 0) { + console.error("✖ unbounded peer ranges — each accepts any future major:\n"); + for (const v of unboundedPeers) { + console.error(` ${v.pkg}\n "${v.peer}": "${v.range}"`); + } + console.error( + `\nGive each an upper bound at the next major you have NOT validated —` + + `\n "^8.20.0" or ">=0.36.0 <1.0.0"` + + `\nA bound turns "the adopter's build mysteriously breaks" into a resolution` + + `\nerror at install time, naming the conflict. Raising a bound is a deliberate` + + `\nact: test against the new major first, then widen it.\n`, + ); + process.exit(1); } - console.error( - `\nGive each an upper bound at the next major you have NOT validated —` + - `\n "^8.20.0" or ">=0.36.0 <1.0.0"` + - `\nA bound turns "the adopter's build mysteriously breaks" into a resolution` + - `\nerror at install time, naming the conflict. Raising a bound is a deliberate` + - `\nact: test against the new major first, then widen it.\n`, - ); - process.exit(1); -} -if (installOnlyDeps.length > 0) { - console.error("✖ a package nothing uses at run time is required at install time:\n"); - for (const v of installOnlyDeps) console.error(` ${v.pkg}\n ${v.field}: "${v.dep}" — ${v.why}`); - console.error( - `\nnpm installs it into every adopter's tree. Move it to devDependencies, or, if shipped` + - `\ncode really needs it, mark the peer optional in peerDependenciesMeta (an @types/*` + - `\npackage a published .d.ts imports goes in ALLOWED_TYPES_DEPS instead).\n`, - ); - process.exit(1); -} + if (installOnlyDeps.length > 0) { + console.error("✖ a package nothing uses at run time is required at install time:\n"); + for (const v of installOnlyDeps) console.error(` ${v.pkg}\n ${v.field}: "${v.dep}" — ${v.why}`); + console.error( + `\nnpm installs it into every adopter's tree. Move it to devDependencies, or, if shipped` + + `\ncode really needs it, mark the peer optional in peerDependenciesMeta (an @types/*` + + `\npackage a published .d.ts imports goes in ALLOWED_TYPES_DEPS instead).\n`, + ); + process.exit(1); + } -console.log(`peer-range bounds: OK (${checked} peer ranges, all bounded)`); + console.log(`peer-range bounds: OK (${peerRanges} peer ranges, all bounded)`); +} From 934ba2338dce03b915bf41b9712cd81a9f76411d Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Fri, 13 Nov 2026 15:03:05 -0500 Subject: [PATCH 07/10] no-mistakes(review): apply reviewer inline fixes --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 853d446c6a..1fdc1d9b55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,8 +83,9 @@ loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersi `conditionalOutput` and its `toString` import options are not carried over. A fresh npm install of the CLI drops from 151 MB (134,641,989 bytes) to 133 MB (115,302,944 bytes) of `node_modules`. `scripts/check-peer-ranges.ts` now also fails a publishable package that - requires `dprint-node`, `ts-poet` or any `@types/*` package through `dependencies` or a - non-optional peer; a type package a published `.d.ts` imports goes in its `ALLOWED_TYPES_DEPS` + requires `dprint-node`, `ts-poet` or any `@types/*` package through `dependencies`, + `optionalDependencies` or a non-optional peer; a type package a published `.d.ts` imports goes + in its `ALLOWED_TYPES_DEPS` with the reason. No `@metaobjectsdev/*` package declares `@types/node`: the 3 MB copy in the install comes through the libSQL driver (`@libsql/isomorphic-ws` → `@types/ws`), which is unchanged here. This also repairs the standalone binary (`bun run build:binary`): `--compile` From bf458713d1757f30b4b29902283fb77b462cee79 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Fri, 13 Nov 2026 15:04:10 -0500 Subject: [PATCH 08/10] no-mistakes(review): wire peer-ranges guard test into gate_peer_ranges lane --- scripts/ci-local.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ci-local.sh b/scripts/ci-local.sh index 073f695ed3..6583acfd17 100755 --- a/scripts/ci-local.sh +++ b/scripts/ci-local.sh @@ -369,7 +369,7 @@ gate_assess_prompt() { node scripts/build-assess-prompt.mjs --out /dev/null && n # no peer warning. A workspace CANNOT catch this: devDeps are pinned and bun.lock # freezes them, so every test runs against the version we chose, never the one an # adopter resolves. The check reads MANIFESTS, so it needs no network. -gate_peer_ranges() { bun scripts/check-peer-ranges.ts; } +gate_peer_ranges() { bun scripts/check-peer-ranges.ts && bun test scripts/check-peer-ranges.test.ts; } # ── this repo's OWNED copies of the reference templates must BE the templates ── # owned-copies-current.test.ts already asserts byte-identity; this runs the same check From 40fb73e2ea45877b980727cc4ae6639acce02cb1 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Fri, 13 Nov 2026 16:49:12 -0500 Subject: [PATCH 09/10] no-mistakes(test): apply changes made during test validation --- scripts/publish-set.mjs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/publish-set.mjs b/scripts/publish-set.mjs index 15d420404c..e67231ff28 100755 --- a/scripts/publish-set.mjs +++ b/scripts/publish-set.mjs @@ -65,6 +65,7 @@ export function enumerateWorkspacePackages(root = ROOT) { return found; } +/** @param {{ dependencies?: Record }} pkg */ const siblingDeps = (pkg) => Object.keys(pkg.dependencies || {}).filter((k) => k.startsWith("@metaobjectsdev/")); @@ -122,7 +123,7 @@ export function publishSet({ root = ROOT, tierOrder = TIER_ORDER } = {}) { // edit, which is otherwise invisible until a mid-publish registry window. const position = new Map(ordered.map((p, i) => [p.pkg.name, i])); const inverted = ordered.flatMap((p, i) => - siblingDeps(p.pkg).filter((d) => position.get(d) > i).map((d) => `${p.pkg.name} (#${i}) depends on ${d} (#${position.get(d)})`), + siblingDeps(p.pkg).filter((d) => (position.get(d) ?? -1) > i).map((d) => `${p.pkg.name} (#${i}) depends on ${d} (#${position.get(d)})`), ); if (inverted.length) { throw new Error( @@ -140,7 +141,7 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) try { result = publishSet(); } catch (e) { - console.error(`\x1b[31m✖ publish-set: ${e.message}\x1b[0m`); + console.error(`\x1b[31m✖ publish-set: ${e instanceof Error ? e.message : String(e)}\x1b[0m`); process.exit(1); } const { lockstep, set } = result; From 1f9de62455148632a613205f6e6ebc4424893b80 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Fri, 13 Nov 2026 21:24:42 -0500 Subject: [PATCH 10/10] no-mistakes(document): Doc sync: ts-poet printer, guard, binary --- AGENTS.md | 2 +- docs/features/own-your-codegen.md | 3 +-- scripts/ci-local.sh | 2 ++ 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 8808485aa9..81493b92ef 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -260,7 +260,7 @@ import { EntityFetcherProvider, EntityGrid } from "@metaobjectsdev/tanstack"; - **TS runtime**: Bun-first for development (zero-config TS, native test runner). Node-compatible for distribution; users install via npm/pnpm/bun without lock-in to Bun's runtime. - **Module system**: ESM only. No CommonJS, no transpile step required. - **Storage format**: JSON files in `metaobjects/meta..json` at project root. `.metaobjects/.gen-state/` holds the codegen merge base: the snapshot **bodies are gitignored** (a second full copy of all generated output), but **`.hashes.json` is COMMITTED** — one hash per generated path, and the only thing that lets `meta gen` tell "this file is exactly what I wrote" from "somebody edited this" on a machine that did not generate it. Ignore it and a fresh clone or CI runner silently overwrites hand edits; commit it and an edited file is refused by name instead. -- **Codegen substrate**: ts-poet for greenfield emit, ts-morph for in-place edits, Biome for format pass, `git merge-file --diff3` for hand-edit-preserving regen. +- **Codegen substrate**: the vendored ts-poet printer (`codegen-ts/src/poet.ts`) for greenfield emit, ts-morph for in-place edits, Biome for format pass, `git merge-file --diff3` for hand-edit-preserving regen. - **Runtime substrate**: Kysely for TS (user-provided connection, async-only). - **Migration substrate**: Postgres + SQLite for TS v0.3. - **Metadata location**: resolved via `resolveCollection()` (`@metaobjectsdev/sdk`) — the single authority. `metaobjects/` is the **default value of `sources`** and nothing else: no other module, command or user-facing message may assert that a directory of that name exists or is where metadata lives. Exactly five sites may name it — `sdk/src/metadata-files.ts` (`DEFAULT_METADATA_DIR`, its single definition), `sdk/src/sources.ts` (`DEFAULT_SOURCES`, **the** default), `sdk/src/collection.ts` (inside `resolveCollection`, *applying* that default), `sdk/src/index.ts` (the barrel re-export of the constant, no use), and `cli/src/commands/init.ts` (the scaffolder **writing** the layout). Enforced by `sdk/test/no-hardcoded-metadata-dir.test.ts`, whose allowlist demands a written reason per entry. See [docs/features/metadata-sources.md](docs/features/metadata-sources.md). diff --git a/docs/features/own-your-codegen.md b/docs/features/own-your-codegen.md index f98ab492f5..d7f885efa3 100644 --- a/docs/features/own-your-codegen.md +++ b/docs/features/own-your-codegen.md @@ -109,8 +109,7 @@ holds), the naming tables and the type-mapping tables (`names.ts`, `naming.ts`, projection and report lowering that `meta migrate` and `meta verify` run. An owned file imports the engine from the package's engine entry, `@metaobjectsdev/codegen-ts/engine`, which also exports the code printer's `code`, `imp` and `joinCode` (`poet.ts`, the package's -vendored copy of `ts-poet`). The list is -`ENGINE_MODULES`, exported from that entry; a module +vendored copy of `ts-poet`). The list is `ENGINE_MODULES`, exported from that entry; a module that is not on it and that a generator reaches is copied. An owned generator therefore imports no packaged module that returns emitted text, and a test ejects each generator alone into an empty project to prove it: it compiles as your `tsconfig.codegen.json` checks it and diff --git a/scripts/ci-local.sh b/scripts/ci-local.sh index 6583acfd17..c8b84efb12 100755 --- a/scripts/ci-local.sh +++ b/scripts/ci-local.sh @@ -369,6 +369,8 @@ gate_assess_prompt() { node scripts/build-assess-prompt.mjs --out /dev/null && n # no peer warning. A workspace CANNOT catch this: devDeps are pinned and bun.lock # freezes them, so every test runs against the version we chose, never the one an # adopter resolves. The check reads MANIFESTS, so it needs no network. +# The same walk also refuses install-only deps (dprint-node, ts-poet, @types/*) in +# dependencies, optionalDependencies or a required peer; see scripts/check-peer-ranges.ts. gate_peer_ranges() { bun scripts/check-peer-ranges.ts && bun test scripts/check-peer-ranges.test.ts; } # ── this repo's OWNED copies of the reference templates must BE the templates ──