@prisma-next/cli 0.3.0-dev.8 → 0.3.0-pr.100.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.
Files changed (66) hide show
  1. package/README.md +111 -27
  2. package/dist/{chunk-BZMBKEEQ.js → chunk-AGOTG4L3.js} +44 -76
  3. package/dist/chunk-AGOTG4L3.js.map +1 -0
  4. package/dist/chunk-HLLI4YL7.js +180 -0
  5. package/dist/chunk-HLLI4YL7.js.map +1 -0
  6. package/dist/chunk-VG2R7DGF.js +735 -0
  7. package/dist/chunk-VG2R7DGF.js.map +1 -0
  8. package/dist/cli.js +1502 -942
  9. package/dist/cli.js.map +1 -1
  10. package/dist/commands/contract-emit.d.ts.map +1 -1
  11. package/dist/commands/contract-emit.js +3 -2
  12. package/dist/commands/db-init.d.ts.map +1 -1
  13. package/dist/commands/db-init.js +205 -285
  14. package/dist/commands/db-init.js.map +1 -1
  15. package/dist/commands/db-introspect.d.ts.map +1 -1
  16. package/dist/commands/db-introspect.js +108 -139
  17. package/dist/commands/db-introspect.js.map +1 -1
  18. package/dist/commands/db-schema-verify.d.ts.map +1 -1
  19. package/dist/commands/db-schema-verify.js +120 -109
  20. package/dist/commands/db-schema-verify.js.map +1 -1
  21. package/dist/commands/db-sign.d.ts.map +1 -1
  22. package/dist/commands/db-sign.js +152 -152
  23. package/dist/commands/db-sign.js.map +1 -1
  24. package/dist/commands/db-verify.d.ts.map +1 -1
  25. package/dist/commands/db-verify.js +142 -118
  26. package/dist/commands/db-verify.js.map +1 -1
  27. package/dist/control-api/client.d.ts +13 -0
  28. package/dist/control-api/client.d.ts.map +1 -0
  29. package/dist/control-api/operations/db-init.d.ts +29 -0
  30. package/dist/control-api/operations/db-init.d.ts.map +1 -0
  31. package/dist/control-api/types.d.ts +387 -0
  32. package/dist/control-api/types.d.ts.map +1 -0
  33. package/dist/exports/control-api.d.ts +13 -0
  34. package/dist/exports/control-api.d.ts.map +1 -0
  35. package/dist/exports/control-api.js +7 -0
  36. package/dist/exports/control-api.js.map +1 -0
  37. package/dist/exports/index.js +3 -2
  38. package/dist/exports/index.js.map +1 -1
  39. package/dist/utils/cli-errors.d.ts +1 -1
  40. package/dist/utils/cli-errors.d.ts.map +1 -1
  41. package/dist/utils/progress-adapter.d.ts +26 -0
  42. package/dist/utils/progress-adapter.d.ts.map +1 -0
  43. package/package.json +20 -16
  44. package/src/commands/contract-emit.ts +179 -102
  45. package/src/commands/db-init.ts +263 -355
  46. package/src/commands/db-introspect.ts +151 -180
  47. package/src/commands/db-schema-verify.ts +151 -145
  48. package/src/commands/db-sign.ts +202 -196
  49. package/src/commands/db-verify.ts +180 -151
  50. package/src/control-api/client.ts +589 -0
  51. package/src/control-api/operations/db-init.ts +281 -0
  52. package/src/control-api/types.ts +461 -0
  53. package/src/exports/control-api.ts +46 -0
  54. package/src/utils/cli-errors.ts +1 -1
  55. package/src/utils/progress-adapter.ts +86 -0
  56. package/dist/chunk-BZMBKEEQ.js.map +0 -1
  57. package/dist/chunk-CVNWLFXO.js +0 -91
  58. package/dist/chunk-CVNWLFXO.js.map +0 -1
  59. package/dist/chunk-QUPBU4KV.js +0 -131
  60. package/dist/chunk-QUPBU4KV.js.map +0 -1
  61. package/dist/utils/action.d.ts +0 -16
  62. package/dist/utils/action.d.ts.map +0 -1
  63. package/dist/utils/spinner.d.ts +0 -29
  64. package/dist/utils/spinner.d.ts.map +0 -1
  65. package/src/utils/action.ts +0 -43
  66. package/src/utils/spinner.ts +0 -67
@@ -1,21 +1,24 @@
1
1
  import { mkdirSync, writeFileSync } from 'node:fs';
2
2
  import { dirname, relative, resolve } from 'node:path';
3
3
  import { errorContractConfigMissing } from '@prisma-next/core-control-plane/errors';
4
- import { createControlPlaneStack } from '@prisma-next/core-control-plane/types';
4
+ import { notOk, ok, type Result } from '@prisma-next/utils/result';
5
5
  import { Command } from 'commander';
6
6
  import { loadConfig } from '../config-loader';
7
- import { performAction } from '../utils/action';
7
+ import { createControlClient } from '../control-api/client';
8
+ import type { EmitContractSource, EmitFailure } from '../control-api/types';
9
+ import { CliStructuredError, errorRuntime, errorUnexpected } from '../utils/cli-errors';
8
10
  import { setCommandDescriptions } from '../utils/command-helpers';
9
- import { parseGlobalFlags } from '../utils/global-flags';
11
+ import { type GlobalFlags, parseGlobalFlags } from '../utils/global-flags';
10
12
  import {
13
+ type EmitContractResult,
11
14
  formatCommandHelp,
12
15
  formatEmitJson,
13
16
  formatEmitOutput,
14
17
  formatStyledHeader,
15
18
  formatSuccessMessage,
16
19
  } from '../utils/output';
20
+ import { createProgressAdapter } from '../utils/progress-adapter';
17
21
  import { handleResult } from '../utils/result-handler';
18
- import { withSpinner } from '../utils/spinner';
19
22
 
20
23
  interface ContractEmitOptions {
21
24
  readonly config?: string;
@@ -31,6 +34,176 @@ interface ContractEmitOptions {
31
34
  readonly 'no-color'?: boolean;
32
35
  }
33
36
 
37
+ /**
38
+ * Maps an EmitFailure to a CliStructuredError for consistent error handling.
39
+ */
40
+ function mapEmitFailure(failure: EmitFailure): CliStructuredError {
41
+ if (failure.code === 'CONTRACT_SOURCE_INVALID') {
42
+ return errorRuntime(failure.summary, {
43
+ why: failure.why ?? 'Contract source is invalid',
44
+ fix: 'Check your contract source configuration in prisma-next.config.ts',
45
+ });
46
+ }
47
+
48
+ if (failure.code === 'EMIT_FAILED') {
49
+ return errorRuntime(failure.summary, {
50
+ why: failure.why ?? 'Failed to emit contract',
51
+ fix: 'Check your contract configuration and ensure the source is valid',
52
+ });
53
+ }
54
+
55
+ // Exhaustive check - TypeScript will error if a new code is added but not handled
56
+ const exhaustive: never = failure.code;
57
+ throw new Error(`Unhandled EmitFailure code: ${exhaustive}`);
58
+ }
59
+
60
+ /**
61
+ * Executes the contract emit command and returns a structured Result.
62
+ */
63
+ async function executeContractEmitCommand(
64
+ options: ContractEmitOptions,
65
+ flags: GlobalFlags,
66
+ startTime: number,
67
+ ): Promise<Result<EmitContractResult, CliStructuredError>> {
68
+ // Load config
69
+ let config: Awaited<ReturnType<typeof loadConfig>>;
70
+ try {
71
+ config = await loadConfig(options.config);
72
+ } catch (error) {
73
+ // Convert thrown CliStructuredError to Result
74
+ if (error instanceof CliStructuredError) {
75
+ return notOk(error);
76
+ }
77
+ return notOk(
78
+ errorUnexpected(error instanceof Error ? error.message : String(error), {
79
+ why: 'Failed to load config',
80
+ }),
81
+ );
82
+ }
83
+
84
+ // Resolve contract from config
85
+ if (!config.contract) {
86
+ return notOk(
87
+ errorContractConfigMissing({
88
+ why: 'Config.contract is required for emit. Define it in your config: contract: { source: ..., output: ..., types: ... }',
89
+ }),
90
+ );
91
+ }
92
+
93
+ // Contract config is already normalized by defineConfig() with defaults applied
94
+ const contractConfig = config.contract;
95
+
96
+ // Resolve artifact paths from config (already normalized by defineConfig() with defaults)
97
+ if (!contractConfig.output || !contractConfig.types) {
98
+ return notOk(
99
+ errorContractConfigMissing({
100
+ why: 'Contract config must have output and types paths. This should not happen if defineConfig() was used.',
101
+ }),
102
+ );
103
+ }
104
+ const outputJsonPath = resolve(contractConfig.output);
105
+ const outputDtsPath = resolve(contractConfig.types);
106
+
107
+ // Output header (only for human-readable output)
108
+ if (flags.json !== 'object' && !flags.quiet) {
109
+ // Normalize config path for display (match contract path format - no ./ prefix)
110
+ const configPath = options.config
111
+ ? relative(process.cwd(), resolve(options.config))
112
+ : 'prisma-next.config.ts';
113
+ // Convert absolute paths to relative paths for display
114
+ const contractPath = relative(process.cwd(), outputJsonPath);
115
+ const typesPath = relative(process.cwd(), outputDtsPath);
116
+ const header = formatStyledHeader({
117
+ command: 'contract emit',
118
+ description: 'Write your contract to JSON and sign it',
119
+ url: 'https://pris.ly/contract-emit',
120
+ details: [
121
+ { label: 'config', value: configPath },
122
+ { label: 'contract', value: contractPath },
123
+ { label: 'types', value: typesPath },
124
+ ],
125
+ flags,
126
+ });
127
+ console.log(header);
128
+ }
129
+
130
+ // Create control client (no driver needed for emit)
131
+ const client = createControlClient({
132
+ family: config.family,
133
+ target: config.target,
134
+ adapter: config.adapter,
135
+ extensionPacks: config.extensionPacks ?? [],
136
+ });
137
+
138
+ // Create progress adapter
139
+ const onProgress = createProgressAdapter({ flags });
140
+
141
+ try {
142
+ // Convert user config source to discriminated union
143
+ // Type assertion is safe: we check typeof to determine if it's a function
144
+ const source: EmitContractSource =
145
+ typeof contractConfig.source === 'function'
146
+ ? { kind: 'loader', load: contractConfig.source as () => unknown | Promise<unknown> }
147
+ : { kind: 'value', value: contractConfig.source };
148
+
149
+ // Call emit with progress callback
150
+ const result = await client.emit({
151
+ contractConfig: {
152
+ source,
153
+ output: outputJsonPath,
154
+ types: outputDtsPath,
155
+ },
156
+ onProgress,
157
+ });
158
+
159
+ // Handle failures by mapping to CLI structured error
160
+ if (!result.ok) {
161
+ return notOk(mapEmitFailure(result.failure));
162
+ }
163
+
164
+ // Create directories if needed
165
+ mkdirSync(dirname(outputJsonPath), { recursive: true });
166
+ mkdirSync(dirname(outputDtsPath), { recursive: true });
167
+
168
+ // Write the results to files
169
+ writeFileSync(outputJsonPath, result.value.contractJson, 'utf-8');
170
+ writeFileSync(outputDtsPath, result.value.contractDts, 'utf-8');
171
+
172
+ // Add blank line after all async operations if spinners were shown
173
+ if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {
174
+ console.log('');
175
+ }
176
+
177
+ // Convert success result to CLI output format
178
+ const emitResult: EmitContractResult = {
179
+ coreHash: result.value.coreHash,
180
+ profileHash: result.value.profileHash,
181
+ outDir: dirname(outputJsonPath),
182
+ files: {
183
+ json: outputJsonPath,
184
+ dts: outputDtsPath,
185
+ },
186
+ timings: { total: Date.now() - startTime },
187
+ };
188
+
189
+ return ok(emitResult);
190
+ } catch (error) {
191
+ // Use static type guard to work across module boundaries
192
+ if (CliStructuredError.is(error)) {
193
+ return notOk(error);
194
+ }
195
+
196
+ // Wrap unexpected errors
197
+ return notOk(
198
+ errorUnexpected('Unexpected error during contract emit', {
199
+ why: error instanceof Error ? error.message : String(error),
200
+ }),
201
+ );
202
+ } finally {
203
+ await client.close();
204
+ }
205
+ }
206
+
34
207
  export function createContractEmitCommand(): Command {
35
208
  const command = new Command('emit');
36
209
  setCommandDescriptions(
@@ -57,105 +230,9 @@ export function createContractEmitCommand(): Command {
57
230
  .option('--no-color', 'Disable color output')
58
231
  .action(async (options: ContractEmitOptions) => {
59
232
  const flags = parseGlobalFlags(options);
233
+ const startTime = Date.now();
60
234
 
61
- const result = await performAction(async () => {
62
- // Load config
63
- const config = await loadConfig(options.config);
64
-
65
- // Resolve contract from config
66
- if (!config.contract) {
67
- throw errorContractConfigMissing({
68
- why: 'Config.contract is required for emit. Define it in your config: contract: { source: ..., output: ..., types: ... }',
69
- });
70
- }
71
-
72
- // Contract config is already normalized by defineConfig() with defaults applied
73
- const contractConfig = config.contract;
74
-
75
- // Resolve artifact paths from config (already normalized by defineConfig() with defaults)
76
- if (!contractConfig.output || !contractConfig.types) {
77
- throw errorContractConfigMissing({
78
- why: 'Contract config must have output and types paths. This should not happen if defineConfig() was used.',
79
- });
80
- }
81
- const outputJsonPath = resolve(contractConfig.output);
82
- const outputDtsPath = resolve(contractConfig.types);
83
-
84
- // Output header (only for human-readable output)
85
- if (flags.json !== 'object' && !flags.quiet) {
86
- // Normalize config path for display (match contract path format - no ./ prefix)
87
- const configPath = options.config
88
- ? relative(process.cwd(), resolve(options.config))
89
- : 'prisma-next.config.ts';
90
- // Convert absolute paths to relative paths for display
91
- const contractPath = relative(process.cwd(), outputJsonPath);
92
- const typesPath = relative(process.cwd(), outputDtsPath);
93
- const header = formatStyledHeader({
94
- command: 'contract emit',
95
- description: 'Write your contract to JSON and sign it',
96
- url: 'https://pris.ly/contract-emit',
97
- details: [
98
- { label: 'config', value: configPath },
99
- { label: 'contract', value: contractPath },
100
- { label: 'types', value: typesPath },
101
- ],
102
- flags,
103
- });
104
- console.log(header);
105
- }
106
-
107
- const stack = createControlPlaneStack({
108
- target: config.target,
109
- adapter: config.adapter,
110
- driver: config.driver,
111
- extensionPacks: config.extensionPacks,
112
- });
113
- const familyInstance = config.family.create(stack);
114
-
115
- // Resolve contract source from config (user's config handles loading)
116
- let contractRaw: unknown;
117
- if (typeof contractConfig.source === 'function') {
118
- contractRaw = await contractConfig.source();
119
- } else {
120
- contractRaw = contractConfig.source;
121
- }
122
-
123
- // Call emitContract on family instance (handles stripping mappings and validation internally)
124
- const emitResult = await withSpinner(
125
- () => familyInstance.emitContract({ contractIR: contractRaw }),
126
- {
127
- message: 'Emitting contract...',
128
- flags,
129
- },
130
- );
131
-
132
- // Create directories if needed
133
- mkdirSync(dirname(outputJsonPath), { recursive: true });
134
- mkdirSync(dirname(outputDtsPath), { recursive: true });
135
-
136
- // Write the results to files
137
- writeFileSync(outputJsonPath, emitResult.contractJson, 'utf-8');
138
- writeFileSync(outputDtsPath, emitResult.contractDts, 'utf-8');
139
-
140
- // Add blank line after all async operations if spinners were shown
141
- if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {
142
- console.log('');
143
- }
144
-
145
- // Return result with file paths for output formatting
146
- return {
147
- coreHash: emitResult.coreHash,
148
- profileHash: emitResult.profileHash,
149
- outDir: dirname(outputJsonPath),
150
- files: {
151
- json: outputJsonPath,
152
- dts: outputDtsPath,
153
- },
154
- timings: {
155
- total: 0, // Timing is handled by emitContract internally if needed
156
- },
157
- };
158
- });
235
+ const result = await executeContractEmitCommand(options, flags, startTime);
159
236
 
160
237
  // Handle result - formats output and returns exit code
161
238
  const exitCode = handleResult(result, flags, (emitResult) => {