1+ import * as fs from 'fs' ;
2+ import * as path from 'path' ;
3+
4+ // Read the versions configuration
5+ const configPath = path . join ( __dirname , '../config/parser-versions.json' ) ;
6+ const config = JSON . parse ( fs . readFileSync ( configPath , 'utf-8' ) ) ;
7+
8+ // Read the package.json template
9+ const templatePath = path . join ( __dirname , '../config/package.template.json' ) ;
10+ const packageTemplate = fs . readFileSync ( templatePath , 'utf-8' ) ;
11+
12+ // Create versions directory if it doesn't exist
13+ const versionsDir = path . join ( __dirname , '../versions' ) ;
14+ if ( ! fs . existsSync ( versionsDir ) ) {
15+ fs . mkdirSync ( versionsDir , { recursive : true } ) ;
16+ }
17+
18+ // Generate version-specific packages
19+ const pgVersions = Object . keys ( config [ 'libpg-query' ] ) ;
20+
21+ pgVersions . forEach ( pgVersion => {
22+ const libpgQueryVersion = config [ 'libpg-query' ] [ pgVersion ] ;
23+
24+ const typesVersion = config [ '@pgsql/types' ] [ pgVersion ] ;
25+
26+ // For pgsql-parser, we only have versions 13 and 17
27+ let pgsqlParserVersion = config [ 'pgsql-parser' ] [ pgVersion ] ;
28+ if ( ! pgsqlParserVersion ) {
29+ // If specific version doesn't exist, skip this PG version
30+ console . log ( `Skipping PG${ pgVersion } - no pgsql-parser version available` ) ;
31+ return ;
32+ }
33+
34+ // Create version directory
35+ const versionDir = path . join ( versionsDir , `${ pgVersion } ` ) ;
36+ if ( ! fs . existsSync ( versionDir ) ) {
37+ fs . mkdirSync ( versionDir , { recursive : true } ) ;
38+ }
39+
40+ // Create src directory
41+ const srcDir = path . join ( versionDir , 'src' ) ;
42+ if ( ! fs . existsSync ( srcDir ) ) {
43+ fs . mkdirSync ( srcDir , { recursive : true } ) ;
44+ }
45+
46+ // Generate package.json
47+ const packageJson = packageTemplate
48+ . replace ( / { { VERSION} } / g, `${ pgVersion } .0.0` )
49+ . replace ( / { { LIBPG_ Q U E R Y _ V E R S I O N } } / g, libpgQueryVersion )
50+ . replace ( / { { PGSQL_ P A R S E R _ V E R S I O N } } / g, pgsqlParserVersion )
51+ . replace ( / { { TYPES_ V E R S I O N } } / g, typesVersion ) ;
52+
53+ fs . writeFileSync (
54+ path . join ( versionDir , 'package.json' ) ,
55+ packageJson
56+ ) ;
57+
58+ // Generate index.ts
59+ const indexContent = `export {
60+ parse as parse,
61+ parseSync as parseSync,
62+ loadModule as loadModule
63+ } from 'libpg-query';
64+
65+ export {
66+ deparse,
67+ deparseSync,
68+ } from 'pgsql-deparser';
69+
70+ export * from '@pgsql/types';
71+ ` ;
72+
73+ fs . writeFileSync (
74+ path . join ( srcDir , 'index.ts' ) ,
75+ indexContent
76+ ) ;
77+
78+ fs . writeFileSync ( path . join ( versionDir , 'tsconfig.json' ) , JSON . stringify ( {
79+ "compilerOptions" : {
80+ "outDir" : "dist" ,
81+ "rootDir" : "src/" ,
82+ "target" : "es2022" ,
83+ "module" : "commonjs" ,
84+ "esModuleInterop" : true ,
85+ "forceConsistentCasingInFileNames" : true ,
86+ "strict" : true ,
87+ "strictNullChecks" : false ,
88+ "skipLibCheck" : true ,
89+ "sourceMap" : false ,
90+ "declaration" : true ,
91+ "resolveJsonModule" : true ,
92+ "moduleResolution" : "node"
93+ } ,
94+ "include" : [
95+ "src/**/*.ts"
96+ ] ,
97+ "exclude" : [
98+ "dist" ,
99+ "node_modules" ,
100+ "**/*.spec.*" ,
101+ "**/*.test.*"
102+ ]
103+ } , null , 2 ) ) ;
104+
105+
106+ fs . writeFileSync ( path . join ( versionDir , 'tsconfig.esm.json' ) , JSON . stringify ( {
107+ "extends" : "./tsconfig.json" ,
108+ "compilerOptions" : {
109+ "outDir" : "dist/esm" ,
110+ "module" : "es2022" ,
111+ "rootDir" : "src/" ,
112+ "declaration" : false
113+ }
114+ } , null , 2 ) ) ;
115+
116+
117+ // Copy the README.md files
118+ fs . copyFileSync (
119+ path . join ( __dirname , '../README.md' ) ,
120+ path . join ( versionDir , 'README.md' )
121+ ) ;
122+
123+ console . log ( `✓ Generated PG${ pgVersion } with libpg-query@${ libpgQueryVersion } and pgsql-parser@${ pgsqlParserVersion } ` ) ;
124+ } ) ;
125+
126+ console . log ( '\nVersion preparation complete!' ) ;
0 commit comments