1+ import * as fs from 'fs' ;
2+ import * as path from 'path' ;
3+
4+ /**
5+ * Script to generate package.json files for each version directory
6+ * Uses version-config.json to map versions to their dependencies
7+ */
8+
9+ const VERSIONS_DIR = 'versions' ;
10+ const CONFIG_FILE = 'config/deparser-versions.json' ;
11+
12+ interface VersionConfig {
13+ deparserVersion : string ;
14+ typesVersion : string ;
15+ pgVersion : string ;
16+ npmTag : string ;
17+ }
18+
19+ interface Config {
20+ packageName : string ;
21+ versions : Record < string , VersionConfig > ;
22+ packageTemplate : Record < string , any > ;
23+ }
24+
25+ function loadConfig ( ) : Config {
26+ const configPath = path . join ( process . cwd ( ) , CONFIG_FILE ) ;
27+ const configContent = fs . readFileSync ( configPath , 'utf-8' ) ;
28+ return JSON . parse ( configContent ) ;
29+ }
30+
31+ function generatePackageJson ( packageName : string , version : string , versionConfig : VersionConfig , template : Record < string , any > ) : any {
32+ return {
33+ name : packageName ,
34+ version : versionConfig . deparserVersion ,
35+ description : `PostgreSQL v${ version } AST Deparser - Transforms v${ version } ASTs to v17 and deparses them` ,
36+ main : "index.js" ,
37+ module : "esm/index.js" ,
38+ types : "index.d.ts" ,
39+ ...template ,
40+ scripts : {
41+ "copy" : "copyfiles -f ../../LICENSE README.md package.json dist" ,
42+ "clean" : "rimraf dist" ,
43+ "prepare" : "npm run build" ,
44+ "build" : "npm run clean && tsc && tsc -p tsconfig.esm.json && npm run copy"
45+ } ,
46+ dependencies : {
47+ [ `@pgsql/types` ] : `^${ versionConfig . typesVersion } `
48+ } ,
49+ keywords : [
50+ ...template . keywords ,
51+ `v${ version } ` ,
52+ `postgresql-${ version } `
53+ ]
54+ } ;
55+ }
56+
57+ function generateTsConfig ( ) : any {
58+ return {
59+ "compilerOptions" : {
60+ "target" : "es2018" ,
61+ "module" : "commonjs" ,
62+ "lib" : [ "es2018" ] ,
63+ "declaration" : true ,
64+ "outDir" : "./dist" ,
65+ "rootDir" : "./" ,
66+ "strict" : false ,
67+ "esModuleInterop" : true ,
68+ "skipLibCheck" : true ,
69+ "forceConsistentCasingInFileNames" : true ,
70+ "moduleResolution" : "node" ,
71+ "resolveJsonModule" : true
72+ } ,
73+ "include" : [
74+ "**/*.ts"
75+ ] ,
76+ "exclude" : [
77+ "node_modules" ,
78+ "dist" ,
79+ "**/*.test.ts"
80+ ]
81+ } ;
82+ }
83+
84+ function generateTsConfigEsm ( ) : any {
85+ return {
86+ "extends" : "./tsconfig.json" ,
87+ "compilerOptions" : {
88+ "module" : "esnext" ,
89+ "outDir" : "./dist/esm" ,
90+ "declaration" : false
91+ }
92+ } ;
93+ }
94+
95+ function generateVersionPackages ( ) : void {
96+ console . log ( 'Generating package.json files for each version...\n' ) ;
97+
98+ const config = loadConfig ( ) ;
99+
100+ for ( const [ version , versionConfig ] of Object . entries ( config . versions ) ) {
101+ console . log ( `Processing version ${ version } ...` ) ;
102+
103+ const versionDir = path . join ( VERSIONS_DIR , version ) ;
104+
105+ if ( ! fs . existsSync ( versionDir ) ) {
106+ console . error ( ` ✗ Version directory ${ versionDir } does not exist!` ) ;
107+ continue ;
108+ }
109+
110+ // Generate package.json
111+ const packageJson = generatePackageJson ( config . packageName , version , versionConfig , config . packageTemplate ) ;
112+ const packageJsonPath = path . join ( versionDir , 'package.json' ) ;
113+ fs . writeFileSync ( packageJsonPath , JSON . stringify ( packageJson , null , 2 ) ) ;
114+ console . log ( ` ✓ Created package.json` ) ;
115+
116+ // Generate tsconfig.json
117+ const tsConfig = generateTsConfig ( ) ;
118+ const tsConfigPath = path . join ( versionDir , 'tsconfig.json' ) ;
119+ fs . writeFileSync ( tsConfigPath , JSON . stringify ( tsConfig , null , 2 ) ) ;
120+ console . log ( ` ✓ Created tsconfig.json` ) ;
121+
122+ // Generate tsconfig.esm.json
123+ const tsConfigEsm = generateTsConfigEsm ( ) ;
124+ const tsConfigEsmPath = path . join ( versionDir , 'tsconfig.esm.json' ) ;
125+ fs . writeFileSync ( tsConfigEsmPath , JSON . stringify ( tsConfigEsm , null , 2 ) ) ;
126+ console . log ( ` ✓ Created tsconfig.esm.json` ) ;
127+
128+ // Update README with package name and npm tag
129+ updateReadmeWithPackageName ( version , config . packageName , versionConfig . npmTag ) ;
130+
131+ console . log ( '' ) ;
132+ }
133+
134+ console . log ( 'Done! Package files have been generated for all versions.' ) ;
135+ }
136+
137+ function updateReadmeWithPackageName ( version : string , packageName : string , npmTag : string ) : void {
138+ const versionDir = path . join ( VERSIONS_DIR , version ) ;
139+ const readmePath = path . join ( versionDir , 'README.md' ) ;
140+
141+ if ( ! fs . existsSync ( readmePath ) ) {
142+ return ;
143+ }
144+
145+ let content = fs . readFileSync ( readmePath , 'utf-8' ) ;
146+
147+ // Remove any existing installation sections
148+ content = content . replace ( / # # I n s t a l l a t i o n [ \s \S ] * ?(? = # # | $ ) / gm, '' ) ;
149+
150+ // Add installation instructions at the beginning
151+ const installSection = `## Installation
152+
153+ \`\`\`bash
154+ # Install specific version using npm tag
155+ npm install ${ packageName } @${ npmTag }
156+
157+ # Or using yarn
158+ yarn add ${ packageName } @${ npmTag }
159+ \`\`\`
160+
161+ ` ;
162+
163+ // Insert after the title
164+ const lines = content . split ( '\n' ) ;
165+ const titleIndex = lines . findIndex ( line => line . startsWith ( '# ' ) ) ;
166+ if ( titleIndex !== - 1 ) {
167+ // Find the next non-empty line after title
168+ let insertIndex = titleIndex + 1 ;
169+ while ( insertIndex < lines . length && lines [ insertIndex ] . trim ( ) === '' ) {
170+ insertIndex ++ ;
171+ }
172+ lines . splice ( insertIndex , 0 , installSection ) ;
173+ content = lines . join ( '\n' ) ;
174+ }
175+
176+ // Update import examples
177+ content = content . replace ( / f r o m ' \. \/ i n d e x ' / g, `from '${ packageName } '` ) ;
178+ content = content . replace ( / f r o m ' p g s q l - d e p a r s e r - v \d + ' / g, `from '${ packageName } '` ) ;
179+
180+ fs . writeFileSync ( readmePath , content ) ;
181+ console . log ( ` ✓ Updated README.md with package name` ) ;
182+ }
183+
184+ // Run the script
185+ generateVersionPackages ( ) ;
0 commit comments