@pgsql/cli 1.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/LICENSE +21 -0
  3. package/README.md +268 -0
  4. package/dist/LICENSE +21 -0
  5. package/dist/README.md +268 -0
  6. package/dist/commands/deparse.d.ts +1 -0
  7. package/dist/commands/deparse.js +66 -0
  8. package/dist/commands/parse.d.ts +1 -0
  9. package/dist/commands/parse.js +60 -0
  10. package/dist/commands/proto-fetch/cli.d.ts +14 -0
  11. package/dist/commands/proto-fetch/cli.js +41 -0
  12. package/dist/commands/proto-fetch/helpers.d.ts +3 -0
  13. package/dist/commands/proto-fetch/helpers.js +73 -0
  14. package/dist/commands/proto-fetch.d.ts +1 -0
  15. package/dist/commands/proto-fetch.js +42 -0
  16. package/dist/commands/proto-gen.d.ts +1 -0
  17. package/dist/commands/proto-gen.js +59 -0
  18. package/dist/commands/runtime-schema.d.ts +1 -0
  19. package/dist/commands/runtime-schema.js +70 -0
  20. package/dist/esm/commands/deparse.js +60 -0
  21. package/dist/esm/commands/parse.js +54 -0
  22. package/dist/esm/commands/proto-fetch/cli.js +37 -0
  23. package/dist/esm/commands/proto-fetch/helpers.js +64 -0
  24. package/dist/esm/commands/proto-fetch.js +36 -0
  25. package/dist/esm/commands/proto-gen.js +53 -0
  26. package/dist/esm/commands/runtime-schema.js +64 -0
  27. package/dist/esm/index.js +56 -0
  28. package/dist/esm/package.js +26 -0
  29. package/dist/esm/utils/help.js +190 -0
  30. package/dist/index.d.ts +2 -0
  31. package/dist/index.js +61 -0
  32. package/dist/package.d.ts +1 -0
  33. package/dist/package.js +29 -0
  34. package/dist/package.json +60 -0
  35. package/dist/utils/help.d.ts +2 -0
  36. package/dist/utils/help.js +197 -0
  37. package/jest.config.js +18 -0
  38. package/package.json +60 -0
  39. package/src/commands/deparse.ts +60 -0
  40. package/src/commands/parse.ts +60 -0
  41. package/src/commands/proto-fetch/cli.ts +52 -0
  42. package/src/commands/proto-fetch/helpers.ts +72 -0
  43. package/src/commands/proto-fetch.ts +42 -0
  44. package/src/commands/proto-gen.ts +49 -0
  45. package/src/commands/runtime-schema.ts +74 -0
  46. package/src/index.ts +69 -0
  47. package/src/package.ts +33 -0
  48. package/src/utils/help.ts +198 -0
  49. package/tsconfig.esm.json +9 -0
  50. package/tsconfig.json +9 -0
package/src/index.ts ADDED
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env node
2
+ import minimist from 'minimist';
3
+ import chalk from 'chalk';
4
+ import { readFileSync } from 'fs';
5
+ import { resolve } from 'path';
6
+
7
+ import { parseCommand } from './commands/parse';
8
+ import { deparseCommand } from './commands/deparse';
9
+ import { protoGenCommand } from './commands/proto-gen';
10
+ import { protoFetchCommand } from './commands/proto-fetch';
11
+ import { runtimeSchemaCommand } from './commands/runtime-schema';
12
+ import { showHelp, showVersion } from './utils/help';
13
+
14
+ const argv = minimist(process.argv.slice(2), {
15
+ alias: {
16
+ h: 'help',
17
+ v: 'version',
18
+ o: 'output',
19
+ f: 'format'
20
+ }
21
+ });
22
+
23
+ const command = argv._[0];
24
+
25
+ async function main() {
26
+ try {
27
+ if (argv.version) {
28
+ showVersion();
29
+ process.exit(0);
30
+ }
31
+
32
+ if (!command || argv.help) {
33
+ showHelp(command);
34
+ process.exit(0);
35
+ }
36
+
37
+ switch (command) {
38
+ case 'parse':
39
+ await parseCommand(argv);
40
+ break;
41
+
42
+ case 'deparse':
43
+ await deparseCommand(argv);
44
+ break;
45
+
46
+ case 'proto-gen':
47
+ await protoGenCommand(argv);
48
+ break;
49
+
50
+ case 'proto-fetch':
51
+ await protoFetchCommand(argv);
52
+ break;
53
+
54
+ case 'runtime-schema':
55
+ await runtimeSchemaCommand(argv);
56
+ break;
57
+
58
+ default:
59
+ console.error(chalk.red(`Unknown command: ${command}`));
60
+ showHelp();
61
+ process.exit(1);
62
+ }
63
+ } catch (error: any) {
64
+ console.error(chalk.red('Error:'), error.message || error);
65
+ process.exit(1);
66
+ }
67
+ }
68
+
69
+ main();
package/src/package.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { existsSync,readFileSync } from "fs";
2
+ import { dirname,join } from "path";
3
+
4
+ // need to search due to the dist/ folder and src/, etc.
5
+ function findPackageJson(currentDir: string): string {
6
+ const filePath = join(currentDir, 'package.json');
7
+
8
+ // Check if package.json exists in the current directory
9
+ if (existsSync(filePath)) {
10
+ return filePath;
11
+ }
12
+
13
+ // Get the parent directory
14
+ const parentDir = dirname(currentDir);
15
+
16
+ // If reached the root directory, package.json is not found
17
+ if (parentDir === currentDir) {
18
+ throw new Error('package.json not found in any parent directory');
19
+ }
20
+
21
+ // Recursively look in the parent directory
22
+ return findPackageJson(parentDir);
23
+ }
24
+
25
+ export function readAndParsePackageJson(): any {
26
+ // Start searching from the current directory
27
+ const pkgPath = findPackageJson(__dirname);
28
+
29
+ // Read and parse the package.json
30
+ const str = readFileSync(pkgPath, 'utf8');
31
+ const pkg = JSON.parse(str);
32
+ return pkg;
33
+ }
@@ -0,0 +1,198 @@
1
+ import chalk from 'chalk';
2
+ import { readFileSync } from 'fs';
3
+ import { resolve, join } from 'path';
4
+
5
+ export function showVersion() {
6
+ // Try to find package.json in various locations
7
+ const possiblePaths = [
8
+ join(process.cwd(), 'package.json'),
9
+ join(process.argv[1], '../../package.json'),
10
+ join(process.argv[1], '../../../package.json')
11
+ ];
12
+
13
+ for (const path of possiblePaths) {
14
+ try {
15
+ const packageJson = JSON.parse(readFileSync(path, 'utf-8'));
16
+ if (packageJson.name === '@pgsql/cli') {
17
+ console.log(packageJson.version);
18
+ return;
19
+ }
20
+ } catch {
21
+ // Continue to next path
22
+ }
23
+ }
24
+
25
+ // Fallback: just show the version we know
26
+ console.log('1.29.2');
27
+ }
28
+
29
+ export function showHelp(command?: string) {
30
+ if (command) {
31
+ switch (command) {
32
+ case 'parse':
33
+ showParseHelp();
34
+ break;
35
+ case 'deparse':
36
+ showDeparseHelp();
37
+ break;
38
+ case 'proto-gen':
39
+ showProtoGenHelp();
40
+ break;
41
+ case 'proto-fetch':
42
+ showProtoFetchHelp();
43
+ break;
44
+ case 'runtime-schema':
45
+ showRuntimeSchemaHelp();
46
+ break;
47
+ default:
48
+ showGeneralHelp();
49
+ }
50
+ } else {
51
+ showGeneralHelp();
52
+ }
53
+ }
54
+
55
+ function showGeneralHelp() {
56
+ console.log(`
57
+ ${chalk.bold('pgsql')} - Unified CLI for PostgreSQL AST operations
58
+
59
+ ${chalk.yellow('Usage:')}
60
+ pgsql <command> [options]
61
+
62
+ ${chalk.yellow('Commands:')}
63
+ ${chalk.green('parse')} Parse SQL to AST
64
+ ${chalk.green('deparse')} Convert AST to SQL
65
+ ${chalk.green('proto-gen')} Generate TypeScript from protobuf
66
+ ${chalk.green('proto-fetch')} Download and process proto files
67
+ ${chalk.green('runtime-schema')} Generate runtime schema for AST nodes
68
+
69
+ ${chalk.yellow('Global Options:')}
70
+ -h, --help Show help
71
+ -v, --version Show version
72
+
73
+ ${chalk.yellow('Examples:')}
74
+ pgsql parse query.sql
75
+ pgsql deparse ast.json
76
+ pgsql proto-gen --inFile pg_query.proto --outDir out
77
+ pgsql proto-fetch --url https://example.com/pg_query.proto --inFile pg_query.proto
78
+
79
+ Run 'pgsql <command> --help' for detailed help on a specific command.
80
+ `);
81
+ }
82
+
83
+ function showParseHelp() {
84
+ console.log(`
85
+ ${chalk.bold('pgsql parse')} - Parse SQL to AST
86
+
87
+ ${chalk.yellow('Usage:')}
88
+ pgsql parse <sqlfile> [options]
89
+
90
+ ${chalk.yellow('Options:')}
91
+ -o, --output <file> Output to file instead of stdout
92
+ -f, --format <format> Output format: json, pretty (default: pretty)
93
+ --clean Clean the AST tree (remove location info)
94
+ -h, --help Show help
95
+
96
+ ${chalk.yellow('Examples:')}
97
+ pgsql parse query.sql
98
+ pgsql parse query.sql -o ast.json
99
+ pgsql parse query.sql --format json --clean
100
+ `);
101
+ }
102
+
103
+ function showDeparseHelp() {
104
+ console.log(`
105
+ ${chalk.bold('pgsql deparse')} - Convert AST to SQL
106
+
107
+ ${chalk.yellow('Usage:')}
108
+ pgsql deparse [options]
109
+
110
+ ${chalk.yellow('Options:')}
111
+ -i, --input <file> Input JSON file (or use stdin)
112
+ -o, --output <file> Output to file instead of stdout
113
+ -h, --help Show help
114
+
115
+ ${chalk.yellow('Examples:')}
116
+ pgsql deparse -i ast.json
117
+ pgsql deparse -i ast.json -o query.sql
118
+ cat ast.json | pgsql deparse
119
+ pgsql parse query.sql | pgsql deparse
120
+ `);
121
+ }
122
+
123
+ function showProtoGenHelp() {
124
+ console.log(`
125
+ ${chalk.bold('pgsql proto-gen')} - Generate TypeScript from protobuf
126
+
127
+ ${chalk.yellow('Usage:')}
128
+ pgsql proto-gen --inFile <proto> --outDir <dir> [options]
129
+
130
+ ${chalk.yellow('Required Options:')}
131
+ --inFile <file> Input .proto file
132
+ --outDir <dir> Output directory
133
+
134
+ ${chalk.yellow('Optional Flags:')}
135
+ --enums Generate TypeScript enums
136
+ --enums-json Generate JSON enum mappings
137
+ --types Generate TypeScript interfaces
138
+ --utils Generate utility functions
139
+ --ast-helpers Generate AST helper methods
140
+ --wrapped-helpers Generate wrapped AST helpers
141
+ --optional Make all fields optional
142
+ --keep-case Keep original field casing
143
+ --remove-undefined Remove UNDEFINED enum at position 0
144
+ -h, --help Show help
145
+
146
+ ${chalk.yellow('Examples:')}
147
+ pgsql proto-gen --inFile pg_query.proto --outDir out --types --enums
148
+ pgsql proto-gen --inFile pg_query.proto --outDir out --types --utils --ast-helpers
149
+ pgsql proto-gen --inFile pg_query.proto --outDir out --types --optional --keep-case
150
+ `);
151
+ }
152
+
153
+ function showProtoFetchHelp() {
154
+ console.log(`
155
+ ${chalk.bold('pgsql proto-fetch')} - Download and process proto files
156
+
157
+ ${chalk.yellow('Usage:')}
158
+ pgsql proto-fetch [options]
159
+
160
+ ${chalk.yellow('Options:')}
161
+ --url <url> Proto file URL to download
162
+ --inFile <file> Where to save the proto file
163
+ --outFile <file> Generated JS output file
164
+ --replace-pkg <old> Original package name to replace
165
+ --with-pkg <new> New package name
166
+ -h, --help Show help
167
+
168
+ ${chalk.yellow('Examples:')}
169
+ pgsql proto-fetch --url https://raw.githubusercontent.com/pganalyze/libpg_query/16-latest/protobuf/pg_query.proto \\
170
+ --inFile pg_query.proto \\
171
+ --outFile pg_query.js \\
172
+ --replace-pkg "protobufjs/minimal" \\
173
+ --with-pkg "@launchql/protobufjs/minimal"
174
+ `);
175
+ }
176
+
177
+ function showRuntimeSchemaHelp() {
178
+ console.log(`
179
+ ${chalk.bold('pgsql runtime-schema')} - Generate runtime schema for AST nodes
180
+
181
+ ${chalk.yellow('Usage:')}
182
+ pgsql runtime-schema --inFile <proto> --outDir <dir> [options]
183
+
184
+ ${chalk.yellow('Required Options:')}
185
+ --inFile <file> Input .proto file
186
+ --outDir <dir> Output directory
187
+
188
+ ${chalk.yellow('Optional Options:')}
189
+ --format <format> Output format: json, typescript (default: json)
190
+ --filename <name> Output filename (default: runtime-schema)
191
+ -h, --help Show help
192
+
193
+ ${chalk.yellow('Examples:')}
194
+ pgsql runtime-schema --inFile pg_query.proto --outDir out
195
+ pgsql runtime-schema --inFile pg_query.proto --outDir out --format typescript
196
+ pgsql runtime-schema --inFile pg_query.proto --outDir out --filename ast-schema
197
+ `);
198
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist/esm",
5
+ "module": "es2022",
6
+ "rootDir": "src/",
7
+ "declaration": false
8
+ }
9
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src/"
6
+ },
7
+ "include": ["src/**/*.ts"],
8
+ "exclude": ["dist", "node_modules", "**/*.spec.*", "**/*.test.*"]
9
+ }