1+ import * as fs from 'fs' ;
2+ import * as path from 'path' ;
3+
4+ /**
5+ * Script to generate deparser index files for each version
6+ * Creates index.ts files that use transformers to provide deparse functionality
7+ */
8+
9+ interface VersionConfig {
10+ version : number ;
11+ directTransformerClass : string ;
12+ directTransformerFile : string ;
13+ }
14+
15+ const VERSIONS_DIR = 'versions' ;
16+
17+ const versionConfigs : VersionConfig [ ] = [
18+ {
19+ version : 13 ,
20+ directTransformerClass : 'PG13ToPG17Transformer' ,
21+ directTransformerFile : './v13-to-v17-direct'
22+ } ,
23+ {
24+ version : 14 ,
25+ directTransformerClass : 'PG14ToPG17Transformer' ,
26+ directTransformerFile : './v14-to-v17-direct'
27+ } ,
28+ {
29+ version : 15 ,
30+ directTransformerClass : 'PG15ToPG17Transformer' ,
31+ directTransformerFile : './v15-to-v17-direct'
32+ } ,
33+ {
34+ version : 16 ,
35+ directTransformerClass : 'PG16ToPG17Transformer' ,
36+ directTransformerFile : './v16-to-v17-direct'
37+ }
38+ ] ;
39+
40+ function generateDeparserIndex ( config : VersionConfig ) : string {
41+ return `/**
42+ * Deparser for PostgreSQL version ${ config . version }
43+ * Auto-generated by generate-version-deparsers.ts
44+ */
45+
46+ import { Node, ParseResult } from '@pgsql/types';
47+ import {
48+ deparse as deparse17,
49+ deparseSync as deparseSync17,
50+ DeparserOptions
51+ } from './deparser';
52+ import { ${ config . directTransformerClass } } from '${ config . directTransformerFile } ';
53+
54+ const tx = new ${ config . directTransformerClass } ();
55+
56+ export async function deparse(query: Node | Node[] | ParseResult, opts?: DeparserOptions): Promise<string> {
57+ const ast17 = tx.transform(query);
58+ return await deparse17(ast17, opts);
59+ }
60+
61+ export function deparseSync(query: Node | Node[] | ParseResult, opts?: DeparserOptions): string {
62+ const ast17 = tx.transform(query);
63+ return deparseSync17(ast17, opts);
64+ }
65+
66+ // Re-export DeparserOptions for convenience
67+ export { DeparserOptions } from './deparser';
68+ ` ;
69+ }
70+
71+ function updateVersionDirectory ( config : VersionConfig ) : void {
72+ const versionDir = path . join ( VERSIONS_DIR , config . version . toString ( ) ) ;
73+
74+ if ( ! fs . existsSync ( versionDir ) ) {
75+ console . error ( `Version directory ${ versionDir } does not exist!` ) ;
76+ return ;
77+ }
78+
79+ // Create src directory if it doesn't exist
80+ const srcDir = path . join ( versionDir , 'src' ) ;
81+ if ( ! fs . existsSync ( srcDir ) ) {
82+ fs . mkdirSync ( srcDir , { recursive : true } ) ;
83+ }
84+
85+ // Remove old index.js if it exists
86+ const oldIndexPath = path . join ( srcDir , 'index.js' ) ;
87+ if ( fs . existsSync ( oldIndexPath ) ) {
88+ fs . unlinkSync ( oldIndexPath ) ;
89+ console . log ( ` ✓ Removed old index.js` ) ;
90+ }
91+
92+ // Generate new index.ts in src directory
93+ const indexContent = generateDeparserIndex ( config ) ;
94+ const indexPath = path . join ( srcDir , 'index.ts' ) ;
95+ fs . writeFileSync ( indexPath , indexContent ) ;
96+ console . log ( ` ✓ Created index.ts with deparser functionality` ) ;
97+ }
98+
99+
100+
101+ function main ( ) : void {
102+ console . log ( 'Generating version-specific deparsers...\n' ) ;
103+
104+ for ( const config of versionConfigs ) {
105+ console . log ( `Processing version ${ config . version } ...` ) ;
106+ updateVersionDirectory ( config ) ;
107+
108+ console . log ( '' ) ;
109+ }
110+
111+ console . log ( 'Done! Version-specific deparsers have been generated.' ) ;
112+ }
113+
114+ // Run the script
115+ main ( ) ;
0 commit comments