@prisma-next/cli 0.3.0-dev.3 → 0.3.0-dev.30
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 +111 -27
- package/dist/{chunk-BZMBKEEQ.js → chunk-AGOTG4L3.js} +44 -76
- package/dist/chunk-AGOTG4L3.js.map +1 -0
- package/dist/chunk-HLLI4YL7.js +180 -0
- package/dist/chunk-HLLI4YL7.js.map +1 -0
- package/dist/chunk-VG2R7DGF.js +735 -0
- package/dist/chunk-VG2R7DGF.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +1502 -968
- package/dist/cli.js.map +1 -1
- package/dist/commands/contract-emit.d.ts +2 -4
- package/dist/commands/contract-emit.d.ts.map +1 -0
- package/dist/commands/contract-emit.js +3 -2
- package/dist/commands/db-init.d.ts +2 -4
- package/dist/commands/db-init.d.ts.map +1 -0
- package/dist/commands/db-init.js +205 -289
- package/dist/commands/db-init.js.map +1 -1
- package/dist/commands/db-introspect.d.ts +2 -4
- package/dist/commands/db-introspect.d.ts.map +1 -0
- package/dist/commands/db-introspect.js +108 -143
- package/dist/commands/db-introspect.js.map +1 -1
- 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-schema-verify.js +120 -113
- package/dist/commands/db-schema-verify.js.map +1 -1
- package/dist/commands/db-sign.d.ts +2 -4
- package/dist/commands/db-sign.d.ts.map +1 -0
- package/dist/commands/db-sign.js +152 -156
- package/dist/commands/db-sign.js.map +1 -1
- package/dist/commands/db-verify.d.ts +2 -4
- package/dist/commands/db-verify.d.ts.map +1 -0
- package/dist/commands/db-verify.js +142 -122
- package/dist/commands/db-verify.js.map +1 -1
- package/dist/config-loader.d.ts +3 -5
- package/dist/config-loader.d.ts.map +1 -0
- package/dist/control-api/client.d.ts +13 -0
- package/dist/control-api/client.d.ts.map +1 -0
- package/dist/control-api/operations/db-init.d.ts +29 -0
- package/dist/control-api/operations/db-init.d.ts.map +1 -0
- package/dist/control-api/types.d.ts +387 -0
- package/dist/control-api/types.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/control-api.d.ts +13 -0
- package/dist/exports/control-api.d.ts.map +1 -0
- package/dist/exports/control-api.js +7 -0
- package/dist/exports/control-api.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} +4 -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/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 +70 -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/progress-adapter.d.ts +26 -0
- package/dist/utils/progress-adapter.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/package.json +30 -26
- package/src/cli.ts +260 -0
- package/src/commands/contract-emit.ts +259 -0
- package/src/commands/db-init.ts +360 -0
- package/src/commands/db-introspect.ts +227 -0
- package/src/commands/db-schema-verify.ts +238 -0
- package/src/commands/db-sign.ts +279 -0
- package/src/commands/db-verify.ts +258 -0
- package/src/config-loader.ts +76 -0
- package/src/control-api/client.ts +589 -0
- package/src/control-api/operations/db-init.ts +281 -0
- package/src/control-api/types.ts +461 -0
- package/src/exports/config-types.ts +6 -0
- package/src/exports/control-api.ts +46 -0
- package/src/exports/index.ts +4 -0
- package/src/load-ts-contract.ts +217 -0
- package/src/utils/cli-errors.ts +26 -0
- package/src/utils/command-helpers.ts +26 -0
- package/src/utils/framework-components.ts +177 -0
- package/src/utils/global-flags.ts +75 -0
- package/src/utils/output.ts +1471 -0
- package/src/utils/progress-adapter.ts +86 -0
- package/src/utils/result-handler.ts +44 -0
- package/dist/chunk-464LNZCE.js +0 -134
- package/dist/chunk-464LNZCE.js.map +0 -1
- package/dist/chunk-BZMBKEEQ.js.map +0 -1
- package/dist/chunk-ZKYEJROM.js +0 -94
- package/dist/chunk-ZKYEJROM.js.map +0 -1
- 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,1471 @@
|
|
|
1
|
+
import { relative } from 'node:path';
|
|
2
|
+
import { bgGreen, blue, bold, cyan, dim, green, magenta, red, yellow } from 'colorette';
|
|
3
|
+
import type { Command } from 'commander';
|
|
4
|
+
import stringWidth from 'string-width';
|
|
5
|
+
import stripAnsi from 'strip-ansi';
|
|
6
|
+
import wrapAnsi from 'wrap-ansi';
|
|
7
|
+
// EmitContractResult type for CLI output formatting (includes file paths)
|
|
8
|
+
export interface EmitContractResult {
|
|
9
|
+
readonly coreHash: string;
|
|
10
|
+
readonly profileHash: string;
|
|
11
|
+
readonly outDir: string;
|
|
12
|
+
readonly files: {
|
|
13
|
+
readonly json: string;
|
|
14
|
+
readonly dts: string;
|
|
15
|
+
};
|
|
16
|
+
readonly timings: {
|
|
17
|
+
readonly total: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
import type { CoreSchemaView, SchemaTreeNode } from '@prisma-next/core-control-plane/schema-view';
|
|
22
|
+
import type {
|
|
23
|
+
IntrospectSchemaResult,
|
|
24
|
+
SchemaVerificationNode,
|
|
25
|
+
SignDatabaseResult,
|
|
26
|
+
VerifyDatabaseResult,
|
|
27
|
+
VerifyDatabaseSchemaResult,
|
|
28
|
+
} from '@prisma-next/core-control-plane/types';
|
|
29
|
+
import type { CliErrorConflict, CliErrorEnvelope } from './cli-errors';
|
|
30
|
+
import { getLongDescription } from './command-helpers';
|
|
31
|
+
import type { GlobalFlags } from './global-flags';
|
|
32
|
+
|
|
33
|
+
// ============================================================================
|
|
34
|
+
// Helper Functions
|
|
35
|
+
// ============================================================================
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Formats a timestamp for output.
|
|
39
|
+
*/
|
|
40
|
+
function formatTimestamp(): string {
|
|
41
|
+
return new Date().toISOString();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates a prefix string if timestamps are enabled.
|
|
46
|
+
*/
|
|
47
|
+
function createPrefix(flags: GlobalFlags): string {
|
|
48
|
+
return flags.timestamps ? `[${formatTimestamp()}] ` : '';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Checks if verbose output is enabled at the specified level.
|
|
53
|
+
*/
|
|
54
|
+
function isVerbose(flags: GlobalFlags, level: 1 | 2): boolean {
|
|
55
|
+
return (flags.verbose ?? 0) >= level;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Creates a color-aware formatter function.
|
|
60
|
+
* Returns a function that applies the color only if colors are enabled.
|
|
61
|
+
*/
|
|
62
|
+
function createColorFormatter<T extends (text: string) => string>(
|
|
63
|
+
useColor: boolean,
|
|
64
|
+
colorFn: T,
|
|
65
|
+
): (text: string) => string {
|
|
66
|
+
return useColor ? colorFn : (text: string) => text;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Formats text with dim styling if colors are enabled.
|
|
71
|
+
*/
|
|
72
|
+
function formatDim(useColor: boolean, text: string): string {
|
|
73
|
+
return useColor ? dim(text) : text;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ============================================================================
|
|
77
|
+
// Emit Output Formatters
|
|
78
|
+
// ============================================================================
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Formats human-readable output for contract emit.
|
|
82
|
+
*/
|
|
83
|
+
export function formatEmitOutput(result: EmitContractResult, flags: GlobalFlags): string {
|
|
84
|
+
if (flags.quiet) {
|
|
85
|
+
return '';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const lines: string[] = [];
|
|
89
|
+
const prefix = createPrefix(flags);
|
|
90
|
+
|
|
91
|
+
// Convert absolute paths to relative paths from cwd
|
|
92
|
+
const jsonPath = relative(process.cwd(), result.files.json);
|
|
93
|
+
const dtsPath = relative(process.cwd(), result.files.dts);
|
|
94
|
+
|
|
95
|
+
lines.push(`${prefix}✔ Emitted contract.json → ${jsonPath}`);
|
|
96
|
+
lines.push(`${prefix}✔ Emitted contract.d.ts → ${dtsPath}`);
|
|
97
|
+
lines.push(`${prefix} coreHash: ${result.coreHash}`);
|
|
98
|
+
if (result.profileHash) {
|
|
99
|
+
lines.push(`${prefix} profileHash: ${result.profileHash}`);
|
|
100
|
+
}
|
|
101
|
+
if (isVerbose(flags, 1)) {
|
|
102
|
+
lines.push(`${prefix} Total time: ${result.timings.total}ms`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return lines.join('\n');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Formats JSON output for contract emit.
|
|
110
|
+
*/
|
|
111
|
+
export function formatEmitJson(result: EmitContractResult): string {
|
|
112
|
+
const output = {
|
|
113
|
+
ok: true,
|
|
114
|
+
coreHash: result.coreHash,
|
|
115
|
+
...(result.profileHash ? { profileHash: result.profileHash } : {}),
|
|
116
|
+
outDir: result.outDir,
|
|
117
|
+
files: result.files,
|
|
118
|
+
timings: result.timings,
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
return JSON.stringify(output, null, 2);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ============================================================================
|
|
125
|
+
// Error Output Formatters
|
|
126
|
+
// ============================================================================
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Formats error output for human-readable display.
|
|
130
|
+
*/
|
|
131
|
+
export function formatErrorOutput(error: CliErrorEnvelope, flags: GlobalFlags): string {
|
|
132
|
+
const lines: string[] = [];
|
|
133
|
+
const prefix = createPrefix(flags);
|
|
134
|
+
const useColor = flags.color !== false;
|
|
135
|
+
const formatRed = createColorFormatter(useColor, red);
|
|
136
|
+
const formatDimText = (text: string) => formatDim(useColor, text);
|
|
137
|
+
|
|
138
|
+
lines.push(`${prefix}${formatRed('✖')} ${error.summary} (${error.code})`);
|
|
139
|
+
|
|
140
|
+
if (error.why) {
|
|
141
|
+
lines.push(`${prefix}${formatDimText(` Why: ${error.why}`)}`);
|
|
142
|
+
}
|
|
143
|
+
if (error.fix) {
|
|
144
|
+
lines.push(`${prefix}${formatDimText(` Fix: ${error.fix}`)}`);
|
|
145
|
+
}
|
|
146
|
+
if (error.where?.path) {
|
|
147
|
+
const whereLine = error.where.line
|
|
148
|
+
? `${error.where.path}:${error.where.line}`
|
|
149
|
+
: error.where.path;
|
|
150
|
+
lines.push(`${prefix}${formatDimText(` Where: ${whereLine}`)}`);
|
|
151
|
+
}
|
|
152
|
+
// Show conflicts list if present (always show a short list; show full list when verbose)
|
|
153
|
+
if (error.meta?.['conflicts']) {
|
|
154
|
+
const conflicts = error.meta['conflicts'] as readonly CliErrorConflict[];
|
|
155
|
+
if (conflicts.length > 0) {
|
|
156
|
+
const maxToShow = isVerbose(flags, 1) ? conflicts.length : Math.min(3, conflicts.length);
|
|
157
|
+
const header = isVerbose(flags, 1)
|
|
158
|
+
? ' Conflicts:'
|
|
159
|
+
: ` Conflicts (showing ${maxToShow} of ${conflicts.length}):`;
|
|
160
|
+
lines.push(`${prefix}${formatDimText(header)}`);
|
|
161
|
+
for (const conflict of conflicts.slice(0, maxToShow)) {
|
|
162
|
+
lines.push(`${prefix}${formatDimText(` - [${conflict.kind}] ${conflict.summary}`)}`);
|
|
163
|
+
}
|
|
164
|
+
if (!isVerbose(flags, 1) && conflicts.length > maxToShow) {
|
|
165
|
+
lines.push(`${prefix}${formatDimText(' Re-run with -v/--verbose to see all conflicts')}`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// Show issues list if present (always show a short list; show full list when verbose)
|
|
170
|
+
if (error.meta?.['issues']) {
|
|
171
|
+
const issues = error.meta['issues'] as readonly { kind?: string; message?: string }[];
|
|
172
|
+
if (issues.length > 0) {
|
|
173
|
+
const maxToShow = isVerbose(flags, 1) ? issues.length : Math.min(3, issues.length);
|
|
174
|
+
const header = isVerbose(flags, 1)
|
|
175
|
+
? ' Issues:'
|
|
176
|
+
: ` Issues (showing ${maxToShow} of ${issues.length}):`;
|
|
177
|
+
lines.push(`${prefix}${formatDimText(header)}`);
|
|
178
|
+
for (const issue of issues.slice(0, maxToShow)) {
|
|
179
|
+
const kind = issue.kind ?? 'issue';
|
|
180
|
+
const message = issue.message ?? '';
|
|
181
|
+
lines.push(`${prefix}${formatDimText(` - [${kind}] ${message}`)}`);
|
|
182
|
+
}
|
|
183
|
+
if (!isVerbose(flags, 1) && issues.length > maxToShow) {
|
|
184
|
+
lines.push(`${prefix}${formatDimText(' Re-run with -v/--verbose to see all issues')}`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (error.docsUrl && isVerbose(flags, 1)) {
|
|
189
|
+
lines.push(formatDimText(error.docsUrl));
|
|
190
|
+
}
|
|
191
|
+
if (isVerbose(flags, 2) && error.meta) {
|
|
192
|
+
lines.push(`${prefix}${formatDimText(` Meta: ${JSON.stringify(error.meta, null, 2)}`)}`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return lines.join('\n');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Formats error output as JSON.
|
|
200
|
+
*/
|
|
201
|
+
export function formatErrorJson(error: CliErrorEnvelope): string {
|
|
202
|
+
return JSON.stringify(error, null, 2);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ============================================================================
|
|
206
|
+
// Verify Output Formatters
|
|
207
|
+
// ============================================================================
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Formats human-readable output for database verify.
|
|
211
|
+
*/
|
|
212
|
+
export function formatVerifyOutput(result: VerifyDatabaseResult, flags: GlobalFlags): string {
|
|
213
|
+
if (flags.quiet) {
|
|
214
|
+
return '';
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const lines: string[] = [];
|
|
218
|
+
const prefix = createPrefix(flags);
|
|
219
|
+
const useColor = flags.color !== false;
|
|
220
|
+
const formatGreen = createColorFormatter(useColor, green);
|
|
221
|
+
const formatRed = createColorFormatter(useColor, red);
|
|
222
|
+
const formatDimText = (text: string) => formatDim(useColor, text);
|
|
223
|
+
|
|
224
|
+
if (result.ok) {
|
|
225
|
+
lines.push(`${prefix}${formatGreen('✔')} ${result.summary}`);
|
|
226
|
+
lines.push(`${prefix}${formatDimText(` coreHash: ${result.contract.coreHash}`)}`);
|
|
227
|
+
if (result.contract.profileHash) {
|
|
228
|
+
lines.push(`${prefix}${formatDimText(` profileHash: ${result.contract.profileHash}`)}`);
|
|
229
|
+
}
|
|
230
|
+
} else {
|
|
231
|
+
lines.push(`${prefix}${formatRed('✖')} ${result.summary} (${result.code})`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (isVerbose(flags, 1)) {
|
|
235
|
+
if (result.codecCoverageSkipped) {
|
|
236
|
+
lines.push(
|
|
237
|
+
`${prefix}${formatDimText(' Codec coverage check skipped (helper returned no supported types)')}`,
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
lines.push(`${prefix}${formatDimText(` Total time: ${result.timings.total}ms`)}`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return lines.join('\n');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Formats JSON output for database verify.
|
|
248
|
+
*/
|
|
249
|
+
export function formatVerifyJson(result: VerifyDatabaseResult): string {
|
|
250
|
+
const output = {
|
|
251
|
+
ok: result.ok,
|
|
252
|
+
...(result.code ? { code: result.code } : {}),
|
|
253
|
+
summary: result.summary,
|
|
254
|
+
contract: result.contract,
|
|
255
|
+
...(result.marker ? { marker: result.marker } : {}),
|
|
256
|
+
target: result.target,
|
|
257
|
+
...(result.missingCodecs ? { missingCodecs: result.missingCodecs } : {}),
|
|
258
|
+
...(result.meta ? { meta: result.meta } : {}),
|
|
259
|
+
timings: result.timings,
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
return JSON.stringify(output, null, 2);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Formats JSON output for database introspection.
|
|
267
|
+
*/
|
|
268
|
+
export function formatIntrospectJson(result: IntrospectSchemaResult<unknown>): string {
|
|
269
|
+
return JSON.stringify(result, null, 2);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Renders a schema tree structure from CoreSchemaView.
|
|
274
|
+
* Matches the style of renderSchemaVerificationTree for consistency.
|
|
275
|
+
*/
|
|
276
|
+
function renderSchemaTree(
|
|
277
|
+
node: SchemaTreeNode,
|
|
278
|
+
flags: GlobalFlags,
|
|
279
|
+
options: {
|
|
280
|
+
readonly isLast: boolean;
|
|
281
|
+
readonly prefix: string;
|
|
282
|
+
readonly useColor: boolean;
|
|
283
|
+
readonly formatDimText: (text: string) => string;
|
|
284
|
+
readonly isRoot?: boolean;
|
|
285
|
+
},
|
|
286
|
+
): string[] {
|
|
287
|
+
const { isLast, prefix, useColor, formatDimText, isRoot = false } = options;
|
|
288
|
+
const lines: string[] = [];
|
|
289
|
+
|
|
290
|
+
// Format node label with color based on kind (matching schema-verify style)
|
|
291
|
+
let formattedLabel: string = node.label;
|
|
292
|
+
|
|
293
|
+
if (useColor) {
|
|
294
|
+
switch (node.kind) {
|
|
295
|
+
case 'root':
|
|
296
|
+
formattedLabel = bold(node.label);
|
|
297
|
+
break;
|
|
298
|
+
case 'entity': {
|
|
299
|
+
// Parse "table tableName" format - color "table" dim, tableName cyan
|
|
300
|
+
const tableMatch = node.label.match(/^table\s+(.+)$/);
|
|
301
|
+
if (tableMatch?.[1]) {
|
|
302
|
+
const tableName = tableMatch[1];
|
|
303
|
+
formattedLabel = `${dim('table')} ${cyan(tableName)}`;
|
|
304
|
+
} else {
|
|
305
|
+
// Fallback: color entire label with cyan
|
|
306
|
+
formattedLabel = cyan(node.label);
|
|
307
|
+
}
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
case 'collection': {
|
|
311
|
+
// "columns" grouping node - dim the label
|
|
312
|
+
formattedLabel = dim(node.label);
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
case 'field': {
|
|
316
|
+
// Parse column name format: "columnName: typeDisplay (nullability)"
|
|
317
|
+
// Color code: column name (cyan), type (default), nullability (dim)
|
|
318
|
+
const columnMatch = node.label.match(/^([^:]+):\s*(.+)$/);
|
|
319
|
+
if (columnMatch?.[1] && columnMatch[2]) {
|
|
320
|
+
const columnName = columnMatch[1];
|
|
321
|
+
const rest = columnMatch[2];
|
|
322
|
+
// Parse rest: "typeDisplay (nullability)"
|
|
323
|
+
const typeMatch = rest.match(/^([^\s(]+)\s*(\([^)]+\))$/);
|
|
324
|
+
if (typeMatch?.[1] && typeMatch[2]) {
|
|
325
|
+
const typeDisplay = typeMatch[1];
|
|
326
|
+
const nullability = typeMatch[2];
|
|
327
|
+
formattedLabel = `${cyan(columnName)}: ${typeDisplay} ${dim(nullability)}`;
|
|
328
|
+
} else {
|
|
329
|
+
// Fallback if format doesn't match
|
|
330
|
+
formattedLabel = `${cyan(columnName)}: ${rest}`;
|
|
331
|
+
}
|
|
332
|
+
} else {
|
|
333
|
+
formattedLabel = node.label;
|
|
334
|
+
}
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
case 'index': {
|
|
338
|
+
// Parse index/unique constraint/primary key formats
|
|
339
|
+
// "primary key: columnName" -> dim "primary key", cyan columnName
|
|
340
|
+
const pkMatch = node.label.match(/^primary key:\s*(.+)$/);
|
|
341
|
+
if (pkMatch?.[1]) {
|
|
342
|
+
const columnNames = pkMatch[1];
|
|
343
|
+
formattedLabel = `${dim('primary key')}: ${cyan(columnNames)}`;
|
|
344
|
+
} else {
|
|
345
|
+
// "unique name" -> dim "unique", cyan "name"
|
|
346
|
+
const uniqueMatch = node.label.match(/^unique\s+(.+)$/);
|
|
347
|
+
if (uniqueMatch?.[1]) {
|
|
348
|
+
const name = uniqueMatch[1];
|
|
349
|
+
formattedLabel = `${dim('unique')} ${cyan(name)}`;
|
|
350
|
+
} else {
|
|
351
|
+
// "index name" or "unique index name" -> dim label prefix, cyan name
|
|
352
|
+
const indexMatch = node.label.match(/^(unique\s+)?index\s+(.+)$/);
|
|
353
|
+
if (indexMatch?.[2]) {
|
|
354
|
+
const prefix = indexMatch[1] ? `${dim('unique')} ` : '';
|
|
355
|
+
const name = indexMatch[2];
|
|
356
|
+
formattedLabel = `${prefix}${dim('index')} ${cyan(name)}`;
|
|
357
|
+
} else {
|
|
358
|
+
formattedLabel = dim(node.label);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
case 'extension': {
|
|
365
|
+
// Parse extension message formats similar to schema-verify
|
|
366
|
+
// "extensionName extension is enabled" -> cyan extensionName, dim rest
|
|
367
|
+
const extMatch = node.label.match(/^([^\s]+)\s+(extension is enabled)$/);
|
|
368
|
+
if (extMatch?.[1] && extMatch[2]) {
|
|
369
|
+
const extName = extMatch[1];
|
|
370
|
+
const rest = extMatch[2];
|
|
371
|
+
formattedLabel = `${cyan(extName)} ${dim(rest)}`;
|
|
372
|
+
} else {
|
|
373
|
+
// Fallback: color entire label with magenta
|
|
374
|
+
formattedLabel = magenta(node.label);
|
|
375
|
+
}
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
default:
|
|
379
|
+
formattedLabel = node.label;
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// Root node renders without tree characters or │ prefix
|
|
385
|
+
if (isRoot) {
|
|
386
|
+
lines.push(formattedLabel);
|
|
387
|
+
} else {
|
|
388
|
+
// Determine tree character for this node
|
|
389
|
+
const treeChar = isLast ? '└' : '├';
|
|
390
|
+
const treePrefix = `${prefix}${formatDimText(treeChar)}─ `;
|
|
391
|
+
// Root's direct children don't have │ prefix, other nodes do
|
|
392
|
+
// But if prefix already contains │ (for nested children), don't add another
|
|
393
|
+
const isRootChild = prefix === '';
|
|
394
|
+
// Check if prefix already contains │ (strip ANSI codes for comparison)
|
|
395
|
+
const prefixWithoutAnsi = stripAnsi(prefix);
|
|
396
|
+
const prefixHasVerticalBar = prefixWithoutAnsi.includes('│');
|
|
397
|
+
if (isRootChild) {
|
|
398
|
+
lines.push(`${treePrefix}${formattedLabel}`);
|
|
399
|
+
} else if (prefixHasVerticalBar) {
|
|
400
|
+
// Prefix already has │, so just use treePrefix directly
|
|
401
|
+
lines.push(`${treePrefix}${formattedLabel}`);
|
|
402
|
+
} else {
|
|
403
|
+
lines.push(`${formatDimText('│')} ${treePrefix}${formattedLabel}`);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// Render children if present
|
|
408
|
+
if (node.children && node.children.length > 0) {
|
|
409
|
+
// For root node, children start with no prefix (they'll add their own tree characters)
|
|
410
|
+
// For other nodes, calculate child prefix based on whether this is last
|
|
411
|
+
const childPrefix = isRoot ? '' : isLast ? `${prefix} ` : `${prefix}${formatDimText('│')} `;
|
|
412
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
413
|
+
const child = node.children[i];
|
|
414
|
+
if (!child) continue;
|
|
415
|
+
const isLastChild = i === node.children.length - 1;
|
|
416
|
+
const childLines = renderSchemaTree(child, flags, {
|
|
417
|
+
isLast: isLastChild,
|
|
418
|
+
prefix: childPrefix,
|
|
419
|
+
useColor,
|
|
420
|
+
formatDimText,
|
|
421
|
+
isRoot: false,
|
|
422
|
+
});
|
|
423
|
+
lines.push(...childLines);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
return lines;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Formats human-readable output for database introspection.
|
|
432
|
+
*/
|
|
433
|
+
export function formatIntrospectOutput(
|
|
434
|
+
result: IntrospectSchemaResult<unknown>,
|
|
435
|
+
schemaView: CoreSchemaView | undefined,
|
|
436
|
+
flags: GlobalFlags,
|
|
437
|
+
): string {
|
|
438
|
+
if (flags.quiet) {
|
|
439
|
+
return '';
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const lines: string[] = [];
|
|
443
|
+
const prefix = createPrefix(flags);
|
|
444
|
+
const useColor = flags.color !== false;
|
|
445
|
+
const formatDimText = (text: string) => formatDim(useColor, text);
|
|
446
|
+
|
|
447
|
+
if (schemaView) {
|
|
448
|
+
// Render tree structure - root node is special (no tree characters)
|
|
449
|
+
const treeLines = renderSchemaTree(schemaView.root, flags, {
|
|
450
|
+
isLast: true,
|
|
451
|
+
prefix: '',
|
|
452
|
+
useColor,
|
|
453
|
+
formatDimText,
|
|
454
|
+
isRoot: true,
|
|
455
|
+
});
|
|
456
|
+
// Apply prefix (for timestamps) to each tree line
|
|
457
|
+
const prefixedTreeLines = treeLines.map((line) => `${prefix}${line}`);
|
|
458
|
+
lines.push(...prefixedTreeLines);
|
|
459
|
+
} else {
|
|
460
|
+
// Fallback: print summary when toSchemaView is not available
|
|
461
|
+
lines.push(`${prefix}✔ ${result.summary}`);
|
|
462
|
+
if (isVerbose(flags, 1)) {
|
|
463
|
+
lines.push(`${prefix} Target: ${result.target.familyId}/${result.target.id}`);
|
|
464
|
+
if (result.meta?.dbUrl) {
|
|
465
|
+
lines.push(`${prefix} Database: ${result.meta.dbUrl}`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// Add timings in verbose mode
|
|
471
|
+
if (isVerbose(flags, 1)) {
|
|
472
|
+
lines.push(`${prefix}${formatDimText(` Total time: ${result.timings.total}ms`)}`);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
return lines.join('\n');
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Renders a schema verification tree structure from SchemaVerificationNode.
|
|
480
|
+
* Similar to renderSchemaTree but for verification nodes with status-based colors and glyphs.
|
|
481
|
+
*/
|
|
482
|
+
function renderSchemaVerificationTree(
|
|
483
|
+
node: SchemaVerificationNode,
|
|
484
|
+
flags: GlobalFlags,
|
|
485
|
+
options: {
|
|
486
|
+
readonly isLast: boolean;
|
|
487
|
+
readonly prefix: string;
|
|
488
|
+
readonly useColor: boolean;
|
|
489
|
+
readonly formatDimText: (text: string) => string;
|
|
490
|
+
readonly isRoot?: boolean;
|
|
491
|
+
},
|
|
492
|
+
): string[] {
|
|
493
|
+
const { isLast, prefix, useColor, formatDimText, isRoot = false } = options;
|
|
494
|
+
const lines: string[] = [];
|
|
495
|
+
|
|
496
|
+
// Format status glyph and color based on status
|
|
497
|
+
let statusGlyph = '';
|
|
498
|
+
let statusColor: (text: string) => string = (text) => text;
|
|
499
|
+
if (useColor) {
|
|
500
|
+
switch (node.status) {
|
|
501
|
+
case 'pass':
|
|
502
|
+
statusGlyph = '✔';
|
|
503
|
+
statusColor = green;
|
|
504
|
+
break;
|
|
505
|
+
case 'warn':
|
|
506
|
+
statusGlyph = '⚠';
|
|
507
|
+
statusColor = (text) => (useColor ? yellow(text) : text);
|
|
508
|
+
break;
|
|
509
|
+
case 'fail':
|
|
510
|
+
statusGlyph = '✖';
|
|
511
|
+
statusColor = red;
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
} else {
|
|
515
|
+
switch (node.status) {
|
|
516
|
+
case 'pass':
|
|
517
|
+
statusGlyph = '✔';
|
|
518
|
+
break;
|
|
519
|
+
case 'warn':
|
|
520
|
+
statusGlyph = '⚠';
|
|
521
|
+
break;
|
|
522
|
+
case 'fail':
|
|
523
|
+
statusGlyph = '✖';
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// Format node label with color based on kind
|
|
529
|
+
// For column nodes, we need to parse the name to color code different parts
|
|
530
|
+
let labelColor: (text: string) => string = (text) => text;
|
|
531
|
+
let formattedLabel: string = node.name;
|
|
532
|
+
|
|
533
|
+
if (useColor) {
|
|
534
|
+
switch (node.kind) {
|
|
535
|
+
case 'contract':
|
|
536
|
+
case 'schema':
|
|
537
|
+
labelColor = bold;
|
|
538
|
+
formattedLabel = labelColor(node.name);
|
|
539
|
+
break;
|
|
540
|
+
case 'table': {
|
|
541
|
+
// Parse "table tableName" format - color "table" dim, tableName cyan
|
|
542
|
+
const tableMatch = node.name.match(/^table\s+(.+)$/);
|
|
543
|
+
if (tableMatch?.[1]) {
|
|
544
|
+
const tableName = tableMatch[1];
|
|
545
|
+
formattedLabel = `${dim('table')} ${cyan(tableName)}`;
|
|
546
|
+
} else {
|
|
547
|
+
formattedLabel = dim(node.name);
|
|
548
|
+
}
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
case 'columns':
|
|
552
|
+
labelColor = dim;
|
|
553
|
+
formattedLabel = labelColor(node.name);
|
|
554
|
+
break;
|
|
555
|
+
case 'column': {
|
|
556
|
+
// Parse column name format: "columnName: contractType → nativeType (nullability)"
|
|
557
|
+
// Color code: column name (cyan), contract type (default), native type (dim), nullability (dim)
|
|
558
|
+
const columnMatch = node.name.match(/^([^:]+):\s*(.+)$/);
|
|
559
|
+
if (columnMatch?.[1] && columnMatch[2]) {
|
|
560
|
+
const columnName = columnMatch[1];
|
|
561
|
+
const rest = columnMatch[2];
|
|
562
|
+
// Parse rest: "contractType → nativeType (nullability)"
|
|
563
|
+
// Match contract type (can contain /, @, etc.), arrow, native type, then nullability in parentheses
|
|
564
|
+
const typeMatch = rest.match(/^([^\s→]+)\s*→\s*([^\s(]+)\s*(\([^)]+\))$/);
|
|
565
|
+
if (typeMatch?.[1] && typeMatch[2] && typeMatch[3]) {
|
|
566
|
+
const contractType = typeMatch[1];
|
|
567
|
+
const nativeType = typeMatch[2];
|
|
568
|
+
const nullability = typeMatch[3];
|
|
569
|
+
formattedLabel = `${cyan(columnName)}: ${contractType} → ${dim(nativeType)} ${dim(nullability)}`;
|
|
570
|
+
} else {
|
|
571
|
+
// Fallback if format doesn't match (e.g., no native type or no nullability)
|
|
572
|
+
formattedLabel = `${cyan(columnName)}: ${rest}`;
|
|
573
|
+
}
|
|
574
|
+
} else {
|
|
575
|
+
formattedLabel = node.name;
|
|
576
|
+
}
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
case 'type':
|
|
580
|
+
case 'nullability':
|
|
581
|
+
labelColor = (text) => text; // Default color
|
|
582
|
+
formattedLabel = labelColor(node.name);
|
|
583
|
+
break;
|
|
584
|
+
case 'primaryKey': {
|
|
585
|
+
// Parse "primary key: columnName" format - color "primary key" dim, columnName cyan
|
|
586
|
+
const pkMatch = node.name.match(/^primary key:\s*(.+)$/);
|
|
587
|
+
if (pkMatch?.[1]) {
|
|
588
|
+
const columnNames = pkMatch[1];
|
|
589
|
+
formattedLabel = `${dim('primary key')}: ${cyan(columnNames)}`;
|
|
590
|
+
} else {
|
|
591
|
+
formattedLabel = dim(node.name);
|
|
592
|
+
}
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
case 'foreignKey':
|
|
596
|
+
case 'unique':
|
|
597
|
+
case 'index':
|
|
598
|
+
labelColor = dim;
|
|
599
|
+
formattedLabel = labelColor(node.name);
|
|
600
|
+
break;
|
|
601
|
+
case 'extension': {
|
|
602
|
+
// Parse specific extension message formats
|
|
603
|
+
// "database is postgres" -> dim "database is", cyan "postgres"
|
|
604
|
+
const dbMatch = node.name.match(/^database is\s+(.+)$/);
|
|
605
|
+
if (dbMatch?.[1]) {
|
|
606
|
+
const dbName = dbMatch[1];
|
|
607
|
+
formattedLabel = `${dim('database is')} ${cyan(dbName)}`;
|
|
608
|
+
} else {
|
|
609
|
+
// "vector extension is enabled" -> dim everything except extension name
|
|
610
|
+
// Match pattern: "extensionName extension is enabled"
|
|
611
|
+
const extMatch = node.name.match(/^([^\s]+)\s+(extension is enabled)$/);
|
|
612
|
+
if (extMatch?.[1] && extMatch[2]) {
|
|
613
|
+
const extName = extMatch[1];
|
|
614
|
+
const rest = extMatch[2];
|
|
615
|
+
formattedLabel = `${cyan(extName)} ${dim(rest)}`;
|
|
616
|
+
} else {
|
|
617
|
+
// Fallback: color entire name with magenta
|
|
618
|
+
labelColor = magenta;
|
|
619
|
+
formattedLabel = labelColor(node.name);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
break;
|
|
623
|
+
}
|
|
624
|
+
default:
|
|
625
|
+
formattedLabel = node.name;
|
|
626
|
+
break;
|
|
627
|
+
}
|
|
628
|
+
} else {
|
|
629
|
+
formattedLabel = node.name;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
const statusGlyphColored = statusColor(statusGlyph);
|
|
633
|
+
|
|
634
|
+
// Build the label with optional message for failure/warn nodes
|
|
635
|
+
let nodeLabel = formattedLabel;
|
|
636
|
+
if (
|
|
637
|
+
(node.status === 'fail' || node.status === 'warn') &&
|
|
638
|
+
node.message &&
|
|
639
|
+
node.message.length > 0
|
|
640
|
+
) {
|
|
641
|
+
// Always show message for failure/warn nodes - it provides crucial context
|
|
642
|
+
// For parent nodes, the message summarizes child failures
|
|
643
|
+
// For leaf nodes, the message explains the specific issue
|
|
644
|
+
const messageText = formatDimText(`(${node.message})`);
|
|
645
|
+
nodeLabel = `${formattedLabel} ${messageText}`;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// Root node renders without tree characters or │ prefix
|
|
649
|
+
if (isRoot) {
|
|
650
|
+
lines.push(`${statusGlyphColored} ${nodeLabel}`);
|
|
651
|
+
} else {
|
|
652
|
+
// Determine tree character for this node
|
|
653
|
+
const treeChar = isLast ? '└' : '├';
|
|
654
|
+
const treePrefix = `${prefix}${formatDimText(treeChar)}─ `;
|
|
655
|
+
// Root's direct children don't have │ prefix, other nodes do
|
|
656
|
+
const isRootChild = prefix === '';
|
|
657
|
+
// Check if prefix already contains │ (strip ANSI codes for comparison)
|
|
658
|
+
const prefixWithoutAnsi = stripAnsi(prefix);
|
|
659
|
+
const prefixHasVerticalBar = prefixWithoutAnsi.includes('│');
|
|
660
|
+
if (isRootChild) {
|
|
661
|
+
lines.push(`${treePrefix}${statusGlyphColored} ${nodeLabel}`);
|
|
662
|
+
} else if (prefixHasVerticalBar) {
|
|
663
|
+
// Prefix already has │, so just use treePrefix directly
|
|
664
|
+
lines.push(`${treePrefix}${statusGlyphColored} ${nodeLabel}`);
|
|
665
|
+
} else {
|
|
666
|
+
lines.push(`${formatDimText('│')} ${treePrefix}${statusGlyphColored} ${nodeLabel}`);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// Render children if present
|
|
671
|
+
if (node.children && node.children.length > 0) {
|
|
672
|
+
// For root node, children start with no prefix (they'll add their own tree characters)
|
|
673
|
+
// For other nodes, calculate child prefix based on whether this is last
|
|
674
|
+
const childPrefix = isRoot ? '' : isLast ? `${prefix} ` : `${prefix}${formatDimText('│')} `;
|
|
675
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
676
|
+
const child = node.children[i];
|
|
677
|
+
if (!child) continue;
|
|
678
|
+
const isLastChild = i === node.children.length - 1;
|
|
679
|
+
const childLines = renderSchemaVerificationTree(child, flags, {
|
|
680
|
+
isLast: isLastChild,
|
|
681
|
+
prefix: childPrefix,
|
|
682
|
+
useColor,
|
|
683
|
+
formatDimText,
|
|
684
|
+
isRoot: false,
|
|
685
|
+
});
|
|
686
|
+
lines.push(...childLines);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
return lines;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Formats human-readable output for database schema verification.
|
|
695
|
+
*/
|
|
696
|
+
export function formatSchemaVerifyOutput(
|
|
697
|
+
result: VerifyDatabaseSchemaResult,
|
|
698
|
+
flags: GlobalFlags,
|
|
699
|
+
): string {
|
|
700
|
+
if (flags.quiet) {
|
|
701
|
+
return '';
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
const lines: string[] = [];
|
|
705
|
+
const prefix = createPrefix(flags);
|
|
706
|
+
const useColor = flags.color !== false;
|
|
707
|
+
const formatGreen = createColorFormatter(useColor, green);
|
|
708
|
+
const formatRed = createColorFormatter(useColor, red);
|
|
709
|
+
const formatDimText = (text: string) => formatDim(useColor, text);
|
|
710
|
+
|
|
711
|
+
// Render verification tree first
|
|
712
|
+
const treeLines = renderSchemaVerificationTree(result.schema.root, flags, {
|
|
713
|
+
isLast: true,
|
|
714
|
+
prefix: '',
|
|
715
|
+
useColor,
|
|
716
|
+
formatDimText,
|
|
717
|
+
isRoot: true,
|
|
718
|
+
});
|
|
719
|
+
// Apply prefix (for timestamps) to each tree line
|
|
720
|
+
const prefixedTreeLines = treeLines.map((line) => `${prefix}${line}`);
|
|
721
|
+
lines.push(...prefixedTreeLines);
|
|
722
|
+
|
|
723
|
+
// Add counts and timings in verbose mode
|
|
724
|
+
if (isVerbose(flags, 1)) {
|
|
725
|
+
lines.push(`${prefix}${formatDimText(` Total time: ${result.timings.total}ms`)}`);
|
|
726
|
+
lines.push(
|
|
727
|
+
`${prefix}${formatDimText(` pass=${result.schema.counts.pass} warn=${result.schema.counts.warn} fail=${result.schema.counts.fail}`)}`,
|
|
728
|
+
);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// Blank line before summary
|
|
732
|
+
lines.push('');
|
|
733
|
+
|
|
734
|
+
// Summary line at the end: summary with status glyph
|
|
735
|
+
if (result.ok) {
|
|
736
|
+
lines.push(`${prefix}${formatGreen('✔')} ${result.summary}`);
|
|
737
|
+
} else {
|
|
738
|
+
const codeText = result.code ? ` (${result.code})` : '';
|
|
739
|
+
lines.push(`${prefix}${formatRed('✖')} ${result.summary}${codeText}`);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
return lines.join('\n');
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* Formats JSON output for database schema verification.
|
|
747
|
+
*/
|
|
748
|
+
export function formatSchemaVerifyJson(result: VerifyDatabaseSchemaResult): string {
|
|
749
|
+
return JSON.stringify(result, null, 2);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// ============================================================================
|
|
753
|
+
// Sign Output Formatters
|
|
754
|
+
// ============================================================================
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* Formats human-readable output for database sign.
|
|
758
|
+
*/
|
|
759
|
+
export function formatSignOutput(result: SignDatabaseResult, flags: GlobalFlags): string {
|
|
760
|
+
if (flags.quiet) {
|
|
761
|
+
return '';
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
const lines: string[] = [];
|
|
765
|
+
const prefix = createPrefix(flags);
|
|
766
|
+
const useColor = flags.color !== false;
|
|
767
|
+
const formatGreen = createColorFormatter(useColor, green);
|
|
768
|
+
const formatDimText = (text: string) => formatDim(useColor, text);
|
|
769
|
+
|
|
770
|
+
if (result.ok) {
|
|
771
|
+
// Main success message in white (not dimmed)
|
|
772
|
+
lines.push(`${prefix}${formatGreen('✔')} Database signed`);
|
|
773
|
+
|
|
774
|
+
// Show from -> to hashes with clear labels
|
|
775
|
+
const previousHash = result.marker.previous?.coreHash ?? 'none';
|
|
776
|
+
const currentHash = result.contract.coreHash;
|
|
777
|
+
|
|
778
|
+
lines.push(`${prefix}${formatDimText(` from: ${previousHash}`)}`);
|
|
779
|
+
lines.push(`${prefix}${formatDimText(` to: ${currentHash}`)}`);
|
|
780
|
+
|
|
781
|
+
if (isVerbose(flags, 1)) {
|
|
782
|
+
if (result.contract.profileHash) {
|
|
783
|
+
lines.push(`${prefix}${formatDimText(` profileHash: ${result.contract.profileHash}`)}`);
|
|
784
|
+
}
|
|
785
|
+
if (result.marker.previous?.profileHash) {
|
|
786
|
+
lines.push(
|
|
787
|
+
`${prefix}${formatDimText(` previous profileHash: ${result.marker.previous.profileHash}`)}`,
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
lines.push(`${prefix}${formatDimText(` Total time: ${result.timings.total}ms`)}`);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
return lines.join('\n');
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Formats JSON output for database sign.
|
|
799
|
+
*/
|
|
800
|
+
export function formatSignJson(result: SignDatabaseResult): string {
|
|
801
|
+
return JSON.stringify(result, null, 2);
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// ============================================================================
|
|
805
|
+
// DB Init Output Formatters
|
|
806
|
+
// ============================================================================
|
|
807
|
+
|
|
808
|
+
/**
|
|
809
|
+
* Result type for db init command.
|
|
810
|
+
*/
|
|
811
|
+
export interface DbInitResult {
|
|
812
|
+
readonly ok: boolean;
|
|
813
|
+
readonly mode: 'plan' | 'apply';
|
|
814
|
+
readonly plan?: {
|
|
815
|
+
readonly targetId: string;
|
|
816
|
+
readonly destination: {
|
|
817
|
+
readonly coreHash: string;
|
|
818
|
+
readonly profileHash?: string;
|
|
819
|
+
};
|
|
820
|
+
readonly operations: readonly {
|
|
821
|
+
readonly id: string;
|
|
822
|
+
readonly label: string;
|
|
823
|
+
readonly operationClass: string;
|
|
824
|
+
}[];
|
|
825
|
+
};
|
|
826
|
+
readonly execution?: {
|
|
827
|
+
readonly operationsPlanned: number;
|
|
828
|
+
readonly operationsExecuted: number;
|
|
829
|
+
};
|
|
830
|
+
readonly marker?: {
|
|
831
|
+
readonly coreHash: string;
|
|
832
|
+
readonly profileHash?: string;
|
|
833
|
+
};
|
|
834
|
+
readonly summary: string;
|
|
835
|
+
readonly timings: {
|
|
836
|
+
readonly total: number;
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* Formats human-readable output for db init plan mode.
|
|
842
|
+
*/
|
|
843
|
+
export function formatDbInitPlanOutput(result: DbInitResult, flags: GlobalFlags): string {
|
|
844
|
+
if (flags.quiet) {
|
|
845
|
+
return '';
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
const lines: string[] = [];
|
|
849
|
+
const prefix = createPrefix(flags);
|
|
850
|
+
const useColor = flags.color !== false;
|
|
851
|
+
const formatGreen = createColorFormatter(useColor, green);
|
|
852
|
+
const formatDimText = (text: string) => formatDim(useColor, text);
|
|
853
|
+
|
|
854
|
+
// Plan summary
|
|
855
|
+
const operationCount = result.plan?.operations.length ?? 0;
|
|
856
|
+
lines.push(`${prefix}${formatGreen('✔')} Planned ${operationCount} operation(s)`);
|
|
857
|
+
|
|
858
|
+
// Show operations tree
|
|
859
|
+
if (result.plan?.operations && result.plan.operations.length > 0) {
|
|
860
|
+
lines.push(`${prefix}${formatDimText('│')}`);
|
|
861
|
+
for (let i = 0; i < result.plan.operations.length; i++) {
|
|
862
|
+
const op = result.plan.operations[i];
|
|
863
|
+
if (!op) continue;
|
|
864
|
+
const isLast = i === result.plan.operations.length - 1;
|
|
865
|
+
const treeChar = isLast ? '└' : '├';
|
|
866
|
+
const opClass = formatDimText(`[${op.operationClass}]`);
|
|
867
|
+
lines.push(`${prefix}${formatDimText(treeChar)}─ ${op.label} ${opClass}`);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// Destination hash
|
|
872
|
+
if (result.plan?.destination) {
|
|
873
|
+
lines.push(`${prefix}`);
|
|
874
|
+
lines.push(
|
|
875
|
+
`${prefix}${formatDimText(`Destination hash: ${result.plan.destination.coreHash}`)}`,
|
|
876
|
+
);
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// Timings in verbose mode
|
|
880
|
+
if (isVerbose(flags, 1)) {
|
|
881
|
+
lines.push(`${prefix}${formatDimText(`Total time: ${result.timings.total}ms`)}`);
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// Note about dry run
|
|
885
|
+
lines.push(`${prefix}`);
|
|
886
|
+
lines.push(`${prefix}${formatDimText('This is a dry run. No changes were applied.')}`);
|
|
887
|
+
lines.push(`${prefix}${formatDimText('Run without --plan to apply changes.')}`);
|
|
888
|
+
|
|
889
|
+
return lines.join('\n');
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* Formats human-readable output for db init apply mode.
|
|
894
|
+
*/
|
|
895
|
+
export function formatDbInitApplyOutput(result: DbInitResult, flags: GlobalFlags): string {
|
|
896
|
+
if (flags.quiet) {
|
|
897
|
+
return '';
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
const lines: string[] = [];
|
|
901
|
+
const prefix = createPrefix(flags);
|
|
902
|
+
const useColor = flags.color !== false;
|
|
903
|
+
const formatGreen = createColorFormatter(useColor, green);
|
|
904
|
+
const formatDimText = (text: string) => formatDim(useColor, text);
|
|
905
|
+
|
|
906
|
+
if (result.ok) {
|
|
907
|
+
// Success summary
|
|
908
|
+
const executed = result.execution?.operationsExecuted ?? 0;
|
|
909
|
+
lines.push(`${prefix}${formatGreen('✔')} Applied ${executed} operation(s)`);
|
|
910
|
+
|
|
911
|
+
// Marker info
|
|
912
|
+
if (result.marker) {
|
|
913
|
+
lines.push(`${prefix}${formatDimText(` Marker written: ${result.marker.coreHash}`)}`);
|
|
914
|
+
if (result.marker.profileHash) {
|
|
915
|
+
lines.push(`${prefix}${formatDimText(` Profile hash: ${result.marker.profileHash}`)}`);
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
// Timings in verbose mode
|
|
920
|
+
if (isVerbose(flags, 1)) {
|
|
921
|
+
lines.push(`${prefix}${formatDimText(` Total time: ${result.timings.total}ms`)}`);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
return lines.join('\n');
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* Formats JSON output for db init command.
|
|
930
|
+
*/
|
|
931
|
+
export function formatDbInitJson(result: DbInitResult): string {
|
|
932
|
+
return JSON.stringify(result, null, 2);
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
// ============================================================================
|
|
936
|
+
// Styled Output Formatters
|
|
937
|
+
// ============================================================================
|
|
938
|
+
|
|
939
|
+
/**
|
|
940
|
+
* Fixed width for left column in help output.
|
|
941
|
+
*/
|
|
942
|
+
const LEFT_COLUMN_WIDTH = 20;
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* Minimum width for right column wrapping in help output.
|
|
946
|
+
*/
|
|
947
|
+
const RIGHT_COLUMN_MIN_WIDTH = 40;
|
|
948
|
+
|
|
949
|
+
/**
|
|
950
|
+
* Maximum width for right column wrapping in help output (when terminal is wide enough).
|
|
951
|
+
*/
|
|
952
|
+
const RIGHT_COLUMN_MAX_WIDTH = 90;
|
|
953
|
+
|
|
954
|
+
/**
|
|
955
|
+
* Gets the terminal width, or returns a default if not available.
|
|
956
|
+
*/
|
|
957
|
+
function getTerminalWidth(): number {
|
|
958
|
+
// process.stdout.columns may be undefined in non-TTY environments
|
|
959
|
+
const terminalWidth = process.stdout.columns;
|
|
960
|
+
// Default to 80 if terminal width is not available, but allow override via env var
|
|
961
|
+
const defaultWidth = Number.parseInt(process.env['CLI_WIDTH'] || '80', 10);
|
|
962
|
+
return terminalWidth || defaultWidth;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* Calculates the available width for the right column based on terminal width.
|
|
967
|
+
* Format: "│ " (2) + left column (20) + " " (2) + right column = total
|
|
968
|
+
* So: right column = terminal width - 2 - 20 - 2 = terminal width - 24
|
|
969
|
+
*/
|
|
970
|
+
function calculateRightColumnWidth(): number {
|
|
971
|
+
const terminalWidth = getTerminalWidth();
|
|
972
|
+
const availableWidth = terminalWidth - 2 - LEFT_COLUMN_WIDTH - 2; // Subtract separators and left column
|
|
973
|
+
// Ensure minimum width, but don't exceed maximum
|
|
974
|
+
return Math.max(RIGHT_COLUMN_MIN_WIDTH, Math.min(availableWidth, RIGHT_COLUMN_MAX_WIDTH));
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
/**
|
|
978
|
+
* Creates an arrow segment badge with green background and white text.
|
|
979
|
+
* Body: green background with white "prisma-next" text
|
|
980
|
+
* Tip: dark grey arrow pointing right (Powerline separator)
|
|
981
|
+
*/
|
|
982
|
+
function createPrismaNextBadge(useColor: boolean): string {
|
|
983
|
+
if (!useColor) {
|
|
984
|
+
return 'prisma-next';
|
|
985
|
+
}
|
|
986
|
+
// Body: green background with white text
|
|
987
|
+
const text = ' prisma-next ';
|
|
988
|
+
const body = bgGreen(bold(text));
|
|
989
|
+
|
|
990
|
+
// Use Powerline separator (U+E0B0) which creates the arrow transition effect
|
|
991
|
+
const separator = '\u{E0B0}';
|
|
992
|
+
const tip = green(separator); // Dark grey arrow tip
|
|
993
|
+
|
|
994
|
+
return `${body}${tip}`;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
/**
|
|
998
|
+
* Creates a padding function.
|
|
999
|
+
*/
|
|
1000
|
+
function createPadFunction(): (s: string, w: number) => string {
|
|
1001
|
+
return (s: string, w: number) => s + ' '.repeat(Math.max(0, w - s.length));
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
1005
|
+
* Formats a header line: brand + operation + intent
|
|
1006
|
+
*/
|
|
1007
|
+
function formatHeaderLine(options: {
|
|
1008
|
+
readonly brand: string;
|
|
1009
|
+
readonly operation: string;
|
|
1010
|
+
readonly intent: string;
|
|
1011
|
+
}): string {
|
|
1012
|
+
if (options.operation) {
|
|
1013
|
+
return `${options.brand} ${options.operation} → ${options.intent}`;
|
|
1014
|
+
}
|
|
1015
|
+
return `${options.brand} ${options.intent}`;
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
/**
|
|
1019
|
+
* Formats a "Read more" URL line.
|
|
1020
|
+
* The "Read more" label is in default color (not cyan), and the URL is blue.
|
|
1021
|
+
*/
|
|
1022
|
+
function formatReadMoreLine(options: {
|
|
1023
|
+
readonly url: string;
|
|
1024
|
+
readonly maxLabelWidth: number;
|
|
1025
|
+
readonly useColor: boolean;
|
|
1026
|
+
readonly formatDimText: (text: string) => string;
|
|
1027
|
+
}): string {
|
|
1028
|
+
const pad = createPadFunction();
|
|
1029
|
+
const labelPadded = pad('Read more', options.maxLabelWidth);
|
|
1030
|
+
// Label is default color (not cyan)
|
|
1031
|
+
const valueColored = options.useColor ? blue(options.url) : options.url;
|
|
1032
|
+
return `${options.formatDimText('│')} ${labelPadded} ${valueColored}`;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
/**
|
|
1036
|
+
* Pads text to a fixed width, accounting for ANSI escape codes.
|
|
1037
|
+
* Uses string-width to measure the actual display width.
|
|
1038
|
+
*/
|
|
1039
|
+
function padToFixedWidth(text: string, width: number): string {
|
|
1040
|
+
const actualWidth = stringWidth(text);
|
|
1041
|
+
const padding = Math.max(0, width - actualWidth);
|
|
1042
|
+
return text + ' '.repeat(padding);
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
/**
|
|
1046
|
+
* Wraps text to fit within a specified width using wrap-ansi.
|
|
1047
|
+
* Preserves ANSI escape codes and breaks at word boundaries.
|
|
1048
|
+
*/
|
|
1049
|
+
function wrapTextAnsi(text: string, width: number): string[] {
|
|
1050
|
+
const wrapped = wrapAnsi(text, width, { hard: false, trim: true });
|
|
1051
|
+
return wrapped.split('\n');
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
/**
|
|
1055
|
+
* Formats a default value as "default: <value>" with dimming.
|
|
1056
|
+
*/
|
|
1057
|
+
function formatDefaultValue(value: unknown, useColor: boolean): string {
|
|
1058
|
+
const valueStr = String(value);
|
|
1059
|
+
const defaultText = `default: ${valueStr}`;
|
|
1060
|
+
return useColor ? dim(defaultText) : defaultText;
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
/**
|
|
1064
|
+
* Renders a command tree structure.
|
|
1065
|
+
* Handles both single-level (subcommands of a command) and multi-level (top-level commands with subcommands) trees.
|
|
1066
|
+
*/
|
|
1067
|
+
function renderCommandTree(options: {
|
|
1068
|
+
readonly commands: readonly Command[];
|
|
1069
|
+
readonly useColor: boolean;
|
|
1070
|
+
readonly formatDimText: (text: string) => string;
|
|
1071
|
+
readonly hasItemsAfter: boolean;
|
|
1072
|
+
readonly continuationPrefix?: string;
|
|
1073
|
+
}): string[] {
|
|
1074
|
+
const { commands, useColor, formatDimText, hasItemsAfter, continuationPrefix } = options;
|
|
1075
|
+
const lines: string[] = [];
|
|
1076
|
+
|
|
1077
|
+
if (commands.length === 0) {
|
|
1078
|
+
return lines;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
// Format each command
|
|
1082
|
+
for (let i = 0; i < commands.length; i++) {
|
|
1083
|
+
const cmd = commands[i];
|
|
1084
|
+
if (!cmd) continue;
|
|
1085
|
+
|
|
1086
|
+
const subcommands = cmd.commands.filter((subcmd) => !subcmd.name().startsWith('_'));
|
|
1087
|
+
const isLastCommand = i === commands.length - 1;
|
|
1088
|
+
|
|
1089
|
+
if (subcommands.length > 0) {
|
|
1090
|
+
// Command with subcommands - show command name, then tree-structured subcommands
|
|
1091
|
+
const prefix = isLastCommand && !hasItemsAfter ? formatDimText('└') : formatDimText('├');
|
|
1092
|
+
// For top-level command, pad name to fixed width (accounting for "│ ├─ " = 5 chars)
|
|
1093
|
+
const treePrefix = `${prefix}─ `;
|
|
1094
|
+
const treePrefixWidth = stringWidth(stripAnsi(treePrefix));
|
|
1095
|
+
const remainingWidth = LEFT_COLUMN_WIDTH - treePrefixWidth;
|
|
1096
|
+
const commandNamePadded = padToFixedWidth(cmd.name(), remainingWidth);
|
|
1097
|
+
const commandNameColored = useColor ? cyan(commandNamePadded) : commandNamePadded;
|
|
1098
|
+
lines.push(`${formatDimText('│')} ${treePrefix}${commandNameColored}`);
|
|
1099
|
+
|
|
1100
|
+
for (let j = 0; j < subcommands.length; j++) {
|
|
1101
|
+
const subcmd = subcommands[j];
|
|
1102
|
+
if (!subcmd) continue;
|
|
1103
|
+
|
|
1104
|
+
const isLastSubcommand = j === subcommands.length - 1;
|
|
1105
|
+
const shortDescription = subcmd.description() || '';
|
|
1106
|
+
|
|
1107
|
+
// Use tree characters: └─ for last subcommand, ├─ for others
|
|
1108
|
+
const treeChar = isLastSubcommand ? '└' : '├';
|
|
1109
|
+
const continuation =
|
|
1110
|
+
continuationPrefix ??
|
|
1111
|
+
(isLastCommand && isLastSubcommand && !hasItemsAfter ? ' ' : formatDimText('│'));
|
|
1112
|
+
// For subcommands, account for "│ │ └─ " = 7 chars (or "│ └─ " = 6 chars if continuation is space)
|
|
1113
|
+
const continuationStr = continuation === ' ' ? ' ' : continuation;
|
|
1114
|
+
const subTreePrefix = `${continuationStr} ${formatDimText(treeChar)}─ `;
|
|
1115
|
+
const subTreePrefixWidth = stringWidth(stripAnsi(subTreePrefix));
|
|
1116
|
+
const subRemainingWidth = LEFT_COLUMN_WIDTH - subTreePrefixWidth;
|
|
1117
|
+
const subcommandNamePadded = padToFixedWidth(subcmd.name(), subRemainingWidth);
|
|
1118
|
+
const subcommandNameColored = useColor ? cyan(subcommandNamePadded) : subcommandNamePadded;
|
|
1119
|
+
lines.push(
|
|
1120
|
+
`${formatDimText('│')} ${subTreePrefix}${subcommandNameColored} ${shortDescription}`,
|
|
1121
|
+
);
|
|
1122
|
+
}
|
|
1123
|
+
} else {
|
|
1124
|
+
// Standalone command - show command name and description on same line
|
|
1125
|
+
const prefix = isLastCommand && !hasItemsAfter ? formatDimText('└') : formatDimText('├');
|
|
1126
|
+
const treePrefix = `${prefix}─ `;
|
|
1127
|
+
const treePrefixWidth = stringWidth(stripAnsi(treePrefix));
|
|
1128
|
+
const remainingWidth = LEFT_COLUMN_WIDTH - treePrefixWidth;
|
|
1129
|
+
const commandNamePadded = padToFixedWidth(cmd.name(), remainingWidth);
|
|
1130
|
+
const commandNameColored = useColor ? cyan(commandNamePadded) : commandNamePadded;
|
|
1131
|
+
const shortDescription = cmd.description() || '';
|
|
1132
|
+
lines.push(`${formatDimText('│')} ${treePrefix}${commandNameColored} ${shortDescription}`);
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
return lines;
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/**
|
|
1140
|
+
* Formats multiline description with "Prisma Next" in green.
|
|
1141
|
+
* Wraps at the same right-hand boundary as the right column.
|
|
1142
|
+
* The right edge is defined by: left column (20) + gap (2) + right column (90) = 112 characters total.
|
|
1143
|
+
* Since the description line starts with "│ " (2 chars), the text wraps at 112 - 2 = 110 characters.
|
|
1144
|
+
*/
|
|
1145
|
+
function formatMultilineDescription(options: {
|
|
1146
|
+
readonly descriptionLines: readonly string[];
|
|
1147
|
+
readonly useColor: boolean;
|
|
1148
|
+
readonly formatDimText: (text: string) => string;
|
|
1149
|
+
}): string[] {
|
|
1150
|
+
const lines: string[] = [];
|
|
1151
|
+
const formatGreen = (text: string) => (options.useColor ? green(text) : text);
|
|
1152
|
+
|
|
1153
|
+
// Calculate wrap width to align with right edge of right column
|
|
1154
|
+
// Format: "│ " (2) + left column (20) + " " (2) + right column = total
|
|
1155
|
+
// Description line has "│ " prefix (2 chars), so text wraps at total - 2
|
|
1156
|
+
const rightColumnWidth = calculateRightColumnWidth();
|
|
1157
|
+
const totalWidth = 2 + LEFT_COLUMN_WIDTH + 2 + rightColumnWidth;
|
|
1158
|
+
const wrapWidth = totalWidth - 2; // Subtract "│ " prefix
|
|
1159
|
+
|
|
1160
|
+
for (const descLine of options.descriptionLines) {
|
|
1161
|
+
// Replace "Prisma Next" with green version if present
|
|
1162
|
+
const formattedLine = descLine.replace(/Prisma Next/g, (match) => formatGreen(match));
|
|
1163
|
+
|
|
1164
|
+
// Wrap the line at the same right edge as the right column
|
|
1165
|
+
const wrappedLines = wrapTextAnsi(formattedLine, wrapWidth);
|
|
1166
|
+
for (const wrappedLine of wrappedLines) {
|
|
1167
|
+
lines.push(`${options.formatDimText('│')} ${wrappedLine}`);
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
return lines;
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
/**
|
|
1174
|
+
* Formats the header in the new experimental visual style.
|
|
1175
|
+
* This header appears at the start of command output, showing the operation,
|
|
1176
|
+
* intent, documentation link, and parameters.
|
|
1177
|
+
*/
|
|
1178
|
+
export function formatStyledHeader(options: {
|
|
1179
|
+
readonly command: string;
|
|
1180
|
+
readonly description: string;
|
|
1181
|
+
readonly url?: string;
|
|
1182
|
+
readonly details: ReadonlyArray<{ readonly label: string; readonly value: string }>;
|
|
1183
|
+
readonly flags: GlobalFlags;
|
|
1184
|
+
}): string {
|
|
1185
|
+
const lines: string[] = [];
|
|
1186
|
+
const useColor = options.flags.color !== false;
|
|
1187
|
+
const formatDimText = (text: string) => formatDim(useColor, text);
|
|
1188
|
+
|
|
1189
|
+
// Header: arrow + operation badge + intent
|
|
1190
|
+
const brand = createPrismaNextBadge(useColor);
|
|
1191
|
+
// Use full command path (e.g., "contract emit" not just "emit")
|
|
1192
|
+
const operation = useColor ? bold(options.command) : options.command;
|
|
1193
|
+
const intent = formatDimText(options.description);
|
|
1194
|
+
lines.push(formatHeaderLine({ brand, operation, intent }));
|
|
1195
|
+
lines.push(formatDimText('│')); // Vertical line separator between command and params
|
|
1196
|
+
|
|
1197
|
+
// Format details using fixed left column width (same style as help text options)
|
|
1198
|
+
for (const detail of options.details) {
|
|
1199
|
+
// Add colon to label, then pad to fixed width using padToFixedWidth for ANSI-aware padding
|
|
1200
|
+
const labelWithColon = `${detail.label}:`;
|
|
1201
|
+
const labelPadded = padToFixedWidth(labelWithColon, LEFT_COLUMN_WIDTH);
|
|
1202
|
+
const labelColored = useColor ? cyan(labelPadded) : labelPadded;
|
|
1203
|
+
lines.push(`${formatDimText('│')} ${labelColored} ${detail.value}`);
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
// Add "Read more" URL if present (same style as help text)
|
|
1207
|
+
if (options.url) {
|
|
1208
|
+
lines.push(formatDimText('│')); // Separator line before "Read more"
|
|
1209
|
+
lines.push(
|
|
1210
|
+
formatReadMoreLine({
|
|
1211
|
+
url: options.url,
|
|
1212
|
+
maxLabelWidth: LEFT_COLUMN_WIDTH,
|
|
1213
|
+
useColor,
|
|
1214
|
+
formatDimText,
|
|
1215
|
+
}),
|
|
1216
|
+
);
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
lines.push(formatDimText('└'));
|
|
1220
|
+
|
|
1221
|
+
return `${lines.join('\n')}\n`;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
/**
|
|
1225
|
+
* Formats a success message in the styled output format.
|
|
1226
|
+
*/
|
|
1227
|
+
export function formatSuccessMessage(flags: GlobalFlags): string {
|
|
1228
|
+
const useColor = flags.color !== false;
|
|
1229
|
+
const formatGreen = createColorFormatter(useColor, green);
|
|
1230
|
+
return `${formatGreen('✔')} Success`;
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
// ============================================================================
|
|
1234
|
+
// Help Output Formatters
|
|
1235
|
+
// ============================================================================
|
|
1236
|
+
|
|
1237
|
+
/**
|
|
1238
|
+
* Maps command paths to their documentation URLs.
|
|
1239
|
+
*/
|
|
1240
|
+
function getCommandDocsUrl(commandPath: string): string | undefined {
|
|
1241
|
+
const docsMap: Record<string, string> = {
|
|
1242
|
+
'contract emit': 'https://pris.ly/contract-emit',
|
|
1243
|
+
'db verify': 'https://pris.ly/db-verify',
|
|
1244
|
+
};
|
|
1245
|
+
return docsMap[commandPath];
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
/**
|
|
1249
|
+
* Builds the full command path from a command and its parents.
|
|
1250
|
+
*/
|
|
1251
|
+
function buildCommandPath(command: Command): string {
|
|
1252
|
+
const parts: string[] = [];
|
|
1253
|
+
let current: Command | undefined = command;
|
|
1254
|
+
while (current && current.name() !== 'prisma-next') {
|
|
1255
|
+
parts.unshift(current.name());
|
|
1256
|
+
current = current.parent ?? undefined;
|
|
1257
|
+
}
|
|
1258
|
+
return parts.join(' ');
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
/**
|
|
1262
|
+
* Formats help output for a command using the styled format.
|
|
1263
|
+
*/
|
|
1264
|
+
export function formatCommandHelp(options: {
|
|
1265
|
+
readonly command: Command;
|
|
1266
|
+
readonly flags: GlobalFlags;
|
|
1267
|
+
}): string {
|
|
1268
|
+
const { command, flags } = options;
|
|
1269
|
+
const lines: string[] = [];
|
|
1270
|
+
const useColor = flags.color !== false;
|
|
1271
|
+
const formatDimText = (text: string) => formatDim(useColor, text);
|
|
1272
|
+
|
|
1273
|
+
// Build full command path (e.g., "db verify")
|
|
1274
|
+
const commandPath = buildCommandPath(command);
|
|
1275
|
+
const shortDescription = command.description() || '';
|
|
1276
|
+
const longDescription = getLongDescription(command);
|
|
1277
|
+
|
|
1278
|
+
// Header: "prisma-next <full-command-path> <short-description>"
|
|
1279
|
+
const brand = createPrismaNextBadge(useColor);
|
|
1280
|
+
const operation = useColor ? bold(commandPath) : commandPath;
|
|
1281
|
+
const intent = formatDimText(shortDescription);
|
|
1282
|
+
lines.push(formatHeaderLine({ brand, operation, intent }));
|
|
1283
|
+
lines.push(formatDimText('│')); // Vertical line separator between command and params
|
|
1284
|
+
|
|
1285
|
+
// Extract options and format them
|
|
1286
|
+
const optionsList = command.options.map((opt) => {
|
|
1287
|
+
const flags = opt.flags;
|
|
1288
|
+
const description = opt.description || '';
|
|
1289
|
+
// Commander.js stores default value in defaultValue property
|
|
1290
|
+
const defaultValue = (opt as { defaultValue?: unknown }).defaultValue;
|
|
1291
|
+
return { flags, description, defaultValue };
|
|
1292
|
+
});
|
|
1293
|
+
|
|
1294
|
+
// Extract subcommands if any
|
|
1295
|
+
const subcommands = command.commands.filter((cmd) => !cmd.name().startsWith('_'));
|
|
1296
|
+
|
|
1297
|
+
// Format subcommands as a tree if present
|
|
1298
|
+
if (subcommands.length > 0) {
|
|
1299
|
+
const hasItemsAfter = optionsList.length > 0;
|
|
1300
|
+
const treeLines = renderCommandTree({
|
|
1301
|
+
commands: subcommands,
|
|
1302
|
+
useColor,
|
|
1303
|
+
formatDimText,
|
|
1304
|
+
hasItemsAfter,
|
|
1305
|
+
});
|
|
1306
|
+
lines.push(...treeLines);
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
// Add separator between subcommands and options if both exist
|
|
1310
|
+
if (subcommands.length > 0 && optionsList.length > 0) {
|
|
1311
|
+
lines.push(formatDimText('│'));
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
// Format options with fixed width, wrapping, and default values
|
|
1315
|
+
if (optionsList.length > 0) {
|
|
1316
|
+
for (const opt of optionsList) {
|
|
1317
|
+
// Format flag with fixed 30-char width
|
|
1318
|
+
const flagsPadded = padToFixedWidth(opt.flags, LEFT_COLUMN_WIDTH);
|
|
1319
|
+
let flagsColored = flagsPadded;
|
|
1320
|
+
if (useColor) {
|
|
1321
|
+
// Color placeholders in magenta, then wrap in cyan
|
|
1322
|
+
flagsColored = flagsPadded.replace(/(<[^>]+>)/g, (match: string) => magenta(match));
|
|
1323
|
+
flagsColored = cyan(flagsColored);
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
// Wrap description based on terminal width
|
|
1327
|
+
const rightColumnWidth = calculateRightColumnWidth();
|
|
1328
|
+
const wrappedDescription = wrapTextAnsi(opt.description, rightColumnWidth);
|
|
1329
|
+
|
|
1330
|
+
// First line: flag + first line of description
|
|
1331
|
+
lines.push(`${formatDimText('│')} ${flagsColored} ${wrappedDescription[0] || ''}`);
|
|
1332
|
+
|
|
1333
|
+
// Continuation lines: empty label (30 spaces) + wrapped lines
|
|
1334
|
+
for (let i = 1; i < wrappedDescription.length; i++) {
|
|
1335
|
+
const emptyLabel = ' '.repeat(LEFT_COLUMN_WIDTH);
|
|
1336
|
+
lines.push(`${formatDimText('│')} ${emptyLabel} ${wrappedDescription[i] || ''}`);
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
// Default value line (if present)
|
|
1340
|
+
if (opt.defaultValue !== undefined) {
|
|
1341
|
+
const emptyLabel = ' '.repeat(LEFT_COLUMN_WIDTH);
|
|
1342
|
+
const defaultText = formatDefaultValue(opt.defaultValue, useColor);
|
|
1343
|
+
lines.push(`${formatDimText('│')} ${emptyLabel} ${defaultText}`);
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
// Add docs URL if available (with separator line before it)
|
|
1349
|
+
const docsUrl = getCommandDocsUrl(commandPath);
|
|
1350
|
+
if (docsUrl) {
|
|
1351
|
+
lines.push(formatDimText('│')); // Separator line between params and docs
|
|
1352
|
+
lines.push(
|
|
1353
|
+
formatReadMoreLine({
|
|
1354
|
+
url: docsUrl,
|
|
1355
|
+
maxLabelWidth: LEFT_COLUMN_WIDTH,
|
|
1356
|
+
useColor,
|
|
1357
|
+
formatDimText,
|
|
1358
|
+
}),
|
|
1359
|
+
);
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
// Multi-line description (if present) - shown after all other content
|
|
1363
|
+
if (longDescription) {
|
|
1364
|
+
lines.push(formatDimText('│'));
|
|
1365
|
+
const descriptionLines = longDescription.split('\n').filter((line) => line.trim().length > 0);
|
|
1366
|
+
lines.push(...formatMultilineDescription({ descriptionLines, useColor, formatDimText }));
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
lines.push(formatDimText('└'));
|
|
1370
|
+
|
|
1371
|
+
return `${lines.join('\n')}\n`;
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
/**
|
|
1375
|
+
* Formats help output for the root program using the styled format.
|
|
1376
|
+
*/
|
|
1377
|
+
export function formatRootHelp(options: {
|
|
1378
|
+
readonly program: Command;
|
|
1379
|
+
readonly flags: GlobalFlags;
|
|
1380
|
+
}): string {
|
|
1381
|
+
const { program, flags } = options;
|
|
1382
|
+
const lines: string[] = [];
|
|
1383
|
+
const useColor = flags.color !== false;
|
|
1384
|
+
const formatDimText = (text: string) => formatDim(useColor, text);
|
|
1385
|
+
|
|
1386
|
+
// Header: "prisma-next → Manage your data layer"
|
|
1387
|
+
const brand = createPrismaNextBadge(useColor);
|
|
1388
|
+
const shortDescription = 'Manage your data layer';
|
|
1389
|
+
const intent = formatDimText(shortDescription);
|
|
1390
|
+
lines.push(formatHeaderLine({ brand, operation: '', intent }));
|
|
1391
|
+
lines.push(formatDimText('│')); // Vertical line separator after header
|
|
1392
|
+
|
|
1393
|
+
// Extract top-level commands (exclude hidden commands starting with '_' and the 'help' command)
|
|
1394
|
+
const topLevelCommands = program.commands.filter(
|
|
1395
|
+
(cmd) => !cmd.name().startsWith('_') && cmd.name() !== 'help',
|
|
1396
|
+
);
|
|
1397
|
+
|
|
1398
|
+
// Extract global options (needed to determine if last command)
|
|
1399
|
+
const globalOptions = program.options.map((opt) => {
|
|
1400
|
+
const flags = opt.flags;
|
|
1401
|
+
const description = opt.description || '';
|
|
1402
|
+
// Commander.js stores default value in defaultValue property
|
|
1403
|
+
const defaultValue = (opt as { defaultValue?: unknown }).defaultValue;
|
|
1404
|
+
return { flags, description, defaultValue };
|
|
1405
|
+
});
|
|
1406
|
+
|
|
1407
|
+
// Build command tree
|
|
1408
|
+
if (topLevelCommands.length > 0) {
|
|
1409
|
+
const hasItemsAfter = globalOptions.length > 0;
|
|
1410
|
+
const treeLines = renderCommandTree({
|
|
1411
|
+
commands: topLevelCommands,
|
|
1412
|
+
useColor,
|
|
1413
|
+
formatDimText,
|
|
1414
|
+
hasItemsAfter,
|
|
1415
|
+
});
|
|
1416
|
+
lines.push(...treeLines);
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
// Add separator between commands and options if both exist
|
|
1420
|
+
if (topLevelCommands.length > 0 && globalOptions.length > 0) {
|
|
1421
|
+
lines.push(formatDimText('│'));
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
// Format global options with fixed width, wrapping, and default values
|
|
1425
|
+
if (globalOptions.length > 0) {
|
|
1426
|
+
for (const opt of globalOptions) {
|
|
1427
|
+
// Format flag with fixed 30-char width
|
|
1428
|
+
const flagsPadded = padToFixedWidth(opt.flags, LEFT_COLUMN_WIDTH);
|
|
1429
|
+
let flagsColored = flagsPadded;
|
|
1430
|
+
if (useColor) {
|
|
1431
|
+
// Color placeholders in magenta, then wrap in cyan
|
|
1432
|
+
flagsColored = flagsPadded.replace(/(<[^>]+>)/g, (match: string) => magenta(match));
|
|
1433
|
+
flagsColored = cyan(flagsColored);
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
// Wrap description based on terminal width
|
|
1437
|
+
const rightColumnWidth = calculateRightColumnWidth();
|
|
1438
|
+
const wrappedDescription = wrapTextAnsi(opt.description, rightColumnWidth);
|
|
1439
|
+
|
|
1440
|
+
// First line: flag + first line of description
|
|
1441
|
+
lines.push(`${formatDimText('│')} ${flagsColored} ${wrappedDescription[0] || ''}`);
|
|
1442
|
+
|
|
1443
|
+
// Continuation lines: empty label (30 spaces) + wrapped lines
|
|
1444
|
+
for (let i = 1; i < wrappedDescription.length; i++) {
|
|
1445
|
+
const emptyLabel = ' '.repeat(LEFT_COLUMN_WIDTH);
|
|
1446
|
+
lines.push(`${formatDimText('│')} ${emptyLabel} ${wrappedDescription[i] || ''}`);
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
// Default value line (if present)
|
|
1450
|
+
if (opt.defaultValue !== undefined) {
|
|
1451
|
+
const emptyLabel = ' '.repeat(LEFT_COLUMN_WIDTH);
|
|
1452
|
+
const defaultText = formatDefaultValue(opt.defaultValue, useColor);
|
|
1453
|
+
lines.push(`${formatDimText('│')} ${emptyLabel} ${defaultText}`);
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
// Multi-line description (white, not dimmed, with "Prisma Next" in green) - shown at bottom
|
|
1459
|
+
const formatGreen = (text: string) => (useColor ? green(text) : text);
|
|
1460
|
+
const descriptionLines = [
|
|
1461
|
+
`Use ${formatGreen('Prisma Next')} to define your data layer as a contract. Sign your database and application with the same contract to guarantee compatibility. Plan and apply migrations to safely evolve your schema.`,
|
|
1462
|
+
];
|
|
1463
|
+
if (descriptionLines.length > 0) {
|
|
1464
|
+
lines.push(formatDimText('│')); // Separator line before description
|
|
1465
|
+
lines.push(...formatMultilineDescription({ descriptionLines, useColor, formatDimText }));
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
lines.push(formatDimText('└'));
|
|
1469
|
+
|
|
1470
|
+
return `${lines.join('\n')}\n`;
|
|
1471
|
+
}
|