turbine-orm 0.18.0 → 0.19.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.
@@ -8,9 +8,18 @@
8
8
  *
9
9
  * Strategy:
10
10
  * 1. If the file we're about to import ends in `.ts` / `.mts` / `.cts`,
11
- * probe whether `tsx/esm` is resolvable from the user's CWD.
12
- * 2. If yes, call `module.register('tsx/esm', ...)` ONCE per process.
13
- * 3. If no, surface an actionable error telling the user to install `tsx`.
11
+ * probe whether `tsx` is resolvable from the user's CWD.
12
+ * 2. Prefer tsx's supported programmatic API, `tsx/esm/api`'s `register()`.
13
+ * Calling Node's `module.register('tsx/esm', ...)` directly throws
14
+ * "tsx must be loaded with --import instead of --loader" on every Node
15
+ * version that has `module.register()` (>= 20.6) — tsx's hook file
16
+ * guards against being loaded that way. The `tsx/esm/api` entry point
17
+ * is the documented path and works everywhere `module.register()` does.
18
+ * 3. Fall back to `module.register('tsx/esm', ...)` only for very old tsx
19
+ * versions (< 4.0) that predate `tsx/esm/api`.
20
+ * 4. If tsx isn't installed, or registration genuinely fails, surface an
21
+ * actionable error — including the REAL underlying error message, never
22
+ * a misdiagnosed "tsx is not installed".
14
23
  *
15
24
  * `tsx` is intentionally NOT a runtime dependency — many projects already
16
25
  * have it, and adding a heavy dev tool to a 1-dependency ORM would be silly.
@@ -50,6 +59,14 @@ export function canResolveTsx(resolver) {
50
59
  }
51
60
  }
52
61
  let tsLoaderState = null;
62
+ let tsLoaderError = null;
63
+ /**
64
+ * The underlying error message from the last failed registration attempt,
65
+ * or null. Lets the CLI report the REAL cause instead of guessing.
66
+ */
67
+ export function getTsLoaderError() {
68
+ return tsLoaderError;
69
+ }
53
70
  /**
54
71
  * Register the tsx ESM loader so subsequent dynamic imports of `.ts` files
55
72
  * work. Safe to call multiple times — internal flag prevents double registration.
@@ -57,17 +74,51 @@ let tsLoaderState = null;
57
74
  * Returns:
58
75
  * - 'registered' loader was successfully registered this call
59
76
  * - 'already' a loader was previously registered (idempotent)
60
- * - 'unsupported' Node lacks `module.register()` (Node < 20.6)
77
+ * - 'unsupported' Node lacks `module.register()` (Node < 20.6) and tsx has
78
+ * no programmatic API to fall back to
61
79
  * - 'missing' `tsx` is not installed in the user's project
80
+ * - 'failed' tsx IS installed but registration threw — see
81
+ * {@link getTsLoaderError} for the underlying message
62
82
  */
63
83
  export async function registerTsLoader() {
64
84
  if (tsLoaderState === 'registered' || tsLoaderState === 'already') {
65
85
  return 'already';
66
86
  }
87
+ const userRequire = createRequire(`${process.cwd()}/`);
88
+ // Preferred: tsx's supported programmatic API (tsx >= 4.0).
89
+ let apiPath = null;
90
+ try {
91
+ apiPath = userRequire.resolve('tsx/esm/api');
92
+ }
93
+ catch {
94
+ apiPath = null;
95
+ }
96
+ if (apiPath) {
97
+ try {
98
+ const api = (await import(pathToFileURL(apiPath).href));
99
+ if (typeof api.register !== 'function') {
100
+ throw new Error(`tsx/esm/api resolved at ${apiPath} but exports no register() function`);
101
+ }
102
+ api.register();
103
+ tsLoaderState = 'registered';
104
+ tsLoaderError = null;
105
+ return 'registered';
106
+ }
107
+ catch (err) {
108
+ tsLoaderState = 'failed';
109
+ tsLoaderError = err instanceof Error ? err.message : String(err);
110
+ return 'failed';
111
+ }
112
+ }
113
+ // tsx/esm/api not resolvable — is tsx installed at all?
67
114
  if (!canResolveTsx()) {
68
115
  tsLoaderState = 'missing';
69
116
  return 'missing';
70
117
  }
118
+ // Legacy fallback for tsx < 4.0 (no tsx/esm/api): Node's module.register.
119
+ // On tsx >= 4.19 this path throws ("tsx must be loaded with --import
120
+ // instead of --loader") — but those versions all ship tsx/esm/api, so we
121
+ // only land here for genuinely old installs.
71
122
  try {
72
123
  const mod = await import('node:module');
73
124
  const register = mod.register;
@@ -77,15 +128,18 @@ export async function registerTsLoader() {
77
128
  }
78
129
  register('tsx/esm', pathToFileURL(`${process.cwd()}/`));
79
130
  tsLoaderState = 'registered';
131
+ tsLoaderError = null;
80
132
  return 'registered';
81
133
  }
82
- catch {
83
- tsLoaderState = 'missing';
84
- return 'missing';
134
+ catch (err) {
135
+ tsLoaderState = 'failed';
136
+ tsLoaderError = err instanceof Error ? err.message : String(err);
137
+ return 'failed';
85
138
  }
86
139
  }
87
140
  /** Reset the loader state — used by unit tests only. */
88
141
  export function _resetTsLoaderStateForTests() {
89
142
  tsLoaderState = null;
143
+ tsLoaderError = null;
90
144
  }
91
145
  //# sourceMappingURL=loader.js.map