🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@prisma-next/cli

Package Overview
Dependencies
Maintainers
4
Versions
994
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@prisma-next/cli - npm Package Compare versions

Comparing version
0.16.0-dev.6
to
0.16.0-dev.7
+103
dist/format-CFLZv_zU.mjs
import { A as formatStyledHeader, F as CliStructuredError, _ as createTerminalUI, ct as errorUnexpected, g as parseGlobalFlagsOrExit, l as setCommandDescriptions, rt as errorRuntime, t as addGlobalOptions, u as setCommandExamples, y as handleResult } from "./command-helpers-CVn3U9Uz.mjs";
import { Command } from "commander";
import { loadConfig } from "@prisma-next/config-loader";
import { ifDefined } from "@prisma-next/utils/defined";
import { notOk, ok } from "@prisma-next/utils/result";
import { relative, resolve } from "pathe";
import { readFile, writeFile } from "node:fs/promises";
import { EOL } from "node:os";
import { format } from "@prisma-next/psl-parser/format";
import { isStructuredError } from "@prisma-next/utils/structured-error";
//#region src/control-api/operations/format.ts
function resolveNewline(formatterNewline, eol) {
if (formatterNewline !== void 0) return formatterNewline;
return eol === "\r\n" ? "CRLF" : "LF";
}
async function executeFormat(options) {
const eol = options.eol ?? EOL;
let config;
try {
config = await loadConfig(options.configPath);
} catch (error) {
if (CliStructuredError.is(error)) return notOk(error);
return notOk(errorUnexpected(error instanceof Error ? error.message : String(error)));
}
const source = config.contract?.source;
if (source?.sourceFormat !== "psl") return ok({ formatted: false });
const inputPath = source.inputs?.[0];
if (inputPath === void 0) return ok({ formatted: false });
let contents;
try {
contents = await readFile(inputPath, "utf-8");
} catch (error) {
return notOk(errorRuntime("Failed to read contract source file", {
why: error instanceof Error ? error.message : String(error),
fix: `Check that ${inputPath} exists and is readable.`
}));
}
const formatOptions = {
indent: config.formatter?.indent ?? 2,
newline: resolveNewline(config.formatter?.newline, eol)
};
let formatted;
try {
formatted = format(contents, formatOptions);
} catch (error) {
if (isStructuredError(error) && error.code === "PSL.PARSE_FAILED") return notOk(errorRuntime("Cannot format PSL with parse errors", {
why: error.message,
fix: "Fix the parse errors in your schema and try again.",
meta: { diagnostics: error.meta?.["diagnostics"] }
}));
return notOk(errorUnexpected(error instanceof Error ? error.message : String(error)));
}
try {
await writeFile(inputPath, formatted, "utf-8");
} catch (error) {
return notOk(errorRuntime("Failed to write formatted contract source file", {
why: error instanceof Error ? error.message : String(error),
fix: `Check that ${inputPath} is writable.`
}));
}
return ok({
formatted: true,
path: inputPath
});
}
//#endregion
//#region src/commands/format.ts
function createFormatCommand() {
const command = new Command("format");
setCommandDescriptions(command, "Format your PSL contract source", "Formats the Prisma schema (PSL) contract source declared in your config\n(contract.source.inputs[0]) in place. Only runs when contract.source.sourceFormat\nis 'psl'; a TypeScript or unset source is left untouched. Indent and newline are\nread from the optional formatter config section, defaulting to two spaces and the\nsystem newline.");
setCommandExamples(command, ["prisma-next format", "prisma-next format --config ./custom-config.ts"]);
addGlobalOptions(command).option("--config <path>", "Path to prisma-next.config.ts").action(async (options) => {
const flags = parseGlobalFlagsOrExit(options);
const ui = createTerminalUI(flags);
if (!flags.json && !flags.quiet) {
const displayConfigPath = options.config ? relative(process.cwd(), resolve(options.config)) : "prisma-next.config.ts";
ui.stderr(formatStyledHeader({
command: "format",
description: "Format your PSL contract source",
details: [{
label: "config",
value: displayConfigPath
}],
flags
}));
}
const exitCode = handleResult(await executeFormat({ ...ifDefined("configPath", options.config) }), flags, ui, (value) => {
if (flags.json) {
ui.output(JSON.stringify(value));
return;
}
if (flags.quiet) return;
if (value.formatted) ui.success(`Formatted ${relative(process.cwd(), value.path ?? "")}`);
else ui.info("Nothing to format (contract source is not PSL).");
});
process.exit(exitCode);
});
return command;
}
//#endregion
export { createFormatCommand as t };
//# sourceMappingURL=format-CFLZv_zU.mjs.map
{"version":3,"file":"format-CFLZv_zU.mjs","names":[],"sources":["../src/control-api/operations/format.ts","../src/commands/format.ts"],"sourcesContent":["import { readFile, writeFile } from 'node:fs/promises';\nimport { EOL } from 'node:os';\nimport { loadConfig } from '@prisma-next/config-loader';\nimport { type FormatOptions, format } from '@prisma-next/psl-parser/format';\nimport { notOk, ok, type Result } from '@prisma-next/utils/result';\nimport { isStructuredError } from '@prisma-next/utils/structured-error';\nimport { CliStructuredError, errorRuntime, errorUnexpected } from '../../utils/cli-errors';\n\nexport interface FormatOperationOptions {\n readonly configPath?: string;\n readonly eol?: string;\n}\n\nexport interface FormatOperationResult {\n readonly formatted: boolean;\n readonly path?: string;\n}\n\nexport function resolveNewline(\n formatterNewline: 'LF' | 'CRLF' | undefined,\n eol: string,\n): 'LF' | 'CRLF' {\n if (formatterNewline !== undefined) {\n return formatterNewline;\n }\n return eol === '\\r\\n' ? 'CRLF' : 'LF';\n}\n\nexport async function executeFormat(\n options: FormatOperationOptions,\n): Promise<Result<FormatOperationResult, CliStructuredError>> {\n const eol = options.eol ?? EOL;\n\n let config: Awaited<ReturnType<typeof loadConfig>>;\n try {\n config = await loadConfig(options.configPath);\n } catch (error) {\n if (CliStructuredError.is(error)) {\n return notOk(error);\n }\n return notOk(errorUnexpected(error instanceof Error ? error.message : String(error)));\n }\n\n const source = config.contract?.source;\n if (source?.sourceFormat !== 'psl') {\n return ok({ formatted: false });\n }\n\n const inputPath = source.inputs?.[0];\n if (inputPath === undefined) {\n return ok({ formatted: false });\n }\n\n let contents: string;\n try {\n contents = await readFile(inputPath, 'utf-8');\n } catch (error) {\n return notOk(\n errorRuntime('Failed to read contract source file', {\n why: error instanceof Error ? error.message : String(error),\n fix: `Check that ${inputPath} exists and is readable.`,\n }),\n );\n }\n\n const formatOptions: FormatOptions = {\n indent: config.formatter?.indent ?? 2,\n newline: resolveNewline(config.formatter?.newline, eol),\n };\n\n let formatted: string;\n try {\n formatted = format(contents, formatOptions);\n } catch (error) {\n if (isStructuredError(error) && error.code === 'PSL.PARSE_FAILED') {\n return notOk(\n errorRuntime('Cannot format PSL with parse errors', {\n why: error.message,\n fix: 'Fix the parse errors in your schema and try again.',\n meta: { diagnostics: error.meta?.['diagnostics'] },\n }),\n );\n }\n return notOk(errorUnexpected(error instanceof Error ? error.message : String(error)));\n }\n\n try {\n await writeFile(inputPath, formatted, 'utf-8');\n } catch (error) {\n return notOk(\n errorRuntime('Failed to write formatted contract source file', {\n why: error instanceof Error ? error.message : String(error),\n fix: `Check that ${inputPath} is writable.`,\n }),\n );\n }\n\n return ok({ formatted: true, path: inputPath });\n}\n","import { ifDefined } from '@prisma-next/utils/defined';\nimport { Command } from 'commander';\nimport { relative, resolve } from 'pathe';\nimport { executeFormat } from '../control-api/operations/format';\nimport {\n addGlobalOptions,\n setCommandDescriptions,\n setCommandExamples,\n} from '../utils/command-helpers';\nimport { formatStyledHeader } from '../utils/formatters/styled';\nimport type { CommonCommandOptions } from '../utils/global-flags';\nimport { parseGlobalFlagsOrExit } from '../utils/global-flags';\nimport { handleResult } from '../utils/result-handler';\nimport { createTerminalUI } from '../utils/terminal-ui';\n\ninterface FormatCommandOptions extends CommonCommandOptions {\n readonly config?: string;\n}\n\nexport function createFormatCommand(): Command {\n const command = new Command('format');\n setCommandDescriptions(\n command,\n 'Format your PSL contract source',\n 'Formats the Prisma schema (PSL) contract source declared in your config\\n' +\n '(contract.source.inputs[0]) in place. Only runs when contract.source.sourceFormat\\n' +\n \"is 'psl'; a TypeScript or unset source is left untouched. Indent and newline are\\n\" +\n 'read from the optional formatter config section, defaulting to two spaces and the\\n' +\n 'system newline.',\n );\n setCommandExamples(command, [\n 'prisma-next format',\n 'prisma-next format --config ./custom-config.ts',\n ]);\n addGlobalOptions(command)\n .option('--config <path>', 'Path to prisma-next.config.ts')\n .action(async (options: FormatCommandOptions) => {\n const flags = parseGlobalFlagsOrExit(options);\n const ui = createTerminalUI(flags);\n\n if (!flags.json && !flags.quiet) {\n const displayConfigPath = options.config\n ? relative(process.cwd(), resolve(options.config))\n : 'prisma-next.config.ts';\n ui.stderr(\n formatStyledHeader({\n command: 'format',\n description: 'Format your PSL contract source',\n details: [{ label: 'config', value: displayConfigPath }],\n flags,\n }),\n );\n }\n\n const result = await executeFormat({ ...ifDefined('configPath', options.config) });\n\n const exitCode = handleResult(result, flags, ui, (value) => {\n if (flags.json) {\n ui.output(JSON.stringify(value));\n return;\n }\n if (flags.quiet) {\n return;\n }\n if (value.formatted) {\n ui.success(`Formatted ${relative(process.cwd(), value.path ?? '')}`);\n } else {\n ui.info('Nothing to format (contract source is not PSL).');\n }\n });\n process.exit(exitCode);\n });\n\n return command;\n}\n"],"mappings":";;;;;;;;;;;AAkBA,SAAgB,eACd,kBACA,KACe;CACf,IAAI,qBAAqB,KAAA,GACvB,OAAO;CAET,OAAO,QAAQ,SAAS,SAAS;AACnC;AAEA,eAAsB,cACpB,SAC4D;CAC5D,MAAM,MAAM,QAAQ,OAAO;CAE3B,IAAI;CACJ,IAAI;EACF,SAAS,MAAM,WAAW,QAAQ,UAAU;CAC9C,SAAS,OAAO;EACd,IAAI,mBAAmB,GAAG,KAAK,GAC7B,OAAO,MAAM,KAAK;EAEpB,OAAO,MAAM,gBAAgB,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC,CAAC;CACtF;CAEA,MAAM,SAAS,OAAO,UAAU;CAChC,IAAI,QAAQ,iBAAiB,OAC3B,OAAO,GAAG,EAAE,WAAW,MAAM,CAAC;CAGhC,MAAM,YAAY,OAAO,SAAS;CAClC,IAAI,cAAc,KAAA,GAChB,OAAO,GAAG,EAAE,WAAW,MAAM,CAAC;CAGhC,IAAI;CACJ,IAAI;EACF,WAAW,MAAM,SAAS,WAAW,OAAO;CAC9C,SAAS,OAAO;EACd,OAAO,MACL,aAAa,uCAAuC;GAClD,KAAK,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;GAC1D,KAAK,cAAc,UAAU;EAC/B,CAAC,CACH;CACF;CAEA,MAAM,gBAA+B;EACnC,QAAQ,OAAO,WAAW,UAAU;EACpC,SAAS,eAAe,OAAO,WAAW,SAAS,GAAG;CACxD;CAEA,IAAI;CACJ,IAAI;EACF,Y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D,IAAI,MAAM,MAAM;IACd,GAAG,OAAO,KAAK,UAAU,KAAK,CAAC;IAC/B;GACF;GACA,IAAI,MAAM,OACR;GAEF,IAAI,MAAM,WACR,GAAG,QAAQ,aAAa,SAAS,QAAQ,IAAI,GAAG,MAAM,QAAQ,EAAE,GAAG;QAEnE,GAAG,KAAK,iDAAiD;EAE7D,CAAC;EACD,QAAQ,KAAK,QAAQ;CACvB,CAAC;CAEH,OAAO;AACT"}
+2
-2

@@ -10,3 +10,3 @@ #!/usr/bin/env node

import { t as createDbVerifyCommand } from "./db-verify-BpwCMyWJ.mjs";
import { t as createFormatCommand } from "./format-CefCDzkw.mjs";
import { t as createFormatCommand } from "./format-CFLZv_zU.mjs";
import { t as createMigrationListCommand } from "./migration-list-cnSKilYH.mjs";

@@ -29,3 +29,3 @@ import { createMigrateCommand } from "./commands/migrate.mjs";

//#region package.json
var version = "0.16.0-dev.6";
var version = "0.16.0-dev.7";
//#endregion

@@ -32,0 +32,0 @@ //#region src/commands/init/templates/code-templates.ts

import { t as createContractEmitCommand } from "../contract-emit-DzOkTs11.mjs";
import { t as createFormatCommand } from "../format-CefCDzkw.mjs";
import { t as createFormatCommand } from "../format-CFLZv_zU.mjs";
import { join, resolve } from "pathe";

@@ -4,0 +4,0 @@ import { existsSync, unlinkSync, writeFileSync } from "node:fs";

{
"name": "@prisma-next/cli",
"version": "0.16.0-dev.6",
"version": "0.16.0-dev.7",
"license": "Apache-2.0",

@@ -12,14 +12,14 @@ "type": "module",

"@clack/prompts": "^1.7.0",
"@prisma-next/config": "0.16.0-dev.6",
"@prisma-next/config-loader": "0.16.0-dev.6",
"@prisma-next/contract": "0.16.0-dev.6",
"@prisma-next/emitter": "0.16.0-dev.6",
"@prisma-next/errors": "0.16.0-dev.6",
"@prisma-next/framework-components": "0.16.0-dev.6",
"@prisma-next/language-server": "0.16.0-dev.6",
"@prisma-next/migration-tools": "0.16.0-dev.6",
"@prisma-next/psl-parser": "0.16.0-dev.6",
"@prisma-next/psl-printer": "0.16.0-dev.6",
"@prisma-next/cli-telemetry": "0.16.0-dev.6",
"@prisma-next/utils": "0.16.0-dev.6",
"@prisma-next/config": "0.16.0-dev.7",
"@prisma-next/config-loader": "0.16.0-dev.7",
"@prisma-next/contract": "0.16.0-dev.7",
"@prisma-next/emitter": "0.16.0-dev.7",
"@prisma-next/errors": "0.16.0-dev.7",
"@prisma-next/framework-components": "0.16.0-dev.7",
"@prisma-next/language-server": "0.16.0-dev.7",
"@prisma-next/migration-tools": "0.16.0-dev.7",
"@prisma-next/psl-parser": "0.16.0-dev.7",
"@prisma-next/psl-printer": "0.16.0-dev.7",
"@prisma-next/cli-telemetry": "0.16.0-dev.7",
"@prisma-next/utils": "0.16.0-dev.7",
"arktype": "^2.2.2",

@@ -40,10 +40,10 @@ "ci-info": "^4.3.1",

"devDependencies": {
"@prisma-next/sql-contract": "0.16.0-dev.6",
"@prisma-next/sql-contract-emitter": "0.16.0-dev.6",
"@prisma-next/sql-contract-ts": "0.16.0-dev.6",
"@prisma-next/sql-operations": "0.16.0-dev.6",
"@prisma-next/sql-runtime": "0.16.0-dev.6",
"@prisma-next/test-utils": "0.16.0-dev.6",
"@prisma-next/tsconfig": "0.16.0-dev.6",
"@prisma-next/tsdown": "0.16.0-dev.6",
"@prisma-next/sql-contract": "0.16.0-dev.7",
"@prisma-next/sql-contract-emitter": "0.16.0-dev.7",
"@prisma-next/sql-contract-ts": "0.16.0-dev.7",
"@prisma-next/sql-operations": "0.16.0-dev.7",
"@prisma-next/sql-runtime": "0.16.0-dev.7",
"@prisma-next/test-utils": "0.16.0-dev.7",
"@prisma-next/tsconfig": "0.16.0-dev.7",
"@prisma-next/tsdown": "0.16.0-dev.7",
"@types/node": "25.9.4",

@@ -50,0 +50,0 @@ "tsdown": "0.22.8",

import { readFile, writeFile } from 'node:fs/promises';
import { EOL } from 'node:os';
import { loadConfig } from '@prisma-next/config-loader';
import { type FormatOptions, format, PslFormatError } from '@prisma-next/psl-parser/format';
import { type FormatOptions, format } from '@prisma-next/psl-parser/format';
import { notOk, ok, type Result } from '@prisma-next/utils/result';
import { isStructuredError } from '@prisma-next/utils/structured-error';
import { CliStructuredError, errorRuntime, errorUnexpected } from '../../utils/cli-errors';

@@ -74,3 +75,3 @@

} catch (error) {
if (error instanceof PslFormatError) {
if (isStructuredError(error) && error.code === 'PSL.PARSE_FAILED') {
return notOk(

@@ -80,3 +81,3 @@ errorRuntime('Cannot format PSL with parse errors', {

fix: 'Fix the parse errors in your schema and try again.',
meta: { diagnostics: error.diagnostics },
meta: { diagnostics: error.meta?.['diagnostics'] },
}),

@@ -83,0 +84,0 @@ );

import { A as formatStyledHeader, F as CliStructuredError, _ as createTerminalUI, ct as errorUnexpected, g as parseGlobalFlagsOrExit, l as setCommandDescriptions, rt as errorRuntime, t as addGlobalOptions, u as setCommandExamples, y as handleResult } from "./command-helpers-CVn3U9Uz.mjs";
import { Command } from "commander";
import { loadConfig } from "@prisma-next/config-loader";
import { ifDefined } from "@prisma-next/utils/defined";
import { notOk, ok } from "@prisma-next/utils/result";
import { relative, resolve } from "pathe";
import { readFile, writeFile } from "node:fs/promises";
import { EOL } from "node:os";
import { PslFormatError, format } from "@prisma-next/psl-parser/format";
//#region src/control-api/operations/format.ts
function resolveNewline(formatterNewline, eol) {
if (formatterNewline !== void 0) return formatterNewline;
return eol === "\r\n" ? "CRLF" : "LF";
}
async function executeFormat(options) {
const eol = options.eol ?? EOL;
let config;
try {
config = await loadConfig(options.configPath);
} catch (error) {
if (CliStructuredError.is(error)) return notOk(error);
return notOk(errorUnexpected(error instanceof Error ? error.message : String(error)));
}
const source = config.contract?.source;
if (source?.sourceFormat !== "psl") return ok({ formatted: false });
const inputPath = source.inputs?.[0];
if (inputPath === void 0) return ok({ formatted: false });
let contents;
try {
contents = await readFile(inputPath, "utf-8");
} catch (error) {
return notOk(errorRuntime("Failed to read contract source file", {
why: error instanceof Error ? error.message : String(error),
fix: `Check that ${inputPath} exists and is readable.`
}));
}
const formatOptions = {
indent: config.formatter?.indent ?? 2,
newline: resolveNewline(config.formatter?.newline, eol)
};
let formatted;
try {
formatted = format(contents, formatOptions);
} catch (error) {
if (error instanceof PslFormatError) return notOk(errorRuntime("Cannot format PSL with parse errors", {
why: error.message,
fix: "Fix the parse errors in your schema and try again.",
meta: { diagnostics: error.diagnostics }
}));
return notOk(errorUnexpected(error instanceof Error ? error.message : String(error)));
}
try {
await writeFile(inputPath, formatted, "utf-8");
} catch (error) {
return notOk(errorRuntime("Failed to write formatted contract source file", {
why: error instanceof Error ? error.message : String(error),
fix: `Check that ${inputPath} is writable.`
}));
}
return ok({
formatted: true,
path: inputPath
});
}
//#endregion
//#region src/commands/format.ts
function createFormatCommand() {
const command = new Command("format");
setCommandDescriptions(command, "Format your PSL contract source", "Formats the Prisma schema (PSL) contract source declared in your config\n(contract.source.inputs[0]) in place. Only runs when contract.source.sourceFormat\nis 'psl'; a TypeScript or unset source is left untouched. Indent and newline are\nread from the optional formatter config section, defaulting to two spaces and the\nsystem newline.");
setCommandExamples(command, ["prisma-next format", "prisma-next format --config ./custom-config.ts"]);
addGlobalOptions(command).option("--config <path>", "Path to prisma-next.config.ts").action(async (options) => {
const flags = parseGlobalFlagsOrExit(options);
const ui = createTerminalUI(flags);
if (!flags.json && !flags.quiet) {
const displayConfigPath = options.config ? relative(process.cwd(), resolve(options.config)) : "prisma-next.config.ts";
ui.stderr(formatStyledHeader({
command: "format",
description: "Format your PSL contract source",
details: [{
label: "config",
value: displayConfigPath
}],
flags
}));
}
const exitCode = handleResult(await executeFormat({ ...ifDefined("configPath", options.config) }), flags, ui, (value) => {
if (flags.json) {
ui.output(JSON.stringify(value));
return;
}
if (flags.quiet) return;
if (value.formatted) ui.success(`Formatted ${relative(process.cwd(), value.path ?? "")}`);
else ui.info("Nothing to format (contract source is not PSL).");
});
process.exit(exitCode);
});
return command;
}
//#endregion
export { createFormatCommand as t };
//# sourceMappingURL=format-CefCDzkw.mjs.map
{"version":3,"file":"format-CefCDzkw.mjs","names":[],"sources":["../src/control-api/operations/format.ts","../src/commands/format.ts"],"sourcesContent":["import { readFile, writeFile } from 'node:fs/promises';\nimport { EOL } from 'node:os';\nimport { loadConfig } from '@prisma-next/config-loader';\nimport { type FormatOptions, format, PslFormatError } from '@prisma-next/psl-parser/format';\nimport { notOk, ok, type Result } from '@prisma-next/utils/result';\nimport { CliStructuredError, errorRuntime, errorUnexpected } from '../../utils/cli-errors';\n\nexport interface FormatOperationOptions {\n readonly configPath?: string;\n readonly eol?: string;\n}\n\nexport interface FormatOperationResult {\n readonly formatted: boolean;\n readonly path?: string;\n}\n\nexport function resolveNewline(\n formatterNewline: 'LF' | 'CRLF' | undefined,\n eol: string,\n): 'LF' | 'CRLF' {\n if (formatterNewline !== undefined) {\n return formatterNewline;\n }\n return eol === '\\r\\n' ? 'CRLF' : 'LF';\n}\n\nexport async function executeFormat(\n options: FormatOperationOptions,\n): Promise<Result<FormatOperationResult, CliStructuredError>> {\n const eol = options.eol ?? EOL;\n\n let config: Awaited<ReturnType<typeof loadConfig>>;\n try {\n config = await loadConfig(options.configPath);\n } catch (error) {\n if (CliStructuredError.is(error)) {\n return notOk(error);\n }\n return notOk(errorUnexpected(error instanceof Error ? error.message : String(error)));\n }\n\n const source = config.contract?.source;\n if (source?.sourceFormat !== 'psl') {\n return ok({ formatted: false });\n }\n\n const inputPath = source.inputs?.[0];\n if (inputPath === undefined) {\n return ok({ formatted: false });\n }\n\n let contents: string;\n try {\n contents = await readFile(inputPath, 'utf-8');\n } catch (error) {\n return notOk(\n errorRuntime('Failed to read contract source file', {\n why: error instanceof Error ? error.message : String(error),\n fix: `Check that ${inputPath} exists and is readable.`,\n }),\n );\n }\n\n const formatOptions: FormatOptions = {\n indent: config.formatter?.indent ?? 2,\n newline: resolveNewline(config.formatter?.newline, eol),\n };\n\n let formatted: string;\n try {\n formatted = format(contents, formatOptions);\n } catch (error) {\n if (error instanceof PslFormatError) {\n return notOk(\n errorRuntime('Cannot format PSL with parse errors', {\n why: error.message,\n fix: 'Fix the parse errors in your schema and try again.',\n meta: { diagnostics: error.diagnostics },\n }),\n );\n }\n return notOk(errorUnexpected(error instanceof Error ? error.message : String(error)));\n }\n\n try {\n await writeFile(inputPath, formatted, 'utf-8');\n } catch (error) {\n return notOk(\n errorRuntime('Failed to write formatted contract source file', {\n why: error instanceof Error ? error.message : String(error),\n fix: `Check that ${inputPath} is writable.`,\n }),\n );\n }\n\n return ok({ formatted: true, path: inputPath });\n}\n","import { ifDefined } from '@prisma-next/utils/defined';\nimport { Command } from 'commander';\nimport { relative, resolve } from 'pathe';\nimport { executeFormat } from '../control-api/operations/format';\nimport {\n addGlobalOptions,\n setCommandDescriptions,\n setCommandExamples,\n} from '../utils/command-helpers';\nimport { formatStyledHeader } from '../utils/formatters/styled';\nimport type { CommonCommandOptions } from '../utils/global-flags';\nimport { parseGlobalFlagsOrExit } from '../utils/global-flags';\nimport { handleResult } from '../utils/result-handler';\nimport { createTerminalUI } from '../utils/terminal-ui';\n\ninterface FormatCommandOptions extends CommonCommandOptions {\n readonly config?: string;\n}\n\nexport function createFormatCommand(): Command {\n const command = new Command('format');\n setCommandDescriptions(\n command,\n 'Format your PSL contract source',\n 'Formats the Prisma schema (PSL) contract source declared in your config\\n' +\n '(contract.source.inputs[0]) in place. Only runs when contract.source.sourceFormat\\n' +\n \"is 'psl'; a TypeScript or unset source is left untouched. Indent and newline are\\n\" +\n 'read from the optional formatter config section, defaulting to two spaces and the\\n' +\n 'system newline.',\n );\n setCommandExamples(command, [\n 'prisma-next format',\n 'prisma-next format --config ./custom-config.ts',\n ]);\n addGlobalOptions(command)\n .option('--config <path>', 'Path to prisma-next.config.ts')\n .action(async (options: FormatCommandOptions) => {\n const flags = parseGlobalFlagsOrExit(options);\n const ui = createTerminalUI(flags);\n\n if (!flags.json && !flags.quiet) {\n const displayConfigPath = options.config\n ? relative(process.cwd(), resolve(options.config))\n : 'prisma-next.config.ts';\n ui.stderr(\n formatStyledHeader({\n command: 'format',\n description: 'Format your PSL contract source',\n details: [{ label: 'config', value: displayConfigPath }],\n flags,\n }),\n );\n }\n\n const result = await executeFormat({ ...ifDefined('configPath', options.config) });\n\n const exitCode = handleResult(result, flags, ui, (value) => {\n if (flags.json) {\n ui.output(JSON.stringify(value));\n return;\n }\n if (flags.quiet) {\n return;\n }\n if (value.formatted) {\n ui.success(`Formatted ${relative(process.cwd(), value.path ?? '')}`);\n } else {\n ui.info('Nothing to format (contract source is not PSL).');\n }\n });\n process.exit(exitCode);\n });\n\n return command;\n}\n"],"mappings":";;;;;;;;;;AAiBA,SAAgB,eACd,kBACA,KACe;CACf,IAAI,qBAAqB,KAAA,GACvB,OAAO;CAET,OAAO,QAAQ,SAAS,SAAS;AACnC;AAEA,eAAsB,cACpB,SAC4D;CAC5D,MAAM,MAAM,QAAQ,OAAO;CAE3B,IAAI;CACJ,IAAI;EACF,SAAS,MAAM,WAAW,QAAQ,UAAU;CAC9C,SAAS,OAAO;EACd,IAAI,mBAAmB,GAAG,KAAK,GAC7B,OAAO,MAAM,KAAK;EAEpB,OAAO,MAAM,gBAAgB,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC,CAAC;CACtF;CAEA,MAAM,SAAS,OAAO,UAAU;CAChC,IAAI,QAAQ,iBAAiB,OAC3B,OAAO,GAAG,EAAE,WAAW,MAAM,CAAC;CAGhC,MAAM,YAAY,OAAO,SAAS;CAClC,IAAI,cAAc,KAAA,GAChB,OAAO,GAAG,EAAE,WAAW,MAAM,CAAC;CAGhC,IAAI;CACJ,IAAI;EACF,WAAW,MAAM,SAAS,WAAW,OAAO;CAC9C,SAAS,OAAO;EACd,OAAO,MACL,aAAa,uCAAuC;GAClD,KAAK,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;GAC1D,KAAK,cAAc,UAAU;EAC/B,CAAC,CACH;CACF;CAEA,MAAM,gBAA+B;EACnC,QAAQ,OAAO,WAAW,UAAU;EACpC,SAAS,eAAe,OAAO,WAAW,SAAS,GAAG;CACxD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