@prisma-next/cli 0.5.0-dev.2 → 0.5.0-dev.21
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/README.md +54 -21
- package/dist/agent-skill-mongo.md +63 -31
- package/dist/agent-skill-postgres.md +1 -1
- package/dist/{cli-errors-C0JhVj0c.d.mts → cli-errors-BJLUczXT.d.mts} +1 -0
- package/dist/cli-errors-By1iVE3z.mjs +34 -0
- package/dist/cli-errors-By1iVE3z.mjs.map +1 -0
- package/dist/cli.mjs +127 -13
- package/dist/cli.mjs.map +1 -1
- package/dist/{client-TG7rbCWT.mjs → client-enZIahga.mjs} +20 -5
- package/dist/client-enZIahga.mjs.map +1 -0
- package/dist/commands/contract-emit.d.mts.map +1 -1
- package/dist/commands/contract-emit.mjs +7 -2
- package/dist/commands/contract-infer.mjs +8 -2
- package/dist/commands/db-init.mjs +9 -8
- package/dist/commands/db-init.mjs.map +1 -1
- package/dist/commands/db-schema.mjs +8 -5
- package/dist/commands/db-schema.mjs.map +1 -1
- package/dist/commands/db-sign.mjs +8 -7
- package/dist/commands/db-sign.mjs.map +1 -1
- package/dist/commands/db-update.mjs +9 -8
- package/dist/commands/db-update.mjs.map +1 -1
- package/dist/commands/db-verify.mjs +10 -9
- package/dist/commands/db-verify.mjs.map +1 -1
- package/dist/commands/migration-apply.d.mts +1 -1
- package/dist/commands/migration-apply.d.mts.map +1 -1
- package/dist/commands/migration-apply.mjs +15 -38
- package/dist/commands/migration-apply.mjs.map +1 -1
- package/dist/commands/migration-new.d.mts.map +1 -1
- package/dist/commands/migration-new.mjs +24 -28
- package/dist/commands/migration-new.mjs.map +1 -1
- package/dist/commands/migration-plan.d.mts +6 -3
- package/dist/commands/migration-plan.d.mts.map +1 -1
- package/dist/commands/migration-plan.mjs +38 -38
- package/dist/commands/migration-plan.mjs.map +1 -1
- package/dist/commands/migration-ref.d.mts +6 -4
- package/dist/commands/migration-ref.d.mts.map +1 -1
- package/dist/commands/migration-ref.mjs +31 -40
- package/dist/commands/migration-ref.mjs.map +1 -1
- package/dist/commands/migration-show.d.mts +4 -4
- package/dist/commands/migration-show.d.mts.map +1 -1
- package/dist/commands/migration-show.mjs +19 -26
- package/dist/commands/migration-show.mjs.map +1 -1
- package/dist/commands/migration-status.d.mts +5 -4
- package/dist/commands/migration-status.d.mts.map +1 -1
- package/dist/commands/migration-status.mjs +7 -2
- package/dist/{config-loader-_W4T21X1.mjs → config-loader-ih8ViDb_.mjs} +2 -2
- package/dist/config-loader-ih8ViDb_.mjs.map +1 -0
- package/dist/config-loader.mjs +1 -1
- package/dist/contract-emit-DS5NzZh2.mjs +6 -0
- package/dist/contract-emit-DWtGQYCD.mjs +150 -0
- package/dist/contract-emit-DWtGQYCD.mjs.map +1 -0
- package/dist/contract-emit-RZBWzkop.mjs +329 -0
- package/dist/contract-emit-RZBWzkop.mjs.map +1 -0
- package/dist/{contract-enrichment-CGW6mm-E.mjs → contract-enrichment-4Ptgw3Pe.mjs} +1 -1
- package/dist/{contract-enrichment-CGW6mm-E.mjs.map → contract-enrichment-4Ptgw3Pe.mjs.map} +1 -1
- package/dist/{contract-infer-BP3DrGgz.mjs → contract-infer-BjzkcwQt.mjs} +5 -5
- package/dist/{contract-infer-BP3DrGgz.mjs.map → contract-infer-BjzkcwQt.mjs.map} +1 -1
- package/dist/exports/control-api.d.mts +41 -16
- package/dist/exports/control-api.d.mts.map +1 -1
- package/dist/exports/control-api.mjs +7 -5
- package/dist/exports/index.mjs +8 -3
- package/dist/exports/index.mjs.map +1 -1
- package/dist/exports/init-output.d.mts +39 -0
- package/dist/exports/init-output.d.mts.map +1 -0
- package/dist/exports/init-output.mjs +3 -0
- package/dist/{extract-operation-statements-DZUJNmL3.mjs → extract-operation-statements-CU-Pp4-N.mjs} +2 -2
- package/dist/{extract-operation-statements-DZUJNmL3.mjs.map → extract-operation-statements-CU-Pp4-N.mjs.map} +1 -1
- package/dist/{extract-sql-ddl-DDMX-9mz.mjs → extract-sql-ddl-Bm0Mm0IT.mjs} +1 -1
- package/dist/{extract-sql-ddl-DDMX-9mz.mjs.map → extract-sql-ddl-Bm0Mm0IT.mjs.map} +1 -1
- package/dist/{framework-components-DfZKQBQ2.mjs → framework-components-Bgcre3Z6.mjs} +2 -2
- package/dist/{framework-components-DfZKQBQ2.mjs.map → framework-components-Bgcre3Z6.mjs.map} +1 -1
- package/dist/init-C-H-if1m.mjs +2062 -0
- package/dist/init-C-H-if1m.mjs.map +1 -0
- package/dist/{inspect-live-schema-DWzf4Q_m.mjs → inspect-live-schema-QklSDLt_.mjs} +6 -6
- package/dist/{inspect-live-schema-DWzf4Q_m.mjs.map → inspect-live-schema-QklSDLt_.mjs.map} +1 -1
- package/dist/migration-cli.mjs +15 -8
- package/dist/migration-cli.mjs.map +1 -1
- package/dist/{migration-command-scaffold-CLMD302g.mjs → migration-command-scaffold-BfloSWPZ.mjs} +7 -7
- package/dist/{migration-command-scaffold-CLMD302g.mjs.map → migration-command-scaffold-BfloSWPZ.mjs.map} +1 -1
- package/dist/{migration-status-B0HLF7So.mjs → migration-status-C5VYA5r9.mjs} +21 -35
- package/dist/migration-status-C5VYA5r9.mjs.map +1 -0
- package/dist/{migrations-B0dOQlk0.mjs → migrations-CSaDHNpB.mjs} +3 -3
- package/dist/migrations-CSaDHNpB.mjs.map +1 -0
- package/dist/output-BiO7kt87.mjs +103 -0
- package/dist/output-BiO7kt87.mjs.map +1 -0
- package/dist/{progress-adapter-B-YvmcDu.mjs → progress-adapter-DgRGldpT.mjs} +1 -1
- package/dist/{progress-adapter-B-YvmcDu.mjs.map → progress-adapter-DgRGldpT.mjs.map} +1 -1
- package/dist/quick-reference-mongo.md +34 -13
- package/dist/quick-reference-postgres.md +11 -9
- package/dist/{result-handler-CIyu0Pdt.mjs → result-handler-BmVh8AeV.mjs} +12 -93
- package/dist/result-handler-BmVh8AeV.mjs.map +1 -0
- package/dist/{terminal-ui-C5k88MmW.mjs → terminal-ui-u2YgKghu.mjs} +76 -2
- package/dist/terminal-ui-u2YgKghu.mjs.map +1 -0
- package/dist/{verify-BxiVp50b.mjs → verify-BumcH6Ry.mjs} +2 -2
- package/dist/{verify-BxiVp50b.mjs.map → verify-BumcH6Ry.mjs.map} +1 -1
- package/package.json +20 -15
- package/src/commands/contract-emit.ts +67 -163
- package/src/commands/init/detect-pnpm-catalog.ts +141 -0
- package/src/commands/init/errors.ts +254 -0
- package/src/commands/init/exit-codes.ts +62 -0
- package/src/commands/init/hygiene-gitattributes.ts +97 -0
- package/src/commands/init/hygiene-gitignore.ts +48 -0
- package/src/commands/init/hygiene-package-scripts.ts +91 -0
- package/src/commands/init/index.ts +112 -7
- package/src/commands/init/init.ts +766 -144
- package/src/commands/init/inputs.ts +421 -0
- package/src/commands/init/output.ts +147 -0
- package/src/commands/init/probe-db.ts +308 -0
- package/src/commands/init/reinit-cleanup.ts +83 -0
- package/src/commands/init/templates/agent-skill-mongo.md +63 -31
- package/src/commands/init/templates/agent-skill-postgres.md +1 -1
- package/src/commands/init/templates/agent-skill.ts +25 -3
- package/src/commands/init/templates/code-templates.ts +125 -32
- package/src/commands/init/templates/env.ts +80 -0
- package/src/commands/init/templates/quick-reference-mongo.md +34 -13
- package/src/commands/init/templates/quick-reference-postgres.md +11 -9
- package/src/commands/init/templates/quick-reference.ts +42 -3
- package/src/commands/init/templates/tsconfig.ts +167 -5
- package/src/commands/migration-apply.ts +15 -50
- package/src/commands/migration-new.ts +24 -28
- package/src/commands/migration-plan.ts +58 -42
- package/src/commands/migration-ref.ts +40 -54
- package/src/commands/migration-show.ts +27 -28
- package/src/commands/migration-status.ts +33 -50
- package/src/config-path-validation.ts +0 -1
- package/src/control-api/operations/contract-emit.ts +198 -115
- package/src/control-api/operations/migration-apply.ts +15 -0
- package/src/control-api/types.ts +22 -3
- package/src/exports/control-api.ts +2 -1
- package/src/exports/init-output.ts +10 -0
- package/src/migration-cli.ts +16 -9
- package/src/utils/cli-errors.ts +45 -1
- package/src/utils/command-helpers.ts +13 -26
- package/src/utils/emit-queue.ts +26 -0
- package/src/utils/formatters/graph-migration-mapper.ts +2 -2
- package/src/utils/formatters/migrations.ts +2 -2
- package/src/utils/publish-contract-artifact-pair.ts +134 -0
- package/dist/cli-errors-DHq6GQGu.mjs +0 -5
- package/dist/client-TG7rbCWT.mjs.map +0 -1
- package/dist/config-loader-_W4T21X1.mjs.map +0 -1
- package/dist/contract-emit-CNYyzJwF.mjs +0 -195
- package/dist/contract-emit-CNYyzJwF.mjs.map +0 -1
- package/dist/contract-emit-CQfj7xJn.mjs +0 -122
- package/dist/contract-emit-CQfj7xJn.mjs.map +0 -1
- package/dist/contract-emit-fhNwwhkQ.mjs +0 -4
- package/dist/init-CQfo_4Ro.mjs +0 -430
- package/dist/init-CQfo_4Ro.mjs.map +0 -1
- package/dist/migration-status-B0HLF7So.mjs.map +0 -1
- package/dist/migrations-B0dOQlk0.mjs.map +0 -1
- package/dist/result-handler-CIyu0Pdt.mjs.map +0 -1
- package/dist/terminal-ui-C5k88MmW.mjs.map +0 -1
- package/dist/validate-contract-deps-esa-VQ0h.mjs +0 -37
- package/dist/validate-contract-deps-esa-VQ0h.mjs.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdir
|
|
1
|
+
import { mkdir } from 'node:fs/promises';
|
|
2
2
|
import type { Contract } from '@prisma-next/contract/types';
|
|
3
3
|
import { emit, getEmittedArtifactPaths } from '@prisma-next/emitter';
|
|
4
4
|
import { createControlStack } from '@prisma-next/framework-components/control';
|
|
@@ -7,57 +7,156 @@ import { ifDefined } from '@prisma-next/utils/defined';
|
|
|
7
7
|
import { dirname } from 'pathe';
|
|
8
8
|
import { loadConfig } from '../../config-loader';
|
|
9
9
|
import { errorContractConfigMissing, errorRuntime } from '../../utils/cli-errors';
|
|
10
|
+
import { queueEmitByOutput } from '../../utils/emit-queue';
|
|
10
11
|
import { assertFrameworkComponentsCompatible } from '../../utils/framework-components';
|
|
12
|
+
import { publishContractArtifactPair } from '../../utils/publish-contract-artifact-pair';
|
|
13
|
+
import { validateContractDeps } from '../../utils/validate-contract-deps';
|
|
11
14
|
import { enrichContract } from '../contract-enrichment';
|
|
12
|
-
import type {
|
|
15
|
+
import type {
|
|
16
|
+
ContractEmitOptions,
|
|
17
|
+
ContractEmitResult,
|
|
18
|
+
ControlActionName,
|
|
19
|
+
OnControlProgress,
|
|
20
|
+
} from '../types';
|
|
13
21
|
|
|
14
|
-
|
|
15
|
-
readonly summary: string;
|
|
16
|
-
readonly diagnostics: readonly unknown[];
|
|
17
|
-
readonly meta?: unknown;
|
|
18
|
-
}
|
|
22
|
+
const EMIT_ACTION: ControlActionName = 'emit';
|
|
19
23
|
|
|
20
24
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
21
25
|
return typeof value === 'object' && value !== null;
|
|
22
26
|
}
|
|
23
27
|
|
|
24
|
-
function
|
|
25
|
-
|
|
28
|
+
function startSpan(onProgress: OnControlProgress | undefined, spanId: string, label: string): void {
|
|
29
|
+
onProgress?.({ action: EMIT_ACTION, kind: 'spanStart', spanId, label });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function endSpan(
|
|
33
|
+
onProgress: OnControlProgress | undefined,
|
|
34
|
+
spanId: string,
|
|
35
|
+
outcome: 'ok' | 'error',
|
|
36
|
+
): void {
|
|
37
|
+
onProgress?.({ action: EMIT_ACTION, kind: 'spanEnd', spanId, outcome });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function failedToResolveContractSource(why: string, fix: string, meta?: Record<string, unknown>) {
|
|
41
|
+
return errorRuntime('Failed to resolve contract source', {
|
|
42
|
+
why,
|
|
43
|
+
fix,
|
|
44
|
+
...ifDefined('meta', meta),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type ValidatedProviderResult =
|
|
49
|
+
| { readonly ok: true; readonly value: unknown }
|
|
50
|
+
| { readonly ok: false; readonly error: ReturnType<typeof errorRuntime> };
|
|
51
|
+
|
|
52
|
+
function diagnosticLocationSuffix(diagnostic: Record<string, unknown>): string {
|
|
53
|
+
const sourceId = typeof diagnostic['sourceId'] === 'string' ? diagnostic['sourceId'] : undefined;
|
|
54
|
+
const span = isRecord(diagnostic['span']) ? diagnostic['span'] : undefined;
|
|
55
|
+
const start = span && isRecord(span['start']) ? span['start'] : undefined;
|
|
56
|
+
const line = start && typeof start['line'] === 'number' ? start['line'] : undefined;
|
|
57
|
+
const column = start && typeof start['column'] === 'number' ? start['column'] : undefined;
|
|
58
|
+
if (sourceId && line !== undefined && column !== undefined) {
|
|
59
|
+
return ` (${sourceId}:${line}:${column})`;
|
|
60
|
+
}
|
|
61
|
+
if (sourceId) {
|
|
62
|
+
return ` (${sourceId})`;
|
|
63
|
+
}
|
|
64
|
+
return '';
|
|
26
65
|
}
|
|
27
66
|
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
67
|
+
function mapDiagnosticsToIssues(
|
|
68
|
+
diagnostics: readonly unknown[],
|
|
69
|
+
): ReadonlyArray<{ readonly kind: string; readonly message: string }> {
|
|
70
|
+
const issues: { readonly kind: string; readonly message: string }[] = [];
|
|
71
|
+
for (const raw of diagnostics) {
|
|
72
|
+
if (!isRecord(raw)) continue;
|
|
73
|
+
const code = typeof raw['code'] === 'string' ? raw['code'] : 'diagnostic';
|
|
74
|
+
const message = typeof raw['message'] === 'string' ? raw['message'] : '';
|
|
75
|
+
issues.push({ kind: code, message: `${message}${diagnosticLocationSuffix(raw)}` });
|
|
76
|
+
}
|
|
77
|
+
return issues;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function validateProviderResult(providerResult: unknown): ValidatedProviderResult {
|
|
81
|
+
if (!isRecord(providerResult) || typeof providerResult['ok'] !== 'boolean') {
|
|
82
|
+
return {
|
|
83
|
+
ok: false,
|
|
84
|
+
error: failedToResolveContractSource(
|
|
85
|
+
'Contract source provider returned malformed result shape.',
|
|
86
|
+
'Ensure contract.source.load resolves to ok(Contract) or notOk({ summary, diagnostics }).',
|
|
87
|
+
),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (providerResult['ok']) {
|
|
92
|
+
if (!('value' in providerResult)) {
|
|
93
|
+
return {
|
|
94
|
+
ok: false,
|
|
95
|
+
error: failedToResolveContractSource(
|
|
96
|
+
'Contract source provider returned malformed success result: missing value.',
|
|
97
|
+
'Ensure contract.source.load success payload is ok(Contract).',
|
|
98
|
+
),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
return { ok: true, value: providerResult['value'] };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const failure = providerResult['failure'];
|
|
105
|
+
if (
|
|
106
|
+
!isRecord(failure) ||
|
|
107
|
+
typeof failure['summary'] !== 'string' ||
|
|
108
|
+
!Array.isArray(failure['diagnostics'])
|
|
109
|
+
) {
|
|
110
|
+
return {
|
|
111
|
+
ok: false,
|
|
112
|
+
error: failedToResolveContractSource(
|
|
113
|
+
'Contract source provider returned malformed failure result: expected summary and diagnostics.',
|
|
114
|
+
'Ensure contract.source.load failure payload is notOk({ summary, diagnostics, meta? }).',
|
|
115
|
+
),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
ok: false,
|
|
120
|
+
error: failedToResolveContractSource(
|
|
121
|
+
String(failure['summary']),
|
|
122
|
+
'Fix contract source diagnostics and return ok(Contract).',
|
|
123
|
+
{
|
|
124
|
+
diagnostics: failure['diagnostics'],
|
|
125
|
+
issues: mapDiagnosticsToIssues(failure['diagnostics']),
|
|
126
|
+
...ifDefined('providerMeta', failure['meta']),
|
|
127
|
+
},
|
|
128
|
+
),
|
|
129
|
+
};
|
|
32
130
|
}
|
|
33
131
|
|
|
34
132
|
/**
|
|
35
|
-
*
|
|
133
|
+
* Canonical contract emit operation.
|
|
36
134
|
*
|
|
37
|
-
* This is
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* 4. Emits contract artifacts (JSON and DTS)
|
|
42
|
-
* 5. Writes files to the paths specified in config
|
|
135
|
+
* This is the SINGLE publication path used by both the CLI command
|
|
136
|
+
* (`prisma-next contract emit`) and the Vite plugin
|
|
137
|
+
* (`@prisma-next/vite-plugin-contract-emit`). New callers must go through this
|
|
138
|
+
* function rather than re-implementing load → emit → publish.
|
|
43
139
|
*
|
|
44
|
-
*
|
|
140
|
+
* The whole flow (load config → resolve source → emit bytes → atomic publish)
|
|
141
|
+
* is serialized per output JSON path via `queueEmitByOutput`. Concurrent calls
|
|
142
|
+
* for the same output line up FIFO; the user-visible outcome is "last
|
|
143
|
+
* submission wins on disk" without any supersession bookkeeping. Within a
|
|
144
|
+
* single emit, `publishContractArtifactPair` stages temp files, renames
|
|
145
|
+
* `contract.d.ts` before `contract.json`, and attempts to restore the previous
|
|
146
|
+
* pair if either rename fails — so type-only consumers never observe a
|
|
147
|
+
* mismatched pair.
|
|
45
148
|
*
|
|
46
|
-
* @
|
|
47
|
-
* @
|
|
48
|
-
* @throws If config loading fails, contract is invalid, or file I/O fails
|
|
49
|
-
* @throws signal.reason if cancelled via AbortSignal (typically DOMException with name 'AbortError')
|
|
149
|
+
* @throws {CliStructuredError} on config/source/validation problems
|
|
150
|
+
* @throws {DOMException} `AbortError` if cancelled via `signal`
|
|
50
151
|
*/
|
|
51
152
|
export async function executeContractEmit(
|
|
52
153
|
options: ContractEmitOptions,
|
|
53
154
|
): Promise<ContractEmitResult> {
|
|
54
|
-
const { configPath, signal = new AbortController().signal } = options;
|
|
155
|
+
const { configPath, signal = new AbortController().signal, onProgress } = options;
|
|
55
156
|
const unlessAborted = abortable(signal);
|
|
56
157
|
|
|
57
|
-
// Load config using the existing config loader
|
|
58
158
|
const config = await unlessAborted(loadConfig(configPath));
|
|
59
159
|
|
|
60
|
-
// Validate contract config is present
|
|
61
160
|
if (!config.contract) {
|
|
62
161
|
throw errorContractConfigMissing({
|
|
63
162
|
why: 'Config.contract is required for emit. Define it in your config: contract: { source: ..., output: ... }',
|
|
@@ -66,14 +165,12 @@ export async function executeContractEmit(
|
|
|
66
165
|
|
|
67
166
|
const contractConfig = config.contract;
|
|
68
167
|
|
|
69
|
-
// Validate output path is present and ends with .json
|
|
70
168
|
if (!contractConfig.output) {
|
|
71
169
|
throw errorContractConfigMissing({
|
|
72
170
|
why: 'Contract config must have output path. This should not happen if defineConfig() was used.',
|
|
73
171
|
});
|
|
74
172
|
}
|
|
75
173
|
|
|
76
|
-
// Validate source exists and is callable
|
|
77
174
|
if (typeof contractConfig.source?.load !== 'function') {
|
|
78
175
|
throw errorContractConfigMissing({
|
|
79
176
|
why: 'Contract config must include a valid source provider object',
|
|
@@ -90,98 +187,84 @@ export async function executeContractEmit(
|
|
|
90
187
|
}
|
|
91
188
|
const { jsonPath: outputJsonPath, dtsPath: outputDtsPath } = outputPaths;
|
|
92
189
|
|
|
93
|
-
|
|
190
|
+
return queueEmitByOutput(outputJsonPath, async () => {
|
|
191
|
+
const stack = createControlStack(config);
|
|
94
192
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
193
|
+
const sourceContext = {
|
|
194
|
+
composedExtensionPacks: stack.extensionPacks.map((p) => p.id),
|
|
195
|
+
scalarTypeDescriptors: stack.scalarTypeDescriptors,
|
|
196
|
+
authoringContributions: stack.authoringContributions,
|
|
197
|
+
codecLookup: stack.codecLookup,
|
|
198
|
+
controlMutationDefaults: stack.controlMutationDefaults,
|
|
199
|
+
resolvedInputs: contractConfig.source.inputs ?? [],
|
|
200
|
+
};
|
|
103
201
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
202
|
+
startSpan(onProgress, 'resolveSource', 'Resolving contract source...');
|
|
203
|
+
let providerResult: Awaited<ReturnType<typeof contractConfig.source.load>>;
|
|
204
|
+
try {
|
|
205
|
+
providerResult = await unlessAborted(contractConfig.source.load(sourceContext));
|
|
206
|
+
} catch (error) {
|
|
207
|
+
endSpan(onProgress, 'resolveSource', 'error');
|
|
208
|
+
if (signal.aborted || (isRecord(error) && error['name'] === 'AbortError')) {
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
throw failedToResolveContractSource(
|
|
212
|
+
error instanceof Error ? error.message : String(error),
|
|
213
|
+
'Ensure contract.source.load resolves to ok(Contract) or returns structured diagnostics.',
|
|
214
|
+
);
|
|
110
215
|
}
|
|
111
|
-
throw errorRuntime('Failed to resolve contract source', {
|
|
112
|
-
why: error instanceof Error ? error.message : String(error),
|
|
113
|
-
fix: 'Ensure contract.source.load resolves to ok(Contract) or returns structured diagnostics.',
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
216
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (providerResult.ok && !('value' in providerResult)) {
|
|
125
|
-
throw errorRuntime('Failed to resolve contract source', {
|
|
126
|
-
why: 'Contract source provider returned malformed success result: missing value.',
|
|
127
|
-
fix: 'Ensure contract.source.load success payload is ok(Contract).',
|
|
128
|
-
});
|
|
129
|
-
}
|
|
217
|
+
const validatedContract = validateProviderResult(providerResult);
|
|
218
|
+
if (!validatedContract.ok) {
|
|
219
|
+
endSpan(onProgress, 'resolveSource', 'error');
|
|
220
|
+
throw validatedContract.error;
|
|
221
|
+
}
|
|
222
|
+
endSpan(onProgress, 'resolveSource', 'ok');
|
|
130
223
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
224
|
+
startSpan(onProgress, 'emit', 'Emitting contract...');
|
|
225
|
+
let emitResult: Awaited<ReturnType<typeof emit>>;
|
|
226
|
+
try {
|
|
227
|
+
const familyInstance = config.family.create(stack);
|
|
228
|
+
const rawComponents = [config.target, config.adapter, ...(config.extensionPacks ?? [])];
|
|
229
|
+
const frameworkComponents = assertFrameworkComponentsCompatible(
|
|
230
|
+
config.family.familyId,
|
|
231
|
+
config.target.targetId,
|
|
232
|
+
rawComponents,
|
|
233
|
+
);
|
|
234
|
+
const enrichedIR = enrichContract(validatedContract.value as Contract, frameworkComponents);
|
|
235
|
+
familyInstance.validateContract(enrichedIR);
|
|
236
|
+
emitResult = await unlessAborted(
|
|
237
|
+
emit(enrichedIR, stack, config.family.emission, { outputJsonPath }),
|
|
238
|
+
);
|
|
239
|
+
} catch (error) {
|
|
240
|
+
endSpan(onProgress, 'emit', 'error');
|
|
241
|
+
throw error;
|
|
242
|
+
}
|
|
243
|
+
endSpan(onProgress, 'emit', 'ok');
|
|
137
244
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
},
|
|
245
|
+
await unlessAborted(mkdir(dirname(outputJsonPath), { recursive: true }));
|
|
246
|
+
await publishContractArtifactPair({
|
|
247
|
+
outputJsonPath,
|
|
248
|
+
outputDtsPath,
|
|
249
|
+
contractJson: emitResult.contractJson,
|
|
250
|
+
contractDts: emitResult.contractDts,
|
|
251
|
+
publicationToken: String(process.hrtime.bigint()),
|
|
146
252
|
});
|
|
147
|
-
}
|
|
148
253
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
config.family.familyId,
|
|
154
|
-
config.target.targetId,
|
|
155
|
-
rawComponents,
|
|
156
|
-
);
|
|
157
|
-
const enrichedIR = enrichContract(providerResult.value as Contract, frameworkComponents);
|
|
158
|
-
|
|
159
|
-
familyInstance.validateContract(enrichedIR);
|
|
160
|
-
const emitResult = await unlessAborted(
|
|
161
|
-
emit(enrichedIR, stack, config.family.emission, {
|
|
162
|
-
outputJsonPath: outputJsonPath,
|
|
163
|
-
}),
|
|
164
|
-
);
|
|
165
|
-
|
|
166
|
-
// Create directory if needed and write files (both colocated)
|
|
167
|
-
await unlessAborted(mkdir(dirname(outputJsonPath), { recursive: true }));
|
|
168
|
-
await unlessAborted(writeFile(outputJsonPath, emitResult.contractJson, 'utf-8'));
|
|
169
|
-
await unlessAborted(writeFile(outputDtsPath, emitResult.contractDts, 'utf-8'));
|
|
170
|
-
|
|
171
|
-
// Validate that contract.d.ts type imports are resolvable
|
|
172
|
-
const { validateContractDeps } = await import('../../utils/validate-contract-deps');
|
|
173
|
-
const depsValidation = validateContractDeps(emitResult.contractDts, dirname(outputDtsPath));
|
|
174
|
-
if (depsValidation.warning) {
|
|
175
|
-
process.stderr.write(`\n⚠ ${depsValidation.warning}\n`);
|
|
176
|
-
}
|
|
254
|
+
const validationWarning = validateContractDeps(
|
|
255
|
+
emitResult.contractDts,
|
|
256
|
+
dirname(outputDtsPath),
|
|
257
|
+
).warning;
|
|
177
258
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
259
|
+
return {
|
|
260
|
+
storageHash: emitResult.storageHash,
|
|
261
|
+
...ifDefined('executionHash', emitResult.executionHash),
|
|
262
|
+
profileHash: emitResult.profileHash,
|
|
263
|
+
files: {
|
|
264
|
+
json: outputJsonPath,
|
|
265
|
+
dts: outputDtsPath,
|
|
266
|
+
},
|
|
267
|
+
...ifDefined('validationWarning', validationWarning),
|
|
268
|
+
};
|
|
269
|
+
});
|
|
187
270
|
}
|
|
@@ -30,6 +30,21 @@ export interface ExecuteMigrationApplyOptions<TFamilyId extends string, TTargetI
|
|
|
30
30
|
readonly onProgress?: OnControlProgress;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Apply a sequence of migration packages against the configured driver.
|
|
35
|
+
*
|
|
36
|
+
* Validates the path's continuity (origin → ... → destination, no gaps),
|
|
37
|
+
* then drives the family/target's migration runner over each package's
|
|
38
|
+
* operations in order, surfacing per-migration progress through `onProgress`.
|
|
39
|
+
*
|
|
40
|
+
* The `pendingMigrations` parameter is trusted input. Callers are responsible
|
|
41
|
+
* for upstream verification of the originating migration packages — typically
|
|
42
|
+
* by loading them via `readMigrationPackage` from
|
|
43
|
+
* `@prisma-next/migration-tools/io`, which performs hash-integrity checks at
|
|
44
|
+
* the load boundary. This operation does not re-verify the packages and
|
|
45
|
+
* assumes the `(metadata, ops)` pairs on disk have not been tampered with
|
|
46
|
+
* since emit.
|
|
47
|
+
*/
|
|
33
48
|
export async function executeMigrationApply<TFamilyId extends string, TTargetId extends string>(
|
|
34
49
|
options: ExecuteMigrationApplyOptions<TFamilyId, TTargetId>,
|
|
35
50
|
): Promise<MigrationApplyResult> {
|
package/src/control-api/types.ts
CHANGED
|
@@ -512,18 +512,31 @@ export type MigrationApplyResult = Result<MigrationApplySuccess, MigrationApplyF
|
|
|
512
512
|
|
|
513
513
|
/**
|
|
514
514
|
* Options for the standalone executeContractEmit function.
|
|
515
|
-
*
|
|
516
|
-
*
|
|
515
|
+
*
|
|
516
|
+
* `executeContractEmit` is the canonical publication path for both the
|
|
517
|
+
* `prisma-next contract emit` CLI command and the `@prisma-next/vite-plugin-contract-emit`
|
|
518
|
+
* Vite plugin. Do not duplicate the load → emit → publish dance elsewhere; if a
|
|
519
|
+
* caller needs additional behavior, extend this options shape and update the
|
|
520
|
+
* single implementation rather than building a parallel publication path.
|
|
521
|
+
*
|
|
522
|
+
* Concurrent calls for the same output JSON path are serialized per-output via
|
|
523
|
+
* a FIFO queue; concurrent calls for distinct outputs run in parallel.
|
|
517
524
|
*/
|
|
518
525
|
export interface ContractEmitOptions {
|
|
519
526
|
/** Path to the prisma-next.config.ts file */
|
|
520
527
|
readonly configPath: string;
|
|
521
|
-
/** Optional AbortSignal for
|
|
528
|
+
/** Optional AbortSignal for cancelling the in-flight emit */
|
|
522
529
|
readonly signal?: AbortSignal;
|
|
530
|
+
/** Optional progress callback for observing source-resolution and emit spans */
|
|
531
|
+
readonly onProgress?: OnControlProgress;
|
|
523
532
|
}
|
|
524
533
|
|
|
525
534
|
/**
|
|
526
535
|
* Result from the standalone executeContractEmit function.
|
|
536
|
+
*
|
|
537
|
+
* Always describes the bytes that were just published to disk. Failures throw
|
|
538
|
+
* (config / source-resolution / emit / publish) — callers do not need to
|
|
539
|
+
* branch on a result discriminator.
|
|
527
540
|
*/
|
|
528
541
|
export interface ContractEmitResult {
|
|
529
542
|
/** Hash of the storage contract (schema-level) */
|
|
@@ -539,6 +552,12 @@ export interface ContractEmitResult {
|
|
|
539
552
|
/** Path to the emitted contract.d.ts file */
|
|
540
553
|
readonly dts: string;
|
|
541
554
|
};
|
|
555
|
+
/**
|
|
556
|
+
* Warning surfaced by `validateContractDeps` after a successful publication.
|
|
557
|
+
* Callers (CLI, Vite plugin) decide how to render this; the operation does
|
|
558
|
+
* not write to stderr itself. Undefined when no warning was raised.
|
|
559
|
+
*/
|
|
560
|
+
readonly validationWarning?: string;
|
|
542
561
|
}
|
|
543
562
|
|
|
544
563
|
// ============================================================================
|
|
@@ -23,7 +23,6 @@ export { enrichContract } from '../control-api/contract-enrichment';
|
|
|
23
23
|
|
|
24
24
|
// Standalone operations (for tooling that doesn't need full client)
|
|
25
25
|
export { executeContractEmit } from '../control-api/operations/contract-emit';
|
|
26
|
-
|
|
27
26
|
// CLI-specific types
|
|
28
27
|
export type {
|
|
29
28
|
ContractEmitOptions,
|
|
@@ -54,3 +53,5 @@ export type {
|
|
|
54
53
|
SignOptions,
|
|
55
54
|
VerifyOptions,
|
|
56
55
|
} from '../control-api/types';
|
|
56
|
+
// Lifecycle helpers for hosts that publish to many output paths
|
|
57
|
+
export { disposeEmitQueue } from '../utils/emit-queue';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public re-export of the `init --json` success-document schema (FR1.5).
|
|
3
|
+
*
|
|
4
|
+
* Imported as `@prisma-next/cli/init-output`. The shared error envelope is
|
|
5
|
+
* exported separately from `@prisma-next/errors`; consumers should branch
|
|
6
|
+
* on the `ok` discriminator (success documents carry `ok: true`, error
|
|
7
|
+
* envelopes carry `ok: false`) per the
|
|
8
|
+
* [Style Guide § JSON Semantics](../../../../../../../docs/CLI%20Style%20Guide.md#json-semantics).
|
|
9
|
+
*/
|
|
10
|
+
export { type InitOutput, InitOutputSchema } from '../commands/init/output';
|
package/src/migration-cli.ts
CHANGED
|
@@ -41,13 +41,13 @@ import { fileURLToPath } from 'node:url';
|
|
|
41
41
|
import { CliStructuredError, errorMigrationCliInvalidConfigArg } from '@prisma-next/errors/control';
|
|
42
42
|
import { errorMigrationTargetMismatch } from '@prisma-next/errors/migration';
|
|
43
43
|
import { createControlStack } from '@prisma-next/framework-components/control';
|
|
44
|
+
import type { MigrationMetadata } from '@prisma-next/migration-tools/metadata';
|
|
44
45
|
import {
|
|
45
46
|
buildMigrationArtifacts,
|
|
46
47
|
isDirectEntrypoint,
|
|
47
48
|
type Migration,
|
|
48
49
|
printMigrationHelp,
|
|
49
50
|
} from '@prisma-next/migration-tools/migration';
|
|
50
|
-
import type { MigrationManifest } from '@prisma-next/migration-tools/types';
|
|
51
51
|
import { dirname, join } from 'pathe';
|
|
52
52
|
import { loadConfig } from './config-loader';
|
|
53
53
|
|
|
@@ -202,16 +202,23 @@ export class MigrationCLI {
|
|
|
202
202
|
* `buildMigrationArtifacts` so the pure builder can preserve fields owned
|
|
203
203
|
* by `migration plan` (contract bookends, hints, labels, `createdAt`)
|
|
204
204
|
* across re-emits.
|
|
205
|
+
*
|
|
206
|
+
* Author-time path: this loader is non-verifying by design. Hash mismatch
|
|
207
|
+
* is the *expected* outcome of a re-author (the developer's source
|
|
208
|
+
* changes invalidate the prior hash by construction), and verification
|
|
209
|
+
* here would block legitimate regenerations. Apply-time consumers always
|
|
210
|
+
* route through the verifying `readMigrationPackage` in
|
|
211
|
+
* `@prisma-next/migration-tools/io` instead.
|
|
205
212
|
*/
|
|
206
|
-
function
|
|
213
|
+
function readExistingMetadata(metadataPath: string): Partial<MigrationMetadata> | null {
|
|
207
214
|
let raw: string;
|
|
208
215
|
try {
|
|
209
|
-
raw = readFileSync(
|
|
216
|
+
raw = readFileSync(metadataPath, 'utf-8');
|
|
210
217
|
} catch {
|
|
211
218
|
return null;
|
|
212
219
|
}
|
|
213
220
|
try {
|
|
214
|
-
return JSON.parse(raw) as Partial<
|
|
221
|
+
return JSON.parse(raw) as Partial<MigrationMetadata>;
|
|
215
222
|
} catch {
|
|
216
223
|
return null;
|
|
217
224
|
}
|
|
@@ -236,19 +243,19 @@ function serializeMigrationToDisk(
|
|
|
236
243
|
migrationDir: string,
|
|
237
244
|
dryRun: boolean,
|
|
238
245
|
): void {
|
|
239
|
-
const
|
|
240
|
-
const existing =
|
|
241
|
-
const { opsJson,
|
|
246
|
+
const metadataPath = join(migrationDir, 'migration.json');
|
|
247
|
+
const existing = readExistingMetadata(metadataPath);
|
|
248
|
+
const { opsJson, metadataJson } = buildMigrationArtifacts(instance, existing);
|
|
242
249
|
|
|
243
250
|
if (dryRun) {
|
|
244
|
-
process.stdout.write(`--- migration.json ---\n${
|
|
251
|
+
process.stdout.write(`--- migration.json ---\n${metadataJson}\n`);
|
|
245
252
|
process.stdout.write('--- ops.json ---\n');
|
|
246
253
|
process.stdout.write(`${opsJson}\n`);
|
|
247
254
|
return;
|
|
248
255
|
}
|
|
249
256
|
|
|
250
257
|
writeFileSync(join(migrationDir, 'ops.json'), opsJson);
|
|
251
|
-
writeFileSync(
|
|
258
|
+
writeFileSync(metadataPath, metadataJson);
|
|
252
259
|
|
|
253
260
|
process.stdout.write(`Wrote ops.json + migration.json to ${migrationDir}\n`);
|
|
254
261
|
}
|
package/src/utils/cli-errors.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
* CLI-specific errors (e.g., Commander.js argument validation) can be added here if needed.
|
|
4
4
|
*/
|
|
5
5
|
export type { CliErrorConflict, CliErrorEnvelope } from '@prisma-next/errors/control';
|
|
6
|
-
|
|
6
|
+
|
|
7
|
+
import {
|
|
7
8
|
CliStructuredError,
|
|
8
9
|
errorConfigFileNotFound,
|
|
9
10
|
errorConfigValidation,
|
|
@@ -20,6 +21,26 @@ export {
|
|
|
20
21
|
errorTargetMigrationNotSupported,
|
|
21
22
|
errorUnexpected,
|
|
22
23
|
} from '@prisma-next/errors/control';
|
|
24
|
+
import { errorRuntime } from '@prisma-next/errors/execution';
|
|
25
|
+
import type { MigrationToolsError } from '@prisma-next/migration-tools/errors';
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
CliStructuredError,
|
|
29
|
+
errorConfigFileNotFound,
|
|
30
|
+
errorConfigValidation,
|
|
31
|
+
errorContractConfigMissing,
|
|
32
|
+
errorContractMissingExtensionPacks,
|
|
33
|
+
errorContractValidationFailed,
|
|
34
|
+
errorDatabaseConnectionRequired,
|
|
35
|
+
errorDriverRequired,
|
|
36
|
+
errorFamilyReadMarkerSqlRequired,
|
|
37
|
+
errorFileNotFound,
|
|
38
|
+
errorMigrationCliInvalidConfigArg,
|
|
39
|
+
errorMigrationPlanningFailed,
|
|
40
|
+
errorQueryRunnerFactoryRequired,
|
|
41
|
+
errorTargetMigrationNotSupported,
|
|
42
|
+
errorUnexpected,
|
|
43
|
+
};
|
|
23
44
|
export {
|
|
24
45
|
ERROR_CODE_DESTRUCTIVE_CHANGES,
|
|
25
46
|
errorDestructiveChanges,
|
|
@@ -38,3 +59,26 @@ export {
|
|
|
38
59
|
errorUnfilledPlaceholder,
|
|
39
60
|
placeholder,
|
|
40
61
|
} from '@prisma-next/errors/migration';
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Maps a `MigrationToolsError` raised by the migration-tools loader/graph
|
|
65
|
+
* surface (`readMigrationPackage`, `readMigrationsDir`, `readRefs`,
|
|
66
|
+
* `resolveRef`, `reconstructGraph`, ...) into a CLI `errorRuntime` envelope.
|
|
67
|
+
*
|
|
68
|
+
* The full `error.details` payload is forwarded into `meta` so machine
|
|
69
|
+
* consumers (`--json`) see structural fields like `dir`, `storedHash`,
|
|
70
|
+
* `computedHash` (for `MIGRATION.HASH_MISMATCH`) alongside the stable
|
|
71
|
+
* `code`. The user-visible `summary`/`why`/`fix` text is unchanged.
|
|
72
|
+
*
|
|
73
|
+
* Callers are expected to gate on `MigrationToolsError.is(error)` first
|
|
74
|
+
* (mirroring the original inline pattern); non-`MigrationToolsError`
|
|
75
|
+
* values are caller-classified (rethrow, wrap with command-specific
|
|
76
|
+
* `errorUnexpected`, etc.).
|
|
77
|
+
*/
|
|
78
|
+
export function mapMigrationToolsError(error: MigrationToolsError): CliStructuredError {
|
|
79
|
+
return errorRuntime(error.message, {
|
|
80
|
+
why: error.why,
|
|
81
|
+
fix: error.fix,
|
|
82
|
+
meta: { code: error.code, ...(error.details ?? {}) },
|
|
83
|
+
});
|
|
84
|
+
}
|