@prenta/cli 1.2.0 → 1.2.1

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.
@@ -15,18 +15,70 @@ export function toProjectFileUrl(filePath: string): string {
15
15
  return pathToFileURL(path.resolve(filePath)).href
16
16
  }
17
17
 
18
+ /**
19
+ * Install tsx's process-wide hooks once: the ESM loader and the CommonJS
20
+ * `require` hook.
21
+ *
22
+ * Both are needed. A scaffolded project has no `"type": "module"`, so Node
23
+ * classifies its `.ts` files as CommonJS. The ESM loader compiles the entry
24
+ * file (`generated/prisma/client.ts`), but that file's own imports then run as
25
+ * `require('./internal/class')`, and on Node 22 only the CommonJS hook can
26
+ * resolve an extensionless TypeScript sibling. Without it every command that
27
+ * opens the project database fails with "Cannot find module './internal/class'".
28
+ */
29
+ async function registerTsx(): Promise<void> {
30
+ if (registered) return
31
+ const [esm, cjs] = await Promise.all([import('tsx/esm/api'), import('tsx/cjs/api')])
32
+ esm.register()
33
+ cjs.register()
34
+ registered = true
35
+ }
36
+
37
+ /**
38
+ * The exports a loaded project module actually declared.
39
+ *
40
+ * A scaffolded project has no `"type": "module"`, so tsx compiles its
41
+ * TypeScript to CommonJS: `export default config` and `export const header`
42
+ * become `module.exports = { __esModule: true, default: config, header }`.
43
+ * Imported from this ESM CLI, that whole object arrives as the namespace's
44
+ * `default`, one level above what the file exported. Reading the namespace
45
+ * directly therefore finds `{ default: config }` where the config should be
46
+ * (an object with no collections, plugins or storage) and no named exports at
47
+ * all, so every command that loaded `prenta.config.ts` ran as if it were empty.
48
+ */
49
+ export function projectModuleExports(mod: unknown): Record<string, unknown> {
50
+ if (!mod || typeof mod !== 'object') return {}
51
+ const namespace = mod as Record<string, unknown>
52
+ const outer = namespace.default
53
+ if (
54
+ outer &&
55
+ typeof outer === 'object' &&
56
+ !Array.isArray(outer) &&
57
+ (outer as Record<string, unknown>).__esModule === true
58
+ ) {
59
+ return outer as Record<string, unknown>
60
+ }
61
+ return namespace
62
+ }
63
+
64
+ /**
65
+ * The default export of a loaded project module, or the named export
66
+ * `fallbackName` when the file has no default.
67
+ */
68
+ export function projectDefaultExport(mod: unknown, fallbackName?: string): unknown {
69
+ const exported = projectModuleExports(mod)
70
+ if (exported.default !== undefined) return exported.default
71
+ return fallbackName ? exported[fallbackName] : undefined
72
+ }
73
+
18
74
  /**
19
75
  * Load a consumer TypeScript file (prenta.config.ts, lib/prisma.ts,
20
- * generated/prisma/client.ts) via tsx's process-wide ESM hook.
76
+ * generated/prisma/client.ts) via tsx's process-wide hooks.
21
77
  *
22
78
  * Do not use `tsImport()` — its namespaced loader is what produced the
23
79
  * Windows `?namespace=` crashes on adapter-pg and then enums.ts.
24
80
  */
25
81
  export async function importProjectTypescript(filePath: string): Promise<unknown> {
26
- if (!registered) {
27
- const { register } = await import('tsx/esm/api')
28
- register()
29
- registered = true
30
- }
82
+ await registerTsx()
31
83
  return import(toProjectFileUrl(filePath))
32
84
  }
@@ -1,5 +1,5 @@
1
1
  import { resolve } from 'node:path'
2
- import { importProjectTypescript } from './import-typescript.js'
2
+ import { importProjectTypescript, projectDefaultExport } from './import-typescript.js'
3
3
  import { logger } from './logger.js'
4
4
 
5
5
  /**
@@ -15,7 +15,7 @@ export async function loadProjectConfig(configPath: string): Promise<void> {
15
15
  default?: unknown
16
16
  config?: unknown
17
17
  }
18
- const config = mod.default ?? mod.config
18
+ const config = projectDefaultExport(mod, 'config')
19
19
  if (!config || typeof config !== 'object') return
20
20
  setPrentaConfig(config as never)
21
21