|
| 1 | +# CLI Consolidation Plan |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Consolidate all CLI functionality from `@launchql/proto-cli`, `pgsql-parser`, and add deparser capabilities into a unified `@pgsql/cli` package. |
| 6 | + |
| 7 | +## Current State |
| 8 | + |
| 9 | +### 1. `@launchql/proto-cli` (packages/cli) |
| 10 | +- **Binary**: `pg-proto-parser` |
| 11 | +- **Commands**: |
| 12 | + - `codegen`: Generate TypeScript from protobuf files |
| 13 | + - `protogen`: Download and process proto files |
| 14 | + - `runtime-schema`: Generate runtime schema for AST nodes |
| 15 | +- **Dependencies**: Uses `inquirerer` for interactive CLI, `minimist` for arg parsing |
| 16 | + |
| 17 | +### 2. `pgsql-parser` (packages/parser) |
| 18 | +- **Binary**: `pgsql-parser` |
| 19 | +- **Functionality**: Parse SQL files and output JSON AST |
| 20 | +- **Options**: |
| 21 | + - `--pl`: Parse PL/pgSQL functions only |
| 22 | +- **Dependencies**: Uses `minimist` directly in cli.js |
| 23 | + |
| 24 | +### 3. `deparser` (packages/deparser) |
| 25 | +- **No CLI**: Currently no CLI interface |
| 26 | +- **Functionality**: Convert AST back to SQL |
| 27 | + |
| 28 | +## New CLI Structure: `@pgsql/cli` |
| 29 | + |
| 30 | +### Package Changes |
| 31 | + |
| 32 | +1. **Rename**: `@launchql/proto-cli` → `@pgsql/cli` |
| 33 | +2. **Binary**: `pgsql` (shorter, cleaner) |
| 34 | +3. **Remove minimist** from parser and deparser packages |
| 35 | +4. **Centralize** all CLI logic in the cli package |
| 36 | + |
| 37 | +### Command Structure |
| 38 | + |
| 39 | +``` |
| 40 | +pgsql <command> [options] |
| 41 | +
|
| 42 | +Commands: |
| 43 | + parse Parse SQL to AST |
| 44 | + deparse Convert AST to SQL |
| 45 | + proto-gen Generate TypeScript from protobuf (formerly codegen) |
| 46 | + proto-fetch Download and process proto files (formerly protogen) |
| 47 | + runtime-schema Generate runtime schema for AST nodes |
| 48 | +
|
| 49 | +Global Options: |
| 50 | + -h, --help Show help |
| 51 | + -v, --version Show version |
| 52 | +``` |
| 53 | + |
| 54 | +### Command Details |
| 55 | + |
| 56 | +#### 1. `pgsql parse` |
| 57 | +```bash |
| 58 | +pgsql parse <sqlfile> [options] |
| 59 | + |
| 60 | +Options: |
| 61 | + -o, --output <file> Output to file instead of stdout |
| 62 | + -f, --format <format> Output format: json, pretty (default: pretty) |
| 63 | + --pl Parse as PL/pgSQL function only |
| 64 | + --clean Clean the AST tree (remove location info) |
| 65 | + -h, --help Show help |
| 66 | +``` |
| 67 | + |
| 68 | +#### 2. `pgsql deparse` |
| 69 | +```bash |
| 70 | +pgsql deparse <jsonfile> [options] |
| 71 | + |
| 72 | +Options: |
| 73 | + -o, --output <file> Output to file instead of stdout |
| 74 | + -i, --input <file> Input JSON file (or use stdin) |
| 75 | + --format <format> SQL formatting options |
| 76 | + -h, --help Show help |
| 77 | +``` |
| 78 | + |
| 79 | +#### 3. `pgsql proto-gen` |
| 80 | +```bash |
| 81 | +pgsql proto-gen --inFile <proto> --outDir <dir> [options] |
| 82 | + |
| 83 | +Options: |
| 84 | + --inFile <file> Input .proto file (required) |
| 85 | + --outDir <dir> Output directory (required) |
| 86 | + --enums Generate TypeScript enums |
| 87 | + --enums-json Generate JSON enum mappings |
| 88 | + --types Generate TypeScript interfaces |
| 89 | + --utils Generate utility functions |
| 90 | + --ast-helpers Generate AST helper methods |
| 91 | + --wrapped-helpers Generate wrapped AST helpers |
| 92 | + --optional Make all fields optional |
| 93 | + --keep-case Keep original field casing |
| 94 | + --remove-undefined Remove UNDEFINED enum at position 0 |
| 95 | + -h, --help Show help |
| 96 | +``` |
| 97 | + |
| 98 | +#### 4. `pgsql proto-fetch` |
| 99 | +```bash |
| 100 | +pgsql proto-fetch [options] |
| 101 | + |
| 102 | +Options: |
| 103 | + --url <url> Proto file URL to download |
| 104 | + --inFile <file> Where to save the proto file |
| 105 | + --outFile <file> Generated JS output file |
| 106 | + --replace-pkg <old> Original package name to replace |
| 107 | + --with-pkg <new> New package name |
| 108 | + -h, --help Show help |
| 109 | +``` |
| 110 | + |
| 111 | +#### 5. `pgsql runtime-schema` |
| 112 | +```bash |
| 113 | +pgsql runtime-schema --inFile <proto> --outDir <dir> [options] |
| 114 | + |
| 115 | +Options: |
| 116 | + --inFile <file> Input .proto file (required) |
| 117 | + --outDir <dir> Output directory (required) |
| 118 | + --format <format> Output format: json, typescript (default: json) |
| 119 | + --filename <name> Output filename (default: runtime-schema) |
| 120 | + -h, --help Show help |
| 121 | +``` |
| 122 | + |
| 123 | +## Implementation Steps |
| 124 | + |
| 125 | +### Phase 1: Setup New Package |
| 126 | +1. Copy `packages/cli` to new location (keep history) |
| 127 | +2. Update package.json: |
| 128 | + - Name: `@pgsql/cli` |
| 129 | + - Binary: `pgsql` |
| 130 | + - Update dependencies |
| 131 | +3. Remove `minimist` dependency from parser/deparser packages |
| 132 | + |
| 133 | +### Phase 2: Implement Core Commands |
| 134 | +1. Create new command structure without `inquirerer` for non-interactive mode |
| 135 | +2. Implement `parse` command: |
| 136 | + - Move logic from parser/src/cli.js |
| 137 | + - Add output options |
| 138 | + - Add format options |
| 139 | +3. Implement `deparse` command: |
| 140 | + - Import deparser functionality |
| 141 | + - Add file I/O handling |
| 142 | + - Add formatting options |
| 143 | + |
| 144 | +### Phase 3: Refactor Proto Commands |
| 145 | +1. Rename `codegen` → `proto-gen` |
| 146 | +2. Rename `protogen` → `proto-fetch` |
| 147 | +3. Update command options to use consistent naming |
| 148 | +4. Add new options for wrapped helpers |
| 149 | + |
| 150 | +### Phase 4: Improve Help System |
| 151 | +1. Create detailed help for each command |
| 152 | +2. Add examples to help text |
| 153 | +3. Create man page style documentation |
| 154 | + |
| 155 | +### Phase 5: Testing & Documentation |
| 156 | +1. Add comprehensive tests for all commands |
| 157 | +2. Update README with new command structure |
| 158 | +3. Create migration guide from old CLIs |
| 159 | +4. Add shell completion scripts |
| 160 | + |
| 161 | +## Benefits |
| 162 | + |
| 163 | +1. **Single Entry Point**: One CLI tool for all PostgreSQL AST operations |
| 164 | +2. **Consistent Interface**: Unified command structure and options |
| 165 | +3. **Better Discoverability**: Clear command hierarchy with good help |
| 166 | +4. **Reduced Dependencies**: Remove minimist from individual packages |
| 167 | +5. **Extensibility**: Easy to add new commands in the future |
| 168 | + |
| 169 | +## Migration Guide |
| 170 | + |
| 171 | +### For `pgsql-parser` users: |
| 172 | +```bash |
| 173 | +# Old |
| 174 | +pgsql-parser file.sql |
| 175 | + |
| 176 | +# New |
| 177 | +pgsql parse file.sql |
| 178 | +``` |
| 179 | + |
| 180 | +### For `pg-proto-parser` users: |
| 181 | +```bash |
| 182 | +# Old |
| 183 | +pg-proto-parser codegen --inFile pg_query.proto --outDir out |
| 184 | + |
| 185 | +# New |
| 186 | +pgsql proto-gen --inFile pg_query.proto --outDir out |
| 187 | +``` |
| 188 | + |
| 189 | +## Future Enhancements |
| 190 | + |
| 191 | +1. **Interactive Mode**: Keep inquirerer for interactive command selection |
| 192 | +2. **Config Files**: Support `.pgsqlrc` configuration files |
| 193 | +3. **Plugins**: Allow extending with custom commands |
| 194 | +4. **Watch Mode**: Auto-regenerate on file changes |
| 195 | +5. **Batch Processing**: Process multiple files at once |
0 commit comments