import { r as runCleoCli } from './spawn-cli-wI1lBJhm.js';
import { json } from '@sveltejs/kit';

//#region src/lib/server/cli-action.ts
/**
* Shared error-handling helper for CLI-invoking endpoints.
*
* Wraps spawn-cli calls and returns structured LAFS-compliant error envelopes
* per ADR-039. All CLI subprocess errors return 4xx (client/input error), not 5xx.
*
* @task T722
*/
/**
* Wraps a `cleo` CLI command and returns a structured LAFS error envelope on failure.
*
* On CLI failure (non-zero exit), returns **4xx** with `{ success: false, error, meta }`.
* On CLI success, returns **2xx** with the CLI's JSON envelope (or `{ success: true }`).
*
* @param args - Arguments for `cleo` CLI (e.g., `['nexus', 'analyze', '/path', '--json']`).
* @param options - Error code and metadata options.
* @returns A SvelteKit JSON response (2xx on success, 4xx on failure).
*
* @example
* ```ts
* const result = await executeCliAction(['nexus', 'analyze', projectPath, '--json'], {
*   errorCode: 'E_INDEX_FAILED',
*   meta: { projectId: 'proj-123' },
* });
* return result;
* ```
*/
async function executeCliAction(args, options = {}) {
	const { errorCode = "CLI_FAILURE", meta } = options;
	const result = await runCleoCli(args);
	if (!result.ok) return json({
		success: false,
		error: {
			code: errorCode,
			message: result.stderr.trim() || result.stdout.trim() || "CLI command failed"
		},
		meta: {
			exitCode: result.exitCode,
			...meta
		}
	}, { status: 400 });
	return json(result.envelope ?? {
		success: true,
		data: {},
		meta
	});
}

export { executeCliAction as e };
//# sourceMappingURL=cli-action-DQcaqcfn.js.map
