@prisma-next/cli 0.3.0-dev.1 → 0.3.0-dev.10

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 (85) hide show
  1. package/README.md +2 -2
  2. package/dist/{chunk-ZKYEJROM.js → chunk-CVNWLFXO.js} +6 -9
  3. package/dist/chunk-CVNWLFXO.js.map +1 -0
  4. package/dist/{chunk-464LNZCE.js → chunk-QUPBU4KV.js} +5 -8
  5. package/dist/chunk-QUPBU4KV.js.map +1 -0
  6. package/dist/cli.d.ts +2 -0
  7. package/dist/cli.d.ts.map +1 -0
  8. package/dist/cli.js +34 -60
  9. package/dist/cli.js.map +1 -1
  10. package/dist/commands/contract-emit.d.ts +2 -4
  11. package/dist/commands/contract-emit.d.ts.map +1 -0
  12. package/dist/commands/contract-emit.js +1 -1
  13. package/dist/commands/db-init.d.ts +2 -4
  14. package/dist/commands/db-init.d.ts.map +1 -0
  15. package/dist/commands/db-init.js +6 -10
  16. package/dist/commands/db-init.js.map +1 -1
  17. package/dist/commands/db-introspect.d.ts +2 -4
  18. package/dist/commands/db-introspect.d.ts.map +1 -0
  19. package/dist/commands/db-introspect.js +6 -10
  20. package/dist/commands/db-introspect.js.map +1 -1
  21. package/dist/commands/db-schema-verify.d.ts +2 -4
  22. package/dist/commands/db-schema-verify.d.ts.map +1 -0
  23. package/dist/commands/db-schema-verify.js +6 -10
  24. package/dist/commands/db-schema-verify.js.map +1 -1
  25. package/dist/commands/db-sign.d.ts +2 -4
  26. package/dist/commands/db-sign.d.ts.map +1 -0
  27. package/dist/commands/db-sign.js +6 -10
  28. package/dist/commands/db-sign.js.map +1 -1
  29. package/dist/commands/db-verify.d.ts +2 -4
  30. package/dist/commands/db-verify.d.ts.map +1 -0
  31. package/dist/commands/db-verify.js +6 -10
  32. package/dist/commands/db-verify.js.map +1 -1
  33. package/dist/config-loader.d.ts +3 -5
  34. package/dist/config-loader.d.ts.map +1 -0
  35. package/dist/exports/config-types.d.ts +3 -0
  36. package/dist/exports/config-types.d.ts.map +1 -0
  37. package/dist/exports/config-types.js.map +1 -0
  38. package/dist/exports/index.d.ts +4 -0
  39. package/dist/exports/index.d.ts.map +1 -0
  40. package/dist/{index.js → exports/index.js} +3 -3
  41. package/dist/exports/index.js.map +1 -0
  42. package/dist/{index.d.ts → load-ts-contract.d.ts} +4 -8
  43. package/dist/load-ts-contract.d.ts.map +1 -0
  44. package/dist/utils/action.d.ts +16 -0
  45. package/dist/utils/action.d.ts.map +1 -0
  46. package/dist/utils/cli-errors.d.ts +7 -0
  47. package/dist/utils/cli-errors.d.ts.map +1 -0
  48. package/dist/utils/command-helpers.d.ts +12 -0
  49. package/dist/utils/command-helpers.d.ts.map +1 -0
  50. package/dist/utils/framework-components.d.ts +70 -0
  51. package/dist/utils/framework-components.d.ts.map +1 -0
  52. package/dist/utils/global-flags.d.ts +25 -0
  53. package/dist/utils/global-flags.d.ts.map +1 -0
  54. package/dist/utils/output.d.ts +142 -0
  55. package/dist/utils/output.d.ts.map +1 -0
  56. package/dist/utils/result-handler.d.ts +15 -0
  57. package/dist/utils/result-handler.d.ts.map +1 -0
  58. package/dist/utils/spinner.d.ts +29 -0
  59. package/dist/utils/spinner.d.ts.map +1 -0
  60. package/package.json +21 -21
  61. package/src/cli.ts +260 -0
  62. package/src/commands/contract-emit.ts +182 -0
  63. package/src/commands/db-init.ts +452 -0
  64. package/src/commands/db-introspect.ts +256 -0
  65. package/src/commands/db-schema-verify.ts +232 -0
  66. package/src/commands/db-sign.ts +273 -0
  67. package/src/commands/db-verify.ts +229 -0
  68. package/src/config-loader.ts +76 -0
  69. package/src/exports/config-types.ts +6 -0
  70. package/src/exports/index.ts +4 -0
  71. package/src/load-ts-contract.ts +217 -0
  72. package/src/utils/action.ts +43 -0
  73. package/src/utils/cli-errors.ts +26 -0
  74. package/src/utils/command-helpers.ts +26 -0
  75. package/src/utils/framework-components.ts +177 -0
  76. package/src/utils/global-flags.ts +75 -0
  77. package/src/utils/output.ts +1471 -0
  78. package/src/utils/result-handler.ts +44 -0
  79. package/src/utils/spinner.ts +67 -0
  80. package/dist/chunk-464LNZCE.js.map +0 -1
  81. package/dist/chunk-ZKYEJROM.js.map +0 -1
  82. package/dist/config-types.d.ts +0 -1
  83. package/dist/config-types.js.map +0 -1
  84. package/dist/index.js.map +0 -1
  85. /package/dist/{config-types.js → exports/config-types.js} +0 -0
@@ -0,0 +1,273 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { relative, resolve } from 'node:path';
3
+ import {
4
+ errorDatabaseUrlRequired,
5
+ errorDriverRequired,
6
+ errorFileNotFound,
7
+ errorRuntime,
8
+ errorUnexpected,
9
+ } from '@prisma-next/core-control-plane/errors';
10
+ import type {
11
+ SignDatabaseResult,
12
+ VerifyDatabaseSchemaResult,
13
+ } from '@prisma-next/core-control-plane/types';
14
+ import { createControlPlaneStack } from '@prisma-next/core-control-plane/types';
15
+ import { Command } from 'commander';
16
+ import { loadConfig } from '../config-loader';
17
+ import { performAction } from '../utils/action';
18
+ import { setCommandDescriptions } from '../utils/command-helpers';
19
+ import {
20
+ assertContractRequirementsSatisfied,
21
+ assertFrameworkComponentsCompatible,
22
+ } from '../utils/framework-components';
23
+ import { parseGlobalFlags } from '../utils/global-flags';
24
+ import {
25
+ formatCommandHelp,
26
+ formatSchemaVerifyJson,
27
+ formatSchemaVerifyOutput,
28
+ formatSignJson,
29
+ formatSignOutput,
30
+ formatStyledHeader,
31
+ } from '../utils/output';
32
+ import { handleResult } from '../utils/result-handler';
33
+ import { withSpinner } from '../utils/spinner';
34
+
35
+ interface DbSignOptions {
36
+ readonly db?: string;
37
+ readonly config?: string;
38
+ readonly json?: string | boolean;
39
+ readonly quiet?: boolean;
40
+ readonly q?: boolean;
41
+ readonly verbose?: boolean;
42
+ readonly v?: boolean;
43
+ readonly vv?: boolean;
44
+ readonly trace?: boolean;
45
+ readonly timestamps?: boolean;
46
+ readonly color?: boolean;
47
+ readonly 'no-color'?: boolean;
48
+ }
49
+
50
+ export function createDbSignCommand(): Command {
51
+ const command = new Command('sign');
52
+ setCommandDescriptions(
53
+ command,
54
+ 'Sign the database with your contract so you can safely run queries',
55
+ 'Verifies that your database schema satisfies the emitted contract, and if so, writes or\n' +
56
+ 'updates the contract marker in the database. This command is idempotent and safe to run\n' +
57
+ 'in CI/deployment pipelines. The marker records that this database instance is aligned\n' +
58
+ 'with a specific contract version.',
59
+ );
60
+ command
61
+ .configureHelp({
62
+ formatHelp: (cmd) => {
63
+ const flags = parseGlobalFlags({});
64
+ return formatCommandHelp({ command: cmd, flags });
65
+ },
66
+ })
67
+ .option('--db <url>', 'Database connection string')
68
+ .option('--config <path>', 'Path to prisma-next.config.ts')
69
+ .option('--json [format]', 'Output as JSON (object or ndjson)', false)
70
+ .option('-q, --quiet', 'Quiet mode: errors only')
71
+ .option('-v, --verbose', 'Verbose output: debug info, timings')
72
+ .option('-vv, --trace', 'Trace output: deep internals, stack traces')
73
+ .option('--timestamps', 'Add timestamps to output')
74
+ .option('--color', 'Force color output')
75
+ .option('--no-color', 'Disable color output')
76
+ .action(async (options: DbSignOptions) => {
77
+ const flags = parseGlobalFlags(options);
78
+
79
+ const result = await performAction(async () => {
80
+ // Load config (file I/O)
81
+ const config = await loadConfig(options.config);
82
+ // Normalize config path for display (match contract path format - no ./ prefix)
83
+ const configPath = options.config
84
+ ? relative(process.cwd(), resolve(options.config))
85
+ : 'prisma-next.config.ts';
86
+ const contractPathAbsolute = config.contract?.output
87
+ ? resolve(config.contract.output)
88
+ : resolve('src/prisma/contract.json');
89
+ // Convert to relative path for display
90
+ const contractPath = relative(process.cwd(), contractPathAbsolute);
91
+
92
+ // Output header (only for human-readable output)
93
+ if (flags.json !== 'object' && !flags.quiet) {
94
+ const details: Array<{ label: string; value: string }> = [
95
+ { label: 'config', value: configPath },
96
+ { label: 'contract', value: contractPath },
97
+ ];
98
+ if (options.db) {
99
+ details.push({ label: 'database', value: options.db });
100
+ }
101
+ const header = formatStyledHeader({
102
+ command: 'db sign',
103
+ description: 'Sign the database with your contract so you can safely run queries',
104
+ url: 'https://pris.ly/db-sign',
105
+ details,
106
+ flags,
107
+ });
108
+ console.log(header);
109
+ }
110
+
111
+ // Load contract file (file I/O)
112
+ let contractJsonContent: string;
113
+ try {
114
+ contractJsonContent = await readFile(contractPathAbsolute, 'utf-8');
115
+ } catch (error) {
116
+ if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
117
+ throw errorFileNotFound(contractPathAbsolute, {
118
+ why: `Contract file not found at ${contractPathAbsolute}`,
119
+ });
120
+ }
121
+ throw errorUnexpected(error instanceof Error ? error.message : String(error), {
122
+ why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`,
123
+ });
124
+ }
125
+ const contractJson = JSON.parse(contractJsonContent) as Record<string, unknown>;
126
+
127
+ // Resolve database URL
128
+ const dbUrl = options.db ?? config.db?.url;
129
+ if (!dbUrl) {
130
+ throw errorDatabaseUrlRequired();
131
+ }
132
+
133
+ // Check for driver
134
+ if (!config.driver) {
135
+ throw errorDriverRequired();
136
+ }
137
+
138
+ // Store driver descriptor after null check
139
+ const driverDescriptor = config.driver;
140
+
141
+ // Create family instance (needed for contract validation - no DB connection required)
142
+ const stack = createControlPlaneStack({
143
+ target: config.target,
144
+ adapter: config.adapter,
145
+ driver: driverDescriptor,
146
+ extensionPacks: config.extensionPacks,
147
+ });
148
+ const familyInstance = config.family.create(stack);
149
+
150
+ // Validate contract using instance validator (fail-fast before DB connection)
151
+ const contractIR = familyInstance.validateContractIR(contractJson);
152
+ assertContractRequirementsSatisfied({ contract: contractIR, stack });
153
+
154
+ const rawComponents = [config.target, config.adapter, ...(config.extensionPacks ?? [])];
155
+ const frameworkComponents = assertFrameworkComponentsCompatible(
156
+ config.family.familyId,
157
+ config.target.targetId,
158
+ rawComponents,
159
+ );
160
+
161
+ // Create driver (expensive operation - done after validation)
162
+ const driver = await driverDescriptor.create(dbUrl);
163
+
164
+ try {
165
+ // Schema verification precondition with spinner
166
+ let schemaVerifyResult: VerifyDatabaseSchemaResult;
167
+ try {
168
+ schemaVerifyResult = await withSpinner(
169
+ () =>
170
+ familyInstance.schemaVerify({
171
+ driver,
172
+ contractIR,
173
+ strict: false,
174
+ contractPath: contractPathAbsolute,
175
+ configPath,
176
+ frameworkComponents,
177
+ }),
178
+ {
179
+ message: 'Verifying database satisfies contract',
180
+ flags,
181
+ },
182
+ );
183
+ } catch (error) {
184
+ // Wrap errors from schemaVerify() in structured error
185
+ throw errorRuntime(error instanceof Error ? error.message : String(error), {
186
+ why: `Failed to verify database schema: ${error instanceof Error ? error.message : String(error)}`,
187
+ });
188
+ }
189
+
190
+ // If schema verification failed, return both results for handling outside performAction
191
+ if (!schemaVerifyResult.ok) {
192
+ return { schemaVerifyResult, signResult: undefined };
193
+ }
194
+
195
+ // Schema verification passed - proceed with signing
196
+ let signResult: SignDatabaseResult;
197
+ try {
198
+ signResult = await withSpinner(
199
+ () =>
200
+ familyInstance.sign({
201
+ driver,
202
+ contractIR,
203
+ contractPath: contractPathAbsolute,
204
+ configPath,
205
+ }),
206
+ {
207
+ message: 'Signing database...',
208
+ flags,
209
+ },
210
+ );
211
+ } catch (error) {
212
+ // Wrap errors from sign() in structured error
213
+ throw errorRuntime(error instanceof Error ? error.message : String(error), {
214
+ why: `Failed to sign database: ${error instanceof Error ? error.message : String(error)}`,
215
+ });
216
+ }
217
+
218
+ // Add blank line after all async operations if spinners were shown
219
+ if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {
220
+ console.log('');
221
+ }
222
+
223
+ return { schemaVerifyResult: undefined, signResult };
224
+ } finally {
225
+ // Ensure driver connection is closed
226
+ await driver.close();
227
+ }
228
+ });
229
+
230
+ // Handle result - formats output and returns exit code
231
+ const exitCode = handleResult(result, flags, (value) => {
232
+ const { schemaVerifyResult, signResult } = value;
233
+
234
+ // If schema verification failed, format and print schema verification output
235
+ if (schemaVerifyResult && !schemaVerifyResult.ok) {
236
+ if (flags.json === 'object') {
237
+ console.log(formatSchemaVerifyJson(schemaVerifyResult));
238
+ } else {
239
+ const output = formatSchemaVerifyOutput(schemaVerifyResult, flags);
240
+ if (output) {
241
+ console.log(output);
242
+ }
243
+ }
244
+ // Don't proceed to sign output formatting
245
+ return;
246
+ }
247
+
248
+ // Schema verification passed - format sign output
249
+ if (signResult) {
250
+ if (flags.json === 'object') {
251
+ console.log(formatSignJson(signResult));
252
+ } else {
253
+ const output = formatSignOutput(signResult, flags);
254
+ if (output) {
255
+ console.log(output);
256
+ }
257
+ }
258
+ }
259
+ });
260
+
261
+ // For logical schema mismatches, check if schema verification passed
262
+ // Infra errors already handled by handleResult (returns non-zero exit code)
263
+ if (result.ok && result.value.schemaVerifyResult && !result.value.schemaVerifyResult.ok) {
264
+ // Schema verification failed - exit with code 1
265
+ process.exit(1);
266
+ } else {
267
+ // Success or infra error - use exit code from handleResult
268
+ process.exit(exitCode);
269
+ }
270
+ });
271
+
272
+ return command;
273
+ }
@@ -0,0 +1,229 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { relative, resolve } from 'node:path';
3
+ import type { ContractIR } from '@prisma-next/contract/ir';
4
+ import {
5
+ errorDatabaseUrlRequired,
6
+ errorDriverRequired,
7
+ errorFileNotFound,
8
+ errorHashMismatch,
9
+ errorMarkerMissing,
10
+ errorRuntime,
11
+ errorTargetMismatch,
12
+ errorUnexpected,
13
+ } from '@prisma-next/core-control-plane/errors';
14
+ import type { VerifyDatabaseResult } from '@prisma-next/core-control-plane/types';
15
+ import { createControlPlaneStack } from '@prisma-next/core-control-plane/types';
16
+ import { Command } from 'commander';
17
+ import { loadConfig } from '../config-loader';
18
+ import { performAction } from '../utils/action';
19
+ import { setCommandDescriptions } from '../utils/command-helpers';
20
+ import { assertContractRequirementsSatisfied } from '../utils/framework-components';
21
+ import { parseGlobalFlags } from '../utils/global-flags';
22
+ import {
23
+ formatCommandHelp,
24
+ formatStyledHeader,
25
+ formatVerifyJson,
26
+ formatVerifyOutput,
27
+ } from '../utils/output';
28
+ import { handleResult } from '../utils/result-handler';
29
+ import { withSpinner } from '../utils/spinner';
30
+
31
+ interface DbVerifyOptions {
32
+ readonly db?: string;
33
+ readonly config?: string;
34
+ readonly json?: string | boolean;
35
+ readonly quiet?: boolean;
36
+ readonly q?: boolean;
37
+ readonly verbose?: boolean;
38
+ readonly v?: boolean;
39
+ readonly vv?: boolean;
40
+ readonly trace?: boolean;
41
+ readonly timestamps?: boolean;
42
+ readonly color?: boolean;
43
+ readonly 'no-color'?: boolean;
44
+ }
45
+
46
+ export function createDbVerifyCommand(): Command {
47
+ const command = new Command('verify');
48
+ setCommandDescriptions(
49
+ command,
50
+ 'Check whether the database has been signed with your contract',
51
+ 'Verifies that your database schema matches the emitted contract. Checks table structures,\n' +
52
+ 'column types, constraints, and codec coverage. Reports any mismatches or missing codecs.',
53
+ );
54
+ command
55
+ .configureHelp({
56
+ formatHelp: (cmd) => {
57
+ const flags = parseGlobalFlags({});
58
+ return formatCommandHelp({ command: cmd, flags });
59
+ },
60
+ })
61
+ .option('--db <url>', 'Database connection string')
62
+ .option('--config <path>', 'Path to prisma-next.config.ts')
63
+ .option('--json [format]', 'Output as JSON (object or ndjson)', false)
64
+ .option('-q, --quiet', 'Quiet mode: errors only')
65
+ .option('-v, --verbose', 'Verbose output: debug info, timings')
66
+ .option('-vv, --trace', 'Trace output: deep internals, stack traces')
67
+ .option('--timestamps', 'Add timestamps to output')
68
+ .option('--color', 'Force color output')
69
+ .option('--no-color', 'Disable color output')
70
+ .action(async (options: DbVerifyOptions) => {
71
+ const flags = parseGlobalFlags(options);
72
+
73
+ const result = await performAction(async () => {
74
+ // Load config (file I/O)
75
+ const config = await loadConfig(options.config);
76
+ // Normalize config path for display (match contract path format - no ./ prefix)
77
+ const configPath = options.config
78
+ ? relative(process.cwd(), resolve(options.config))
79
+ : 'prisma-next.config.ts';
80
+ const contractPathAbsolute = config.contract?.output
81
+ ? resolve(config.contract.output)
82
+ : resolve('src/prisma/contract.json');
83
+ // Convert to relative path for display
84
+ const contractPath = relative(process.cwd(), contractPathAbsolute);
85
+
86
+ // Output header (only for human-readable output)
87
+ if (flags.json !== 'object' && !flags.quiet) {
88
+ const details: Array<{ label: string; value: string }> = [
89
+ { label: 'config', value: configPath },
90
+ { label: 'contract', value: contractPath },
91
+ ];
92
+ if (options.db) {
93
+ details.push({ label: 'database', value: options.db });
94
+ }
95
+ const header = formatStyledHeader({
96
+ command: 'db verify',
97
+ description: 'Check whether the database has been signed with your contract',
98
+ url: 'https://pris.ly/db-verify',
99
+ details,
100
+ flags,
101
+ });
102
+ console.log(header);
103
+ }
104
+
105
+ // Load contract file (file I/O)
106
+ let contractJsonContent: string;
107
+ try {
108
+ contractJsonContent = await readFile(contractPathAbsolute, 'utf-8');
109
+ } catch (error) {
110
+ if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
111
+ throw errorFileNotFound(contractPathAbsolute, {
112
+ why: `Contract file not found at ${contractPathAbsolute}`,
113
+ });
114
+ }
115
+ throw errorUnexpected(error instanceof Error ? error.message : String(error), {
116
+ why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`,
117
+ });
118
+ }
119
+ const contractJson = JSON.parse(contractJsonContent) as Record<string, unknown>;
120
+
121
+ // Resolve database URL
122
+ const dbUrl = options.db ?? config.db?.url;
123
+ if (!dbUrl) {
124
+ throw errorDatabaseUrlRequired();
125
+ }
126
+
127
+ // Check for driver
128
+ if (!config.driver) {
129
+ throw errorDriverRequired();
130
+ }
131
+
132
+ // Store driver descriptor after null check
133
+ const driverDescriptor = config.driver;
134
+
135
+ // Create driver
136
+ const driver = await withSpinner(() => driverDescriptor.create(dbUrl), {
137
+ message: 'Connecting to database...',
138
+ flags,
139
+ });
140
+
141
+ try {
142
+ // Create family instance
143
+ const stack = createControlPlaneStack({
144
+ target: config.target,
145
+ adapter: config.adapter,
146
+ driver: driverDescriptor,
147
+ extensionPacks: config.extensionPacks,
148
+ });
149
+ const familyInstance = config.family.create(stack);
150
+
151
+ // Validate contract using instance validator
152
+ const contractIR = familyInstance.validateContractIR(contractJson) as ContractIR;
153
+ assertContractRequirementsSatisfied({ contract: contractIR, stack });
154
+
155
+ // Call family instance verify method
156
+ let verifyResult: VerifyDatabaseResult;
157
+ try {
158
+ verifyResult = await withSpinner(
159
+ () =>
160
+ familyInstance.verify({
161
+ driver,
162
+ contractIR,
163
+ expectedTargetId: config.target.targetId,
164
+ contractPath: contractPathAbsolute,
165
+ configPath,
166
+ }),
167
+ {
168
+ message: 'Verifying database schema...',
169
+ flags,
170
+ },
171
+ );
172
+ } catch (error) {
173
+ // Wrap errors from verify() in structured error
174
+ throw errorUnexpected(error instanceof Error ? error.message : String(error), {
175
+ why: `Failed to verify database: ${error instanceof Error ? error.message : String(error)}`,
176
+ });
177
+ }
178
+
179
+ // If verification failed, throw structured error
180
+ if (!verifyResult.ok && verifyResult.code) {
181
+ if (verifyResult.code === 'PN-RTM-3001') {
182
+ throw errorMarkerMissing();
183
+ }
184
+ if (verifyResult.code === 'PN-RTM-3002') {
185
+ throw errorHashMismatch({
186
+ expected: verifyResult.contract.coreHash,
187
+ ...(verifyResult.marker?.coreHash ? { actual: verifyResult.marker.coreHash } : {}),
188
+ });
189
+ }
190
+ if (verifyResult.code === 'PN-RTM-3003') {
191
+ throw errorTargetMismatch(
192
+ verifyResult.target.expected,
193
+ verifyResult.target.actual ?? 'unknown',
194
+ );
195
+ }
196
+ throw errorRuntime(verifyResult.summary);
197
+ }
198
+
199
+ // Add blank line after all async operations if spinners were shown
200
+ if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {
201
+ console.log('');
202
+ }
203
+
204
+ return verifyResult;
205
+ } finally {
206
+ // Ensure driver connection is closed
207
+ await driver.close();
208
+ }
209
+ });
210
+
211
+ // Handle result - formats output and returns exit code
212
+ const exitCode = handleResult(result, flags, (verifyResult) => {
213
+ // Output based on flags
214
+ if (flags.json === 'object') {
215
+ // JSON output to stdout
216
+ console.log(formatVerifyJson(verifyResult));
217
+ } else {
218
+ // Human-readable output to stdout
219
+ const output = formatVerifyOutput(verifyResult, flags);
220
+ if (output) {
221
+ console.log(output);
222
+ }
223
+ }
224
+ });
225
+ process.exit(exitCode);
226
+ });
227
+
228
+ return command;
229
+ }
@@ -0,0 +1,76 @@
1
+ import { dirname, resolve } from 'node:path';
2
+ import type { PrismaNextConfig } from '@prisma-next/core-control-plane/config-types';
3
+ import { validateConfig } from '@prisma-next/core-control-plane/config-validation';
4
+ import { errorConfigFileNotFound, errorUnexpected } from '@prisma-next/core-control-plane/errors';
5
+ import { loadConfig as loadConfigC12 } from 'c12';
6
+
7
+ /**
8
+ * Loads the Prisma Next config from a TypeScript file.
9
+ * Supports both default export and named export.
10
+ * Uses c12 to automatically handle TypeScript compilation and config file discovery.
11
+ *
12
+ * @param configPath - Optional path to config file. Defaults to `./prisma-next.config.ts` in current directory.
13
+ * @returns The loaded config object.
14
+ * @throws Error if config file doesn't exist or is invalid.
15
+ */
16
+ export async function loadConfig(configPath?: string): Promise<PrismaNextConfig> {
17
+ try {
18
+ const cwd = process.cwd();
19
+ // Resolve config path to absolute path and set cwd to config directory when path is provided
20
+ const resolvedConfigPath = configPath ? resolve(cwd, configPath) : undefined;
21
+ const configCwd = resolvedConfigPath ? dirname(resolvedConfigPath) : cwd;
22
+
23
+ const result = await loadConfigC12<PrismaNextConfig>({
24
+ name: 'prisma-next',
25
+ ...(resolvedConfigPath ? { configFile: resolvedConfigPath } : {}),
26
+ cwd: configCwd,
27
+ });
28
+
29
+ // When a specific config file was requested, verify it was actually loaded
30
+ // (c12 falls back to searching by name if the specified file doesn't exist)
31
+ if (resolvedConfigPath && result.configFile !== resolvedConfigPath) {
32
+ throw errorConfigFileNotFound(resolvedConfigPath);
33
+ }
34
+
35
+ // Check if config is missing or empty (c12 may return empty object when file doesn't exist)
36
+ if (!result.config || Object.keys(result.config).length === 0) {
37
+ // Use c12's configFile if available, otherwise use explicit configPath, otherwise omit path
38
+ const displayPath = result.configFile || resolvedConfigPath || configPath;
39
+ throw errorConfigFileNotFound(displayPath);
40
+ }
41
+
42
+ // Validate config structure
43
+ validateConfig(result.config);
44
+
45
+ return result.config;
46
+ } catch (error) {
47
+ // Re-throw structured errors as-is
48
+ if (
49
+ error instanceof Error &&
50
+ 'code' in error &&
51
+ typeof (error as { code: string }).code === 'string'
52
+ ) {
53
+ throw error;
54
+ }
55
+
56
+ if (error instanceof Error) {
57
+ // Check for file not found errors
58
+ if (
59
+ error.message.includes('not found') ||
60
+ error.message.includes('Cannot find') ||
61
+ error.message.includes('ENOENT')
62
+ ) {
63
+ // Use resolved path if available, otherwise use original configPath
64
+ const displayPath = configPath ? resolve(process.cwd(), configPath) : undefined;
65
+ throw errorConfigFileNotFound(displayPath, {
66
+ why: error.message,
67
+ });
68
+ }
69
+ // For other errors, wrap in unexpected error
70
+ throw errorUnexpected(error.message, {
71
+ why: `Failed to load config: ${error.message}`,
72
+ });
73
+ }
74
+ throw errorUnexpected(String(error));
75
+ }
76
+ }
@@ -0,0 +1,6 @@
1
+ // Re-export core-control-plane config types for convenience
2
+ export type {
3
+ ContractConfig,
4
+ PrismaNextConfig,
5
+ } from '@prisma-next/core-control-plane/config-types';
6
+ export { defineConfig } from '@prisma-next/core-control-plane/config-types';
@@ -0,0 +1,4 @@
1
+ // CLI-specific exports
2
+ export { createContractEmitCommand } from '../commands/contract-emit';
3
+ export type { LoadTsContractOptions } from '../load-ts-contract';
4
+ export { loadContractFromTs } from '../load-ts-contract';