@metaobjectsdev/codegen-ts 0.7.0-rc.12 → 0.7.0-rc.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,34 +17,43 @@ import { existsSync, readFileSync } from "node:fs";
17
17
  import { join, resolve, dirname } from "node:path";
18
18
  import { fileURLToPath } from "node:url";
19
19
 
20
- /** Walk up from `start` until we find a `package.json` (i.e., the codegen-ts
21
- * package root), then return `<pkg-root>/templates`. Works the same way
22
- * from `src/render-engine/framework-provider.ts` (during dev) and
23
- * `dist/render-engine/framework-provider.js` (after `npm install`) because
24
- * in both cases we shipped at the package root. */
20
+ /** Canonical shipped template used to verify a candidate framework
21
+ * templates directory actually contains our defaults. Without this check a
22
+ * hoisted-install layout (pnpm/bun workspaces) can walk up to a CONSUMER
23
+ * package.json and silently return a templates dir that doesn't exist. */
24
+ const CANONICAL_TEMPLATE_REL = "docs/entity-page.md.mustache";
25
+
26
+ /** Walk up from `start` until we find a `package.json` whose neighbour
27
+ * `templates/` directory contains our canonical shipped template (i.e., the
28
+ * codegen-ts package root). Works the same way from
29
+ * `src/render-engine/framework-provider.ts` (during dev) and
30
+ * `dist/render-engine/framework-provider.js` (after `npm install`). */
25
31
  function findFrameworkTemplatesDir(start: string): string {
26
32
  let dir = start;
27
- for (let i = 0; i < 12; i++) {
33
+ while (true) {
28
34
  const pkgJson = join(dir, "package.json");
29
35
  if (existsSync(pkgJson)) {
30
- return join(dir, "templates");
36
+ const templatesDir = join(dir, "templates");
37
+ // Assert we landed at the codegen-ts package root, not a consumer's.
38
+ if (existsSync(join(templatesDir, CANONICAL_TEMPLATE_REL))) {
39
+ return templatesDir;
40
+ }
31
41
  }
32
42
  const parent = dirname(dir);
33
43
  if (parent === dir) break;
34
44
  dir = parent;
35
45
  }
36
- // Last resort — caller will get an unresolved-ref error when it tries to
37
- // use a framework template that isn't on disk.
38
- return join(start, "templates");
46
+ throw new Error(
47
+ `framework templates dir unresolved: walked up from ${start} without finding a package.json ` +
48
+ `whose templates/${CANONICAL_TEMPLATE_REL} exists. This usually means codegen-ts was installed ` +
49
+ `via a hoisted layout (pnpm/bun workspaces) into an unexpected location, or the published ` +
50
+ `tarball is missing the templates/ directory.`,
51
+ );
39
52
  }
40
53
 
41
- const SELF_DIR = (() => {
42
- try {
43
- return dirname(fileURLToPath(import.meta.url));
44
- } catch {
45
- return process.cwd();
46
- }
47
- })();
54
+ // In ESM (CLAUDE.md: "ESM only. No CommonJS."), `import.meta.url` is
55
+ // guaranteed to be a file: URL; no defensive try/catch needed.
56
+ const SELF_DIR = dirname(fileURLToPath(import.meta.url));
48
57
 
49
58
  const FRAMEWORK_TEMPLATES_DIR = findFrameworkTemplatesDir(SELF_DIR);
50
59
 
package/src/runner.ts CHANGED
@@ -174,6 +174,7 @@ export async function runGen(opts: RunGenOpts): Promise<RunGenResult> {
174
174
  outputLayout: selfTarget.outputLayout,
175
175
  },
176
176
  renderContext,
177
+ ...(projectRoot !== undefined && { projectRoot }),
177
178
  warn: (msg) => warnings.push(`[${generator.name}] ${msg}`),
178
179
  };
179
180