@pgsql/cli
Version:
Unified CLI for PostgreSQL AST parsing, deparsing, and code generation
198 lines (175 loc) • 6.9 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.showVersion = showVersion;
exports.showHelp = showHelp;
const chalk_1 = __importDefault(require("chalk"));
const fs_1 = require("fs");
const path_1 = require("path");
function showVersion() {
// Try to find package.json in various locations
const possiblePaths = [
(0, path_1.join)(process.cwd(), 'package.json'),
(0, path_1.join)(process.argv[1], '../../package.json'),
(0, path_1.join)(process.argv[1], '../../../package.json')
];
for (const path of possiblePaths) {
try {
const packageJson = JSON.parse((0, fs_1.readFileSync)(path, 'utf-8'));
if (packageJson.name === '@pgsql/cli') {
console.log(packageJson.version);
return;
}
}
catch {
// Continue to next path
}
}
// Fallback: just show the version we know
console.log('1.29.2');
}
function showHelp(command) {
if (command) {
switch (command) {
case 'parse':
showParseHelp();
break;
case 'deparse':
showDeparseHelp();
break;
case 'proto-gen':
showProtoGenHelp();
break;
case 'proto-fetch':
showProtoFetchHelp();
break;
case 'runtime-schema':
showRuntimeSchemaHelp();
break;
default:
showGeneralHelp();
}
}
else {
showGeneralHelp();
}
}
function showGeneralHelp() {
console.log(`
${chalk_1.default.bold('pgsql')} - Unified CLI for PostgreSQL AST operations
${chalk_1.default.yellow('Usage:')}
pgsql <command> [options]
${chalk_1.default.yellow('Commands:')}
${chalk_1.default.green('parse')} Parse SQL to AST
${chalk_1.default.green('deparse')} Convert AST to SQL
${chalk_1.default.green('proto-gen')} Generate TypeScript from protobuf
${chalk_1.default.green('proto-fetch')} Download and process proto files
${chalk_1.default.green('runtime-schema')} Generate runtime schema for AST nodes
${chalk_1.default.yellow('Global Options:')}
-h, --help Show help
-v, --version Show version
${chalk_1.default.yellow('Examples:')}
pgsql parse query.sql
pgsql deparse ast.json
pgsql proto-gen --inFile pg_query.proto --outDir out
pgsql proto-fetch --url https://example.com/pg_query.proto --inFile pg_query.proto
Run 'pgsql <command> --help' for detailed help on a specific command.
`);
}
function showParseHelp() {
console.log(`
${chalk_1.default.bold('pgsql parse')} - Parse SQL to AST
${chalk_1.default.yellow('Usage:')}
pgsql parse <sqlfile> [options]
${chalk_1.default.yellow('Options:')}
-o, --output <file> Output to file instead of stdout
-f, --format <format> Output format: json, pretty (default: pretty)
--clean Clean the AST tree (remove location info)
-h, --help Show help
${chalk_1.default.yellow('Examples:')}
pgsql parse query.sql
pgsql parse query.sql -o ast.json
pgsql parse query.sql --format json --clean
`);
}
function showDeparseHelp() {
console.log(`
${chalk_1.default.bold('pgsql deparse')} - Convert AST to SQL
${chalk_1.default.yellow('Usage:')}
pgsql deparse [options]
${chalk_1.default.yellow('Options:')}
-i, --input <file> Input JSON file (or use stdin)
-o, --output <file> Output to file instead of stdout
-h, --help Show help
${chalk_1.default.yellow('Examples:')}
pgsql deparse -i ast.json
pgsql deparse -i ast.json -o query.sql
cat ast.json | pgsql deparse
pgsql parse query.sql | pgsql deparse
`);
}
function showProtoGenHelp() {
console.log(`
${chalk_1.default.bold('pgsql proto-gen')} - Generate TypeScript from protobuf
${chalk_1.default.yellow('Usage:')}
pgsql proto-gen --inFile <proto> --outDir <dir> [options]
${chalk_1.default.yellow('Required Options:')}
--inFile <file> Input .proto file
--outDir <dir> Output directory
${chalk_1.default.yellow('Optional Flags:')}
--enums Generate TypeScript enums
--enums-json Generate JSON enum mappings
--types Generate TypeScript interfaces
--utils Generate utility functions
--ast-helpers Generate AST helper methods
--wrapped-helpers Generate wrapped AST helpers
--optional Make all fields optional
--keep-case Keep original field casing
--remove-undefined Remove UNDEFINED enum at position 0
-h, --help Show help
${chalk_1.default.yellow('Examples:')}
pgsql proto-gen --inFile pg_query.proto --outDir out --types --enums
pgsql proto-gen --inFile pg_query.proto --outDir out --types --utils --ast-helpers
pgsql proto-gen --inFile pg_query.proto --outDir out --types --optional --keep-case
`);
}
function showProtoFetchHelp() {
console.log(`
${chalk_1.default.bold('pgsql proto-fetch')} - Download and process proto files
${chalk_1.default.yellow('Usage:')}
pgsql proto-fetch [options]
${chalk_1.default.yellow('Options:')}
--url <url> Proto file URL to download
--inFile <file> Where to save the proto file
--outFile <file> Generated JS output file
--replace-pkg <old> Original package name to replace
--with-pkg <new> New package name
-h, --help Show help
${chalk_1.default.yellow('Examples:')}
pgsql proto-fetch --url https://raw.githubusercontent.com/pganalyze/libpg_query/16-latest/protobuf/pg_query.proto \\
--inFile pg_query.proto \\
--outFile pg_query.js \\
--replace-pkg "protobufjs/minimal" \\
--with-pkg "@launchql/protobufjs/minimal"
`);
}
function showRuntimeSchemaHelp() {
console.log(`
${chalk_1.default.bold('pgsql runtime-schema')} - Generate runtime schema for AST nodes
${chalk_1.default.yellow('Usage:')}
pgsql runtime-schema --inFile <proto> --outDir <dir> [options]
${chalk_1.default.yellow('Required Options:')}
--inFile <file> Input .proto file
--outDir <dir> Output directory
${chalk_1.default.yellow('Optional Options:')}
--format <format> Output format: json, typescript (default: json)
--filename <name> Output filename (default: runtime-schema)
-h, --help Show help
${chalk_1.default.yellow('Examples:')}
pgsql runtime-schema --inFile pg_query.proto --outDir out
pgsql runtime-schema --inFile pg_query.proto --outDir out --format typescript
pgsql runtime-schema --inFile pg_query.proto --outDir out --filename ast-schema
`);
}