UNPKG

@pgsql/cli

Version:

Unified CLI for PostgreSQL AST parsing, deparsing, and code generation

61 lines (60 loc) 2.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseCommand = parseCommand; const fs_1 = require("fs"); const path_1 = require("path"); const pgsql_parser_1 = require("pgsql-parser"); const chalk_1 = __importDefault(require("chalk")); const help_1 = require("../utils/help"); async function parseCommand(argv) { if (argv.help) { (0, help_1.showHelp)('parse'); process.exit(0); } const sqlFile = argv._[1]; if (!sqlFile) { console.error(chalk_1.default.red('Error: SQL file required')); (0, help_1.showHelp)('parse'); process.exit(1); } try { // Resolve file path const filePath = sqlFile.startsWith('/') ? sqlFile : (0, path_1.resolve)((0, path_1.join)(process.cwd(), sqlFile)); // Read SQL content const content = (0, fs_1.readFileSync)(filePath, 'utf-8'); // Parse SQL const ast = await (0, pgsql_parser_1.parse)(content); // Clean AST if requested if (argv.clean) { // For now, we'll skip the clean functionality until we can import it properly console.warn(chalk_1.default.yellow('Warning: --clean flag is not yet implemented')); } // Format output const format = argv.format || 'pretty'; const output = format === 'json' ? JSON.stringify(ast) : JSON.stringify(ast, null, 2); // Write output if (argv.output) { (0, fs_1.writeFileSync)(argv.output, output); console.log(chalk_1.default.green(`✓ AST written to ${argv.output}`)); } else { process.stdout.write(output); } } catch (error) { if (error.code === 'ENOENT') { console.error(chalk_1.default.red(`Error: File not found: ${sqlFile}`)); } else { console.error(chalk_1.default.red('Parse error:'), error.message || error); } process.exit(1); } }