@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
@@ -1,20 +1,24 @@
1
1
  import { mkdirSync, writeFileSync } from 'node:fs';
2
- import { dirname, relative, resolve } from 'node:path';
3
2
  import { errorContractConfigMissing } from '@prisma-next/core-control-plane/errors';
3
+ import { notOk, ok, type Result } from '@prisma-next/utils/result';
4
4
  import { Command } from 'commander';
5
+ import { dirname, isAbsolute, join, relative, resolve } from 'pathe';
5
6
  import { loadConfig } from '../config-loader';
6
- import { performAction } from '../utils/action';
7
+ import { createControlClient } from '../control-api/client';
8
+ import type { EmitFailure } from '../control-api/types';
9
+ import { CliStructuredError, errorRuntime, errorUnexpected } from '../utils/cli-errors';
7
10
  import { setCommandDescriptions } from '../utils/command-helpers';
8
- import { parseGlobalFlags } from '../utils/global-flags';
11
+ import { type GlobalFlags, parseGlobalFlags } from '../utils/global-flags';
9
12
  import {
13
+ type EmitContractResult,
10
14
  formatCommandHelp,
11
15
  formatEmitJson,
12
16
  formatEmitOutput,
13
17
  formatStyledHeader,
14
18
  formatSuccessMessage,
15
19
  } from '../utils/output';
20
+ import { createProgressAdapter } from '../utils/progress-adapter';
16
21
  import { handleResult } from '../utils/result-handler';
17
- import { withSpinner } from '../utils/spinner';
18
22
 
19
23
  interface ContractEmitOptions {
20
24
  readonly config?: string;
@@ -30,11 +34,205 @@ interface ContractEmitOptions {
30
34
  readonly 'no-color'?: boolean;
31
35
  }
32
36
 
37
+ function mapDiagnosticsToIssues(
38
+ failure: EmitFailure,
39
+ ): ReadonlyArray<{ kind: string; message: string }> {
40
+ const diagnostics = failure.diagnostics?.diagnostics ?? [];
41
+ return diagnostics.map((diagnostic) => {
42
+ const location =
43
+ diagnostic.sourceId && diagnostic.span
44
+ ? ` (${diagnostic.sourceId}:${diagnostic.span.start.line}:${diagnostic.span.start.column})`
45
+ : diagnostic.sourceId
46
+ ? ` (${diagnostic.sourceId})`
47
+ : '';
48
+ return {
49
+ kind: diagnostic.code,
50
+ message: `${diagnostic.message}${location}`,
51
+ };
52
+ });
53
+ }
54
+
55
+ /**
56
+ * Maps an EmitFailure to a CliStructuredError for consistent error handling.
57
+ */
58
+ function mapEmitFailure(failure: EmitFailure): CliStructuredError {
59
+ if (failure.code === 'CONTRACT_SOURCE_INVALID') {
60
+ const issues = mapDiagnosticsToIssues(failure);
61
+ return errorRuntime(failure.summary, {
62
+ why: failure.why ?? 'Contract source provider failed',
63
+ fix: 'Check your contract source provider in prisma-next.config.ts and ensure it returns Result<ContractIR, Diagnostics>',
64
+ ...(issues.length > 0 ? { meta: { issues } } : {}),
65
+ });
66
+ }
67
+
68
+ if (failure.code === 'EMIT_FAILED') {
69
+ return errorRuntime(failure.summary, {
70
+ why: failure.why ?? 'Failed to emit contract',
71
+ fix: 'Check your contract configuration and ensure the source is valid',
72
+ });
73
+ }
74
+
75
+ // Exhaustive check - TypeScript will error if a new code is added but not handled
76
+ const exhaustive: never = failure.code;
77
+ throw new Error(`Unhandled EmitFailure code: ${exhaustive}`);
78
+ }
79
+
80
+ /**
81
+ * Executes the contract emit command and returns a structured Result.
82
+ */
83
+ async function executeContractEmitCommand(
84
+ options: ContractEmitOptions,
85
+ flags: GlobalFlags,
86
+ startTime: number,
87
+ ): Promise<Result<EmitContractResult, CliStructuredError>> {
88
+ // Load config
89
+ let config: Awaited<ReturnType<typeof loadConfig>>;
90
+ try {
91
+ config = await loadConfig(options.config);
92
+ } catch (error) {
93
+ // Convert thrown CliStructuredError to Result
94
+ if (error instanceof CliStructuredError) {
95
+ return notOk(error);
96
+ }
97
+ return notOk(
98
+ errorUnexpected(error instanceof Error ? error.message : String(error), {
99
+ why: 'Failed to load config',
100
+ }),
101
+ );
102
+ }
103
+
104
+ // Resolve contract from config
105
+ if (!config.contract) {
106
+ return notOk(
107
+ errorContractConfigMissing({
108
+ why: 'Config.contract is required for emit. Define it in your config: contract: { source: ..., output: ... }',
109
+ }),
110
+ );
111
+ }
112
+
113
+ // Contract config is already normalized by defineConfig() with defaults applied
114
+ const contractConfig = config.contract;
115
+
116
+ // Resolve artifact paths from config (already normalized by defineConfig() with defaults)
117
+ if (!contractConfig.output) {
118
+ return notOk(
119
+ errorContractConfigMissing({
120
+ why: 'Contract config must have output path. This should not happen if defineConfig() was used.',
121
+ }),
122
+ );
123
+ }
124
+ if (!contractConfig.output.endsWith('.json')) {
125
+ return notOk(
126
+ errorContractConfigMissing({
127
+ why: 'Contract config output path must end with .json (e.g., "src/prisma/contract.json")',
128
+ }),
129
+ );
130
+ }
131
+ const configDir = options.config ? dirname(resolve(options.config)) : process.cwd();
132
+ const outputJsonPath = isAbsolute(contractConfig.output)
133
+ ? contractConfig.output
134
+ : join(configDir, contractConfig.output);
135
+ // Colocate .d.ts with .json (contract.json → contract.d.ts)
136
+ const outputDtsPath = `${outputJsonPath.slice(0, -5)}.d.ts`;
137
+
138
+ // Output header (only for human-readable output)
139
+ if (flags.json !== 'object' && !flags.quiet) {
140
+ // Normalize config path for display (match contract path format - no ./ prefix)
141
+ const configPath = options.config
142
+ ? relative(process.cwd(), resolve(options.config))
143
+ : 'prisma-next.config.ts';
144
+ // Convert absolute paths to relative paths for display
145
+ const contractPath = relative(process.cwd(), outputJsonPath);
146
+ const typesPath = relative(process.cwd(), outputDtsPath);
147
+ const header = formatStyledHeader({
148
+ command: 'contract emit',
149
+ description: 'Emit your contract artifacts',
150
+ url: 'https://pris.ly/contract-emit',
151
+ details: [
152
+ { label: 'config', value: configPath },
153
+ { label: 'contract', value: contractPath },
154
+ { label: 'types', value: typesPath },
155
+ ],
156
+ flags,
157
+ });
158
+ console.log(header);
159
+ }
160
+
161
+ // Create control client (no driver needed for emit)
162
+ const client = createControlClient({
163
+ family: config.family,
164
+ target: config.target,
165
+ adapter: config.adapter,
166
+ extensionPacks: config.extensionPacks ?? [],
167
+ });
168
+
169
+ // Create progress adapter
170
+ const onProgress = createProgressAdapter({ flags });
171
+
172
+ try {
173
+ // Call emit with progress callback
174
+ const result = await client.emit({
175
+ contractConfig: {
176
+ sourceProvider: contractConfig.source,
177
+ output: outputJsonPath,
178
+ },
179
+ onProgress,
180
+ });
181
+
182
+ // Handle failures by mapping to CLI structured error
183
+ if (!result.ok) {
184
+ return notOk(mapEmitFailure(result.failure));
185
+ }
186
+
187
+ // Create directories if needed
188
+ mkdirSync(dirname(outputJsonPath), { recursive: true });
189
+ mkdirSync(dirname(outputDtsPath), { recursive: true });
190
+
191
+ // Write the results to files
192
+ writeFileSync(outputJsonPath, result.value.contractJson, 'utf-8');
193
+ writeFileSync(outputDtsPath, result.value.contractDts, 'utf-8');
194
+
195
+ // Add blank line after all async operations if spinners were shown
196
+ if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {
197
+ console.log('');
198
+ }
199
+
200
+ // Convert success result to CLI output format
201
+ const emitResult: EmitContractResult = {
202
+ storageHash: result.value.storageHash,
203
+ ...(result.value.executionHash ? { executionHash: result.value.executionHash } : {}),
204
+ profileHash: result.value.profileHash,
205
+ outDir: dirname(outputJsonPath),
206
+ files: {
207
+ json: outputJsonPath,
208
+ dts: outputDtsPath,
209
+ },
210
+ timings: { total: Date.now() - startTime },
211
+ };
212
+
213
+ return ok(emitResult);
214
+ } catch (error) {
215
+ // Use static type guard to work across module boundaries
216
+ if (CliStructuredError.is(error)) {
217
+ return notOk(error);
218
+ }
219
+
220
+ // Wrap unexpected errors
221
+ return notOk(
222
+ errorUnexpected('Unexpected error during contract emit', {
223
+ why: error instanceof Error ? error.message : String(error),
224
+ }),
225
+ );
226
+ } finally {
227
+ await client.close();
228
+ }
229
+ }
230
+
33
231
  export function createContractEmitCommand(): Command {
34
232
  const command = new Command('emit');
35
233
  setCommandDescriptions(
36
234
  command,
37
- 'Write your contract to JSON and sign it',
235
+ 'Emit your contract artifacts',
38
236
  'Reads your contract source (TypeScript or Prisma schema) and emits contract.json and\n' +
39
237
  'contract.d.ts. The contract.json contains the canonical contract structure, and\n' +
40
238
  'contract.d.ts provides TypeScript types for type-safe query building.',
@@ -56,113 +254,9 @@ export function createContractEmitCommand(): Command {
56
254
  .option('--no-color', 'Disable color output')
57
255
  .action(async (options: ContractEmitOptions) => {
58
256
  const flags = parseGlobalFlags(options);
257
+ const startTime = Date.now();
59
258
 
60
- const result = await performAction(async () => {
61
- // Load config
62
- const config = await loadConfig(options.config);
63
-
64
- // Resolve contract from config
65
- if (!config.contract) {
66
- throw errorContractConfigMissing({
67
- why: 'Config.contract is required for emit. Define it in your config: contract: { source: ..., output: ..., types: ... }',
68
- });
69
- }
70
-
71
- // Contract config is already normalized by defineConfig() with defaults applied
72
- const contractConfig = config.contract;
73
-
74
- // Resolve artifact paths from config (already normalized by defineConfig() with defaults)
75
- if (!contractConfig.output || !contractConfig.types) {
76
- throw errorContractConfigMissing({
77
- why: 'Contract config must have output and types paths. This should not happen if defineConfig() was used.',
78
- });
79
- }
80
- const outputJsonPath = resolve(contractConfig.output);
81
- const outputDtsPath = resolve(contractConfig.types);
82
-
83
- // Output header (only for human-readable output)
84
- if (flags.json !== 'object' && !flags.quiet) {
85
- // Normalize config path for display (match contract path format - no ./ prefix)
86
- const configPath = options.config
87
- ? relative(process.cwd(), resolve(options.config))
88
- : 'prisma-next.config.ts';
89
- // Convert absolute paths to relative paths for display
90
- const contractPath = relative(process.cwd(), outputJsonPath);
91
- const typesPath = relative(process.cwd(), outputDtsPath);
92
- const header = formatStyledHeader({
93
- command: 'contract emit',
94
- description: 'Write your contract to JSON and sign it',
95
- url: 'https://pris.ly/contract-emit',
96
- details: [
97
- { label: 'config', value: configPath },
98
- { label: 'contract', value: contractPath },
99
- { label: 'types', value: typesPath },
100
- ],
101
- flags,
102
- });
103
- console.log(header);
104
- }
105
-
106
- // Create family instance (assembles operation registry, type imports, extension IDs)
107
- // Note: emit command doesn't need driver, but ControlFamilyDescriptor.create() requires it
108
- // We'll need to provide a minimal driver descriptor or make driver optional for emit
109
- // For now, we'll require driver to be present in config even for emit
110
- if (!config.driver) {
111
- throw errorContractConfigMissing({
112
- why: 'Config.driver is required. Even though emit does not use the driver, it is required by ControlFamilyDescriptor.create()',
113
- });
114
- }
115
- const familyInstance = config.family.create({
116
- target: config.target,
117
- adapter: config.adapter,
118
- driver: config.driver,
119
- extensionPacks: config.extensionPacks ?? [],
120
- });
121
-
122
- // Resolve contract source from config (user's config handles loading)
123
- let contractRaw: unknown;
124
- if (typeof contractConfig.source === 'function') {
125
- contractRaw = await contractConfig.source();
126
- } else {
127
- contractRaw = contractConfig.source;
128
- }
129
-
130
- // Call emitContract on family instance (handles stripping mappings and validation internally)
131
- const emitResult = await withSpinner(
132
- () => familyInstance.emitContract({ contractIR: contractRaw }),
133
- {
134
- message: 'Emitting contract...',
135
- flags,
136
- },
137
- );
138
-
139
- // Create directories if needed
140
- mkdirSync(dirname(outputJsonPath), { recursive: true });
141
- mkdirSync(dirname(outputDtsPath), { recursive: true });
142
-
143
- // Write the results to files
144
- writeFileSync(outputJsonPath, emitResult.contractJson, 'utf-8');
145
- writeFileSync(outputDtsPath, emitResult.contractDts, 'utf-8');
146
-
147
- // Add blank line after all async operations if spinners were shown
148
- if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {
149
- console.log('');
150
- }
151
-
152
- // Return result with file paths for output formatting
153
- return {
154
- coreHash: emitResult.coreHash,
155
- profileHash: emitResult.profileHash,
156
- outDir: dirname(outputJsonPath),
157
- files: {
158
- json: outputJsonPath,
159
- dts: outputDtsPath,
160
- },
161
- timings: {
162
- total: 0, // Timing is handled by emitContract internally if needed
163
- },
164
- };
165
- });
259
+ const result = await executeContractEmitCommand(options, flags, startTime);
166
260
 
167
261
  // Handle result - formats output and returns exit code
168
262
  const exitCode = handleResult(result, flags, (emitResult) => {