@prisma-next/cli 0.3.0-dev.5 → 0.3.0-dev.53

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.
Files changed (130) hide show
  1. package/README.md +145 -74
  2. package/dist/cli.d.mts +1 -0
  3. package/dist/cli.js +1 -2376
  4. package/dist/cli.mjs +169 -0
  5. package/dist/cli.mjs.map +1 -0
  6. package/dist/client-BSZKpZTF.mjs +711 -0
  7. package/dist/client-BSZKpZTF.mjs.map +1 -0
  8. package/dist/commands/contract-emit.d.mts +7 -0
  9. package/dist/commands/contract-emit.d.mts.map +1 -0
  10. package/dist/commands/contract-emit.mjs +147 -0
  11. package/dist/commands/contract-emit.mjs.map +1 -0
  12. package/dist/commands/db-init.d.mts +7 -0
  13. package/dist/commands/db-init.d.mts.map +1 -0
  14. package/dist/commands/db-init.mjs +179 -0
  15. package/dist/commands/db-init.mjs.map +1 -0
  16. package/dist/commands/db-introspect.d.mts +7 -0
  17. package/dist/commands/db-introspect.d.mts.map +1 -0
  18. package/dist/commands/db-introspect.mjs +120 -0
  19. package/dist/commands/db-introspect.mjs.map +1 -0
  20. package/dist/commands/db-schema-verify.d.mts +7 -0
  21. package/dist/commands/db-schema-verify.d.mts.map +1 -0
  22. package/dist/commands/db-schema-verify.mjs +116 -0
  23. package/dist/commands/db-schema-verify.mjs.map +1 -0
  24. package/dist/commands/db-sign.d.mts +7 -0
  25. package/dist/commands/db-sign.d.mts.map +1 -0
  26. package/dist/commands/db-sign.mjs +138 -0
  27. package/dist/commands/db-sign.mjs.map +1 -0
  28. package/dist/commands/db-verify.d.mts +7 -0
  29. package/dist/commands/db-verify.d.mts.map +1 -0
  30. package/dist/commands/db-verify.mjs +129 -0
  31. package/dist/commands/db-verify.mjs.map +1 -0
  32. package/dist/config-loader-BJ8HsEdA.mjs +42 -0
  33. package/dist/config-loader-BJ8HsEdA.mjs.map +1 -0
  34. package/dist/{config-loader.d.ts → config-loader.d.mts} +8 -3
  35. package/dist/config-loader.d.mts.map +1 -0
  36. package/dist/config-loader.mjs +3 -0
  37. package/dist/exports/config-types.d.mts +2 -0
  38. package/dist/exports/config-types.mjs +3 -0
  39. package/dist/exports/control-api.d.mts +433 -0
  40. package/dist/exports/control-api.d.mts.map +1 -0
  41. package/dist/exports/control-api.mjs +96 -0
  42. package/dist/exports/control-api.mjs.map +1 -0
  43. package/dist/{load-ts-contract.d.ts → exports/index.d.mts} +10 -5
  44. package/dist/exports/index.d.mts.map +1 -0
  45. package/dist/exports/index.mjs +132 -0
  46. package/dist/exports/index.mjs.map +1 -0
  47. package/dist/result-handler-BZPY7HX4.mjs +1029 -0
  48. package/dist/result-handler-BZPY7HX4.mjs.map +1 -0
  49. package/package.json +48 -37
  50. package/src/commands/contract-emit.ts +205 -111
  51. package/src/commands/db-init.ts +258 -359
  52. package/src/commands/db-introspect.ts +151 -184
  53. package/src/commands/db-schema-verify.ts +151 -149
  54. package/src/commands/db-sign.ts +202 -200
  55. package/src/commands/db-verify.ts +181 -155
  56. package/src/control-api/client.ts +610 -0
  57. package/src/control-api/operations/contract-emit.ts +161 -0
  58. package/src/control-api/operations/db-init.ts +281 -0
  59. package/src/control-api/types.ts +475 -0
  60. package/src/exports/control-api.ts +48 -0
  61. package/src/load-ts-contract.ts +16 -11
  62. package/src/utils/cli-errors.ts +1 -1
  63. package/src/utils/framework-components.ts +11 -30
  64. package/src/utils/output.ts +16 -10
  65. package/src/utils/progress-adapter.ts +86 -0
  66. package/dist/chunk-464LNZCE.js +0 -134
  67. package/dist/chunk-464LNZCE.js.map +0 -1
  68. package/dist/chunk-BZMBKEEQ.js +0 -997
  69. package/dist/chunk-BZMBKEEQ.js.map +0 -1
  70. package/dist/chunk-HWYQOCAJ.js +0 -47
  71. package/dist/chunk-HWYQOCAJ.js.map +0 -1
  72. package/dist/chunk-ZKYEJROM.js +0 -94
  73. package/dist/chunk-ZKYEJROM.js.map +0 -1
  74. package/dist/cli.d.ts +0 -2
  75. package/dist/cli.d.ts.map +0 -1
  76. package/dist/cli.js.map +0 -1
  77. package/dist/commands/contract-emit.d.ts +0 -3
  78. package/dist/commands/contract-emit.d.ts.map +0 -1
  79. package/dist/commands/contract-emit.js +0 -9
  80. package/dist/commands/contract-emit.js.map +0 -1
  81. package/dist/commands/db-init.d.ts +0 -3
  82. package/dist/commands/db-init.d.ts.map +0 -1
  83. package/dist/commands/db-init.js +0 -341
  84. package/dist/commands/db-init.js.map +0 -1
  85. package/dist/commands/db-introspect.d.ts +0 -3
  86. package/dist/commands/db-introspect.d.ts.map +0 -1
  87. package/dist/commands/db-introspect.js +0 -190
  88. package/dist/commands/db-introspect.js.map +0 -1
  89. package/dist/commands/db-schema-verify.d.ts +0 -3
  90. package/dist/commands/db-schema-verify.d.ts.map +0 -1
  91. package/dist/commands/db-schema-verify.js +0 -164
  92. package/dist/commands/db-schema-verify.js.map +0 -1
  93. package/dist/commands/db-sign.d.ts +0 -3
  94. package/dist/commands/db-sign.d.ts.map +0 -1
  95. package/dist/commands/db-sign.js +0 -199
  96. package/dist/commands/db-sign.js.map +0 -1
  97. package/dist/commands/db-verify.d.ts +0 -3
  98. package/dist/commands/db-verify.d.ts.map +0 -1
  99. package/dist/commands/db-verify.js +0 -173
  100. package/dist/commands/db-verify.js.map +0 -1
  101. package/dist/config-loader.d.ts.map +0 -1
  102. package/dist/config-loader.js +0 -7
  103. package/dist/config-loader.js.map +0 -1
  104. package/dist/exports/config-types.d.ts +0 -3
  105. package/dist/exports/config-types.d.ts.map +0 -1
  106. package/dist/exports/config-types.js +0 -6
  107. package/dist/exports/config-types.js.map +0 -1
  108. package/dist/exports/index.d.ts +0 -4
  109. package/dist/exports/index.d.ts.map +0 -1
  110. package/dist/exports/index.js +0 -175
  111. package/dist/exports/index.js.map +0 -1
  112. package/dist/load-ts-contract.d.ts.map +0 -1
  113. package/dist/utils/action.d.ts +0 -16
  114. package/dist/utils/action.d.ts.map +0 -1
  115. package/dist/utils/cli-errors.d.ts +0 -7
  116. package/dist/utils/cli-errors.d.ts.map +0 -1
  117. package/dist/utils/command-helpers.d.ts +0 -12
  118. package/dist/utils/command-helpers.d.ts.map +0 -1
  119. package/dist/utils/framework-components.d.ts +0 -81
  120. package/dist/utils/framework-components.d.ts.map +0 -1
  121. package/dist/utils/global-flags.d.ts +0 -25
  122. package/dist/utils/global-flags.d.ts.map +0 -1
  123. package/dist/utils/output.d.ts +0 -142
  124. package/dist/utils/output.d.ts.map +0 -1
  125. package/dist/utils/result-handler.d.ts +0 -15
  126. package/dist/utils/result-handler.d.ts.map +0 -1
  127. package/dist/utils/spinner.d.ts +0 -29
  128. package/dist/utils/spinner.d.ts.map +0 -1
  129. package/src/utils/action.ts +0 -43
  130. package/src/utils/spinner.ts +0 -67
@@ -0,0 +1,610 @@
1
+ import type { TargetBoundComponentDescriptor } from '@prisma-next/contract/framework-components';
2
+ import type { CoreSchemaView } from '@prisma-next/core-control-plane/schema-view';
3
+ import { createControlPlaneStack } from '@prisma-next/core-control-plane/stack';
4
+ import type {
5
+ ControlDriverInstance,
6
+ ControlFamilyInstance,
7
+ ControlPlaneStack,
8
+ SignDatabaseResult,
9
+ VerifyDatabaseResult,
10
+ VerifyDatabaseSchemaResult,
11
+ } from '@prisma-next/core-control-plane/types';
12
+ import { ifDefined } from '@prisma-next/utils/defined';
13
+ import { notOk, ok } from '@prisma-next/utils/result';
14
+ import { assertFrameworkComponentsCompatible } from '../utils/framework-components';
15
+ import { executeDbInit } from './operations/db-init';
16
+ import type {
17
+ ControlClient,
18
+ ControlClientOptions,
19
+ DbInitOptions,
20
+ DbInitResult,
21
+ EmitOptions,
22
+ EmitResult,
23
+ IntrospectOptions,
24
+ SchemaVerifyOptions,
25
+ SignOptions,
26
+ VerifyOptions,
27
+ } from './types';
28
+
29
+ /**
30
+ * Creates a programmatic control client for Prisma Next operations.
31
+ *
32
+ * The client accepts framework component descriptors at creation time,
33
+ * manages driver lifecycle via connect()/close(), and exposes domain
34
+ * operations that delegate to the existing family instance methods.
35
+ *
36
+ * @see {@link ControlClient} for the client interface
37
+ * @see README.md "Programmatic Control API" section for usage examples
38
+ */
39
+ export function createControlClient(options: ControlClientOptions): ControlClient {
40
+ return new ControlClientImpl(options);
41
+ }
42
+
43
+ /**
44
+ * Implementation of ControlClient.
45
+ * Manages initialization and connection state, delegates operations to family instance.
46
+ */
47
+ class ControlClientImpl implements ControlClient {
48
+ private readonly options: ControlClientOptions;
49
+ private stack: ControlPlaneStack<string, string> | null = null;
50
+ private driver: ControlDriverInstance<string, string> | null = null;
51
+ private familyInstance: ControlFamilyInstance<string> | null = null;
52
+ private frameworkComponents: ReadonlyArray<
53
+ TargetBoundComponentDescriptor<string, string>
54
+ > | null = null;
55
+ private initialized = false;
56
+ private readonly defaultConnection: unknown;
57
+
58
+ constructor(options: ControlClientOptions) {
59
+ this.options = options;
60
+ this.defaultConnection = options.connection;
61
+ }
62
+
63
+ init(): void {
64
+ if (this.initialized) {
65
+ return; // Idempotent
66
+ }
67
+
68
+ // Create the control plane stack
69
+ this.stack = createControlPlaneStack({
70
+ target: this.options.target,
71
+ adapter: this.options.adapter,
72
+ driver: this.options.driver,
73
+ extensionPacks: this.options.extensionPacks,
74
+ });
75
+
76
+ // Create family instance using the stack
77
+ this.familyInstance = this.options.family.create(this.stack);
78
+
79
+ // Validate and type-narrow framework components
80
+ const rawComponents = [
81
+ this.options.target,
82
+ this.options.adapter,
83
+ ...(this.options.extensionPacks ?? []),
84
+ ];
85
+ this.frameworkComponents = assertFrameworkComponentsCompatible(
86
+ this.options.family.familyId,
87
+ this.options.target.targetId,
88
+ rawComponents,
89
+ );
90
+
91
+ this.initialized = true;
92
+ }
93
+
94
+ async connect(connection?: unknown): Promise<void> {
95
+ // Auto-init if needed
96
+ this.init();
97
+
98
+ if (this.driver) {
99
+ throw new Error('Already connected. Call close() before reconnecting.');
100
+ }
101
+
102
+ // Resolve connection: argument > default from options
103
+ const resolvedConnection = connection ?? this.defaultConnection;
104
+ if (resolvedConnection === undefined) {
105
+ throw new Error(
106
+ 'No connection provided. Pass a connection to connect() or provide a default connection when creating the client.',
107
+ );
108
+ }
109
+
110
+ // Check for driver descriptor
111
+ if (!this.stack?.driver) {
112
+ throw new Error(
113
+ 'Driver is not configured. Pass a driver descriptor when creating the control client to enable database operations.',
114
+ );
115
+ }
116
+
117
+ // Create driver instance
118
+ // Cast through any since connection type is driver-specific at runtime.
119
+ // The driver descriptor is typed with any for TConnection in ControlClientOptions,
120
+ // but createControlPlaneStack defaults it to string. We bridge this at runtime.
121
+ // biome-ignore lint/suspicious/noExplicitAny: required for runtime connection type flexibility
122
+ this.driver = await this.stack?.driver.create(resolvedConnection as any);
123
+ }
124
+
125
+ async close(): Promise<void> {
126
+ if (this.driver) {
127
+ await this.driver.close();
128
+ this.driver = null;
129
+ }
130
+ }
131
+
132
+ private async ensureConnected(): Promise<{
133
+ driver: ControlDriverInstance<string, string>;
134
+ familyInstance: ControlFamilyInstance<string>;
135
+ frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<string, string>>;
136
+ }> {
137
+ // Auto-init if needed
138
+ this.init();
139
+
140
+ // Auto-connect if not connected and default connection is available
141
+ if (!this.driver && this.defaultConnection !== undefined) {
142
+ await this.connect(this.defaultConnection);
143
+ }
144
+
145
+ if (!this.driver || !this.familyInstance || !this.frameworkComponents) {
146
+ throw new Error('Not connected. Call connect(connection) first.');
147
+ }
148
+ return {
149
+ driver: this.driver,
150
+ familyInstance: this.familyInstance,
151
+ frameworkComponents: this.frameworkComponents,
152
+ };
153
+ }
154
+
155
+ async verify(options: VerifyOptions): Promise<VerifyDatabaseResult> {
156
+ const { onProgress } = options;
157
+
158
+ // Connect with progress span if connection provided
159
+ if (options.connection !== undefined) {
160
+ onProgress?.({
161
+ action: 'verify',
162
+ kind: 'spanStart',
163
+ spanId: 'connect',
164
+ label: 'Connecting to database...',
165
+ });
166
+ try {
167
+ await this.connect(options.connection);
168
+ onProgress?.({
169
+ action: 'verify',
170
+ kind: 'spanEnd',
171
+ spanId: 'connect',
172
+ outcome: 'ok',
173
+ });
174
+ } catch (error) {
175
+ onProgress?.({
176
+ action: 'verify',
177
+ kind: 'spanEnd',
178
+ spanId: 'connect',
179
+ outcome: 'error',
180
+ });
181
+ throw error;
182
+ }
183
+ }
184
+
185
+ const { driver, familyInstance } = await this.ensureConnected();
186
+
187
+ // Validate contract using family instance
188
+ const contractIR = familyInstance.validateContractIR(options.contractIR);
189
+
190
+ // Emit verify span
191
+ onProgress?.({
192
+ action: 'verify',
193
+ kind: 'spanStart',
194
+ spanId: 'verify',
195
+ label: 'Verifying contract marker...',
196
+ });
197
+
198
+ try {
199
+ // Delegate to family instance verify method
200
+ // Note: We pass empty strings for contractPath/configPath since the programmatic
201
+ // API doesn't deal with file paths. The family instance accepts these as optional
202
+ // metadata for error reporting.
203
+ const result = await familyInstance.verify({
204
+ driver,
205
+ contractIR,
206
+ expectedTargetId: this.options.target.targetId,
207
+ contractPath: '',
208
+ });
209
+
210
+ onProgress?.({
211
+ action: 'verify',
212
+ kind: 'spanEnd',
213
+ spanId: 'verify',
214
+ outcome: result.ok ? 'ok' : 'error',
215
+ });
216
+
217
+ return result;
218
+ } catch (error) {
219
+ onProgress?.({
220
+ action: 'verify',
221
+ kind: 'spanEnd',
222
+ spanId: 'verify',
223
+ outcome: 'error',
224
+ });
225
+ throw error;
226
+ }
227
+ }
228
+
229
+ async schemaVerify(options: SchemaVerifyOptions): Promise<VerifyDatabaseSchemaResult> {
230
+ const { onProgress } = options;
231
+
232
+ // Connect with progress span if connection provided
233
+ if (options.connection !== undefined) {
234
+ onProgress?.({
235
+ action: 'schemaVerify',
236
+ kind: 'spanStart',
237
+ spanId: 'connect',
238
+ label: 'Connecting to database...',
239
+ });
240
+ try {
241
+ await this.connect(options.connection);
242
+ onProgress?.({
243
+ action: 'schemaVerify',
244
+ kind: 'spanEnd',
245
+ spanId: 'connect',
246
+ outcome: 'ok',
247
+ });
248
+ } catch (error) {
249
+ onProgress?.({
250
+ action: 'schemaVerify',
251
+ kind: 'spanEnd',
252
+ spanId: 'connect',
253
+ outcome: 'error',
254
+ });
255
+ throw error;
256
+ }
257
+ }
258
+
259
+ const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
260
+
261
+ // Validate contract using family instance
262
+ const contractIR = familyInstance.validateContractIR(options.contractIR);
263
+
264
+ // Emit schemaVerify span
265
+ onProgress?.({
266
+ action: 'schemaVerify',
267
+ kind: 'spanStart',
268
+ spanId: 'schemaVerify',
269
+ label: 'Verifying database schema...',
270
+ });
271
+
272
+ try {
273
+ // Delegate to family instance schemaVerify method
274
+ const result = await familyInstance.schemaVerify({
275
+ driver,
276
+ contractIR,
277
+ strict: options.strict ?? false,
278
+ contractPath: '',
279
+ frameworkComponents,
280
+ });
281
+
282
+ onProgress?.({
283
+ action: 'schemaVerify',
284
+ kind: 'spanEnd',
285
+ spanId: 'schemaVerify',
286
+ outcome: result.ok ? 'ok' : 'error',
287
+ });
288
+
289
+ return result;
290
+ } catch (error) {
291
+ onProgress?.({
292
+ action: 'schemaVerify',
293
+ kind: 'spanEnd',
294
+ spanId: 'schemaVerify',
295
+ outcome: 'error',
296
+ });
297
+ throw error;
298
+ }
299
+ }
300
+
301
+ async sign(options: SignOptions): Promise<SignDatabaseResult> {
302
+ const { onProgress } = options;
303
+
304
+ // Connect with progress span if connection provided
305
+ if (options.connection !== undefined) {
306
+ onProgress?.({
307
+ action: 'sign',
308
+ kind: 'spanStart',
309
+ spanId: 'connect',
310
+ label: 'Connecting to database...',
311
+ });
312
+ try {
313
+ await this.connect(options.connection);
314
+ onProgress?.({
315
+ action: 'sign',
316
+ kind: 'spanEnd',
317
+ spanId: 'connect',
318
+ outcome: 'ok',
319
+ });
320
+ } catch (error) {
321
+ onProgress?.({
322
+ action: 'sign',
323
+ kind: 'spanEnd',
324
+ spanId: 'connect',
325
+ outcome: 'error',
326
+ });
327
+ throw error;
328
+ }
329
+ }
330
+
331
+ const { driver, familyInstance } = await this.ensureConnected();
332
+
333
+ // Validate contract using family instance
334
+ const contractIR = familyInstance.validateContractIR(options.contractIR);
335
+
336
+ // Emit sign span
337
+ onProgress?.({
338
+ action: 'sign',
339
+ kind: 'spanStart',
340
+ spanId: 'sign',
341
+ label: 'Signing database...',
342
+ });
343
+
344
+ try {
345
+ // Delegate to family instance sign method
346
+ const result = await familyInstance.sign({
347
+ driver,
348
+ contractIR,
349
+ contractPath: options.contractPath ?? '',
350
+ ...ifDefined('configPath', options.configPath),
351
+ });
352
+
353
+ onProgress?.({
354
+ action: 'sign',
355
+ kind: 'spanEnd',
356
+ spanId: 'sign',
357
+ outcome: 'ok',
358
+ });
359
+
360
+ return result;
361
+ } catch (error) {
362
+ onProgress?.({
363
+ action: 'sign',
364
+ kind: 'spanEnd',
365
+ spanId: 'sign',
366
+ outcome: 'error',
367
+ });
368
+ throw error;
369
+ }
370
+ }
371
+
372
+ async dbInit(options: DbInitOptions): Promise<DbInitResult> {
373
+ const { onProgress } = options;
374
+
375
+ // Connect with progress span if connection provided
376
+ if (options.connection !== undefined) {
377
+ onProgress?.({
378
+ action: 'dbInit',
379
+ kind: 'spanStart',
380
+ spanId: 'connect',
381
+ label: 'Connecting to database...',
382
+ });
383
+ try {
384
+ await this.connect(options.connection);
385
+ onProgress?.({
386
+ action: 'dbInit',
387
+ kind: 'spanEnd',
388
+ spanId: 'connect',
389
+ outcome: 'ok',
390
+ });
391
+ } catch (error) {
392
+ onProgress?.({
393
+ action: 'dbInit',
394
+ kind: 'spanEnd',
395
+ spanId: 'connect',
396
+ outcome: 'error',
397
+ });
398
+ throw error;
399
+ }
400
+ }
401
+
402
+ const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
403
+
404
+ // Check target supports migrations
405
+ if (!this.options.target.migrations) {
406
+ throw new Error(`Target "${this.options.target.targetId}" does not support migrations`);
407
+ }
408
+
409
+ // Validate contract using family instance
410
+ const contractIR = familyInstance.validateContractIR(options.contractIR);
411
+
412
+ // Delegate to extracted dbInit operation
413
+ return executeDbInit({
414
+ driver,
415
+ familyInstance,
416
+ contractIR,
417
+ mode: options.mode,
418
+ migrations: this.options.target.migrations,
419
+ frameworkComponents,
420
+ ...(onProgress ? { onProgress } : {}),
421
+ });
422
+ }
423
+
424
+ async introspect(options?: IntrospectOptions): Promise<unknown> {
425
+ const onProgress = options?.onProgress;
426
+
427
+ // Connect with progress span if connection provided
428
+ if (options?.connection !== undefined) {
429
+ onProgress?.({
430
+ action: 'introspect',
431
+ kind: 'spanStart',
432
+ spanId: 'connect',
433
+ label: 'Connecting to database...',
434
+ });
435
+ try {
436
+ await this.connect(options.connection);
437
+ onProgress?.({
438
+ action: 'introspect',
439
+ kind: 'spanEnd',
440
+ spanId: 'connect',
441
+ outcome: 'ok',
442
+ });
443
+ } catch (error) {
444
+ onProgress?.({
445
+ action: 'introspect',
446
+ kind: 'spanEnd',
447
+ spanId: 'connect',
448
+ outcome: 'error',
449
+ });
450
+ throw error;
451
+ }
452
+ }
453
+
454
+ const { driver, familyInstance } = await this.ensureConnected();
455
+
456
+ // TODO: Pass schema option to familyInstance.introspect when schema filtering is implemented
457
+ const _schema = options?.schema;
458
+ void _schema;
459
+
460
+ // Emit introspect span
461
+ onProgress?.({
462
+ action: 'introspect',
463
+ kind: 'spanStart',
464
+ spanId: 'introspect',
465
+ label: 'Introspecting database schema...',
466
+ });
467
+
468
+ try {
469
+ const result = await familyInstance.introspect({ driver });
470
+
471
+ onProgress?.({
472
+ action: 'introspect',
473
+ kind: 'spanEnd',
474
+ spanId: 'introspect',
475
+ outcome: 'ok',
476
+ });
477
+
478
+ return result;
479
+ } catch (error) {
480
+ onProgress?.({
481
+ action: 'introspect',
482
+ kind: 'spanEnd',
483
+ spanId: 'introspect',
484
+ outcome: 'error',
485
+ });
486
+ throw error;
487
+ }
488
+ }
489
+
490
+ toSchemaView(schemaIR: unknown): CoreSchemaView | undefined {
491
+ this.init();
492
+ if (this.familyInstance?.toSchemaView) {
493
+ return this.familyInstance.toSchemaView(schemaIR);
494
+ }
495
+ return undefined;
496
+ }
497
+
498
+ async emit(options: EmitOptions): Promise<EmitResult> {
499
+ const { onProgress, contractConfig } = options;
500
+
501
+ // Ensure initialized (creates stack and family instance)
502
+ // emit() does NOT require a database connection
503
+ this.init();
504
+
505
+ if (!this.familyInstance) {
506
+ throw new Error('Family instance was not initialized. This is a bug.');
507
+ }
508
+
509
+ let contractRaw: unknown;
510
+ onProgress?.({
511
+ action: 'emit',
512
+ kind: 'spanStart',
513
+ spanId: 'resolveSource',
514
+ label: 'Resolving contract source...',
515
+ });
516
+
517
+ try {
518
+ const providerResult = await contractConfig.sourceProvider();
519
+ if (!providerResult.ok) {
520
+ onProgress?.({
521
+ action: 'emit',
522
+ kind: 'spanEnd',
523
+ spanId: 'resolveSource',
524
+ outcome: 'error',
525
+ });
526
+
527
+ return notOk({
528
+ code: 'CONTRACT_SOURCE_INVALID',
529
+ summary: providerResult.failure.summary,
530
+ why: providerResult.failure.summary,
531
+ meta: providerResult.failure.meta,
532
+ diagnostics: providerResult.failure,
533
+ });
534
+ }
535
+ contractRaw = providerResult.value;
536
+
537
+ onProgress?.({
538
+ action: 'emit',
539
+ kind: 'spanEnd',
540
+ spanId: 'resolveSource',
541
+ outcome: 'ok',
542
+ });
543
+ } catch (error) {
544
+ onProgress?.({
545
+ action: 'emit',
546
+ kind: 'spanEnd',
547
+ spanId: 'resolveSource',
548
+ outcome: 'error',
549
+ });
550
+
551
+ const message = error instanceof Error ? error.message : String(error);
552
+ return notOk({
553
+ code: 'CONTRACT_SOURCE_INVALID',
554
+ summary: 'Failed to resolve contract source',
555
+ why: message,
556
+ diagnostics: {
557
+ summary: 'Contract source provider threw an exception',
558
+ diagnostics: [
559
+ {
560
+ code: 'PROVIDER_THROW',
561
+ message,
562
+ },
563
+ ],
564
+ },
565
+ meta: undefined,
566
+ });
567
+ }
568
+
569
+ // Emit contract
570
+ onProgress?.({
571
+ action: 'emit',
572
+ kind: 'spanStart',
573
+ spanId: 'emit',
574
+ label: 'Emitting contract...',
575
+ });
576
+
577
+ try {
578
+ const emitResult = await this.familyInstance.emitContract({ contractIR: contractRaw });
579
+
580
+ onProgress?.({
581
+ action: 'emit',
582
+ kind: 'spanEnd',
583
+ spanId: 'emit',
584
+ outcome: 'ok',
585
+ });
586
+
587
+ return ok({
588
+ storageHash: emitResult.storageHash,
589
+ ...ifDefined('executionHash', emitResult.executionHash),
590
+ profileHash: emitResult.profileHash,
591
+ contractJson: emitResult.contractJson,
592
+ contractDts: emitResult.contractDts,
593
+ });
594
+ } catch (error) {
595
+ onProgress?.({
596
+ action: 'emit',
597
+ kind: 'spanEnd',
598
+ spanId: 'emit',
599
+ outcome: 'error',
600
+ });
601
+
602
+ return notOk({
603
+ code: 'EMIT_FAILED',
604
+ summary: 'Failed to emit contract',
605
+ why: error instanceof Error ? error.message : String(error),
606
+ meta: undefined,
607
+ });
608
+ }
609
+ }
610
+ }