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