33const fs = require ( 'fs' ) ;
44const path = require ( 'path' ) ;
55const { execSync } = require ( 'child_process' ) ;
6+ const readline = require ( 'readline' ) ;
67
7- const pkgPath = path . join ( process . cwd ( ) , 'package.json' ) ;
8+ const rl = readline . createInterface ( {
9+ input : process . stdin ,
10+ output : process . stdout
11+ } ) ;
812
9- if ( ! fs . existsSync ( pkgPath ) ) {
10- console . error ( '❌ No package.json found in current directory.' ) ;
11- process . exit ( 1 ) ;
12- }
13+ const question = ( query ) => new Promise ( ( resolve ) => rl . question ( query , resolve ) ) ;
1314
14- const original = JSON . parse ( fs . readFileSync ( pkgPath , 'utf8' ) ) ;
15- const publishMeta = original [ 'x-publish' ] || { } ;
15+ async function main ( ) {
16+ console . log ( '🚀 Version Packages Publishing Tool\n' ) ;
1617
17- const publishName = publishMeta . publishName || 'libpg-query' ;
18- const distTag = process . env . TAG || publishMeta . distTag || 'latest' ;
18+ // Check for uncommitted changes
19+ try {
20+ execSync ( 'git diff --quiet && git diff --cached --quiet' ) ;
21+ } catch ( error ) {
22+ console . error ( '❌ You have uncommitted changes. Please commit or stash them first.' ) ;
23+ process . exit ( 1 ) ;
24+ }
1925
20- if ( ! original . name || ! original . version ) {
21- console . error ( '❌ package.json must include name and version' ) ;
22- process . exit ( 1 ) ;
23- }
26+ // Get all version directories
27+ const versionsDir = path . join ( __dirname , '..' , 'versions' ) ;
28+ const versionDirs = fs . readdirSync ( versionsDir )
29+ . filter ( dir => / ^ \d + $ / . test ( dir ) )
30+ . sort ( ( a , b ) => parseInt ( b ) - parseInt ( a ) ) ; // Sort descending
31+
32+ // Also check for full package
33+ const fullPackagePath = path . join ( __dirname , '..' , 'full' , 'package.json' ) ;
34+ const hasFullPackage = fs . existsSync ( fullPackagePath ) ;
35+
36+ console . log ( '📦 Available packages:' ) ;
37+ versionDirs . forEach ( v => console . log ( ` - PostgreSQL ${ v } (versions/${ v } )` ) ) ;
38+ if ( hasFullPackage ) {
39+ console . log ( ` - Full package (./full) - PostgreSQL 17` ) ;
40+ }
41+ console . log ( ) ;
42+
43+ // Ask which versions to publish
44+ const publishAll = await question ( 'Publish all packages? (y/N): ' ) ;
45+ let selectedVersions = [ ] ;
46+ let includeFullPackage = false ;
47+
48+ if ( publishAll . toLowerCase ( ) === 'y' ) {
49+ selectedVersions = versionDirs ;
50+ includeFullPackage = hasFullPackage ;
51+ } else {
52+ // Let user select versions
53+ for ( const version of versionDirs ) {
54+ const publish = await question ( `Publish PostgreSQL ${ version } ? (y/N): ` ) ;
55+ if ( publish . toLowerCase ( ) === 'y' ) {
56+ selectedVersions . push ( version ) ;
57+ }
58+ }
59+
60+ if ( hasFullPackage ) {
61+ const publishFull = await question ( `Publish full package (PostgreSQL 17)? (y/N): ` ) ;
62+ includeFullPackage = publishFull . toLowerCase ( ) === 'y' ;
63+ }
64+ }
65+
66+ if ( selectedVersions . length === 0 && ! includeFullPackage ) {
67+ console . log ( '\n❌ No packages selected for publishing.' ) ;
68+ rl . close ( ) ;
69+ return ;
70+ }
2471
25- const modified = { ...original , name : publishName } ;
26-
27- try {
28- console . log ( `📦 Publishing ${ publishName } @${ original . version } with tag '${ distTag } '...` ) ;
29- fs . writeFileSync ( pkgPath , JSON . stringify ( modified , null , 2 ) ) ;
30- execSync ( `npm publish --tag ${ distTag } ` , { stdio : 'inherit' } ) ;
31- console . log ( '✅ Publish complete.' ) ;
32- } catch ( err ) {
33- console . error ( '❌ Publish failed:' , err . message ) ;
34- } finally {
35- fs . writeFileSync ( pkgPath , JSON . stringify ( original , null , 2 ) ) ;
36- console . log ( '🔄 Restored original package.json' ) ;
72+ // Ask for version bump type
73+ console . log ( '\n📈 Version bump type:' ) ;
74+ console . log ( ' 1. patch (0.0.x)' ) ;
75+ console . log ( ' 2. minor (0.x.0)' ) ;
76+ const bumpType = await question ( 'Select bump type (1 or 2): ' ) ;
77+ const bump = bumpType === '2' ? 'minor' : 'patch' ;
78+
79+ console . log ( `\n📋 Will publish:` ) ;
80+ selectedVersions . forEach ( v => console . log ( ` - PostgreSQL ${ v } (${ bump } bump)` ) ) ;
81+ if ( includeFullPackage ) {
82+ console . log ( ` - Full package (${ bump } bump)` ) ;
83+ }
84+
85+ const confirm = await question ( '\nProceed? (y/N): ' ) ;
86+ if ( confirm . toLowerCase ( ) !== 'y' ) {
87+ console . log ( '❌ Publishing cancelled.' ) ;
88+ rl . close ( ) ;
89+ return ;
90+ }
91+
92+ console . log ( '\n🔨 Starting publish process...\n' ) ;
93+
94+ // Process each selected version
95+ for ( const version of selectedVersions ) {
96+ console . log ( `\n📦 Publishing PostgreSQL ${ version } ...` ) ;
97+ const versionPath = path . join ( versionsDir , version ) ;
98+
99+ try {
100+ // Version bump
101+ console . log ( ` 📝 Bumping version (${ bump } )...` ) ;
102+ execSync ( `pnpm version ${ bump } ` , { cwd : versionPath , stdio : 'inherit' } ) ;
103+
104+ // Commit
105+ console . log ( ` 💾 Committing version bump...` ) ;
106+ execSync ( `git add package.json` , { cwd : versionPath } ) ;
107+ execSync ( `git commit -m "release: bump libpg-query v${ version } version"` , { stdio : 'inherit' } ) ;
108+
109+ // Build
110+ console . log ( ` 🔨 Building...` ) ;
111+ execSync ( 'pnpm build' , { cwd : versionPath , stdio : 'inherit' } ) ;
112+
113+ // Test
114+ console . log ( ` 🧪 Running tests...` ) ;
115+ execSync ( 'pnpm test' , { cwd : versionPath , stdio : 'inherit' } ) ;
116+
117+ // Publish
118+ console . log ( ` 📤 Publishing to npm...` ) ;
119+ execSync ( 'pnpm run publish:pkg' , { cwd : versionPath , stdio : 'inherit' } ) ;
120+
121+ console . log ( ` ✅ PostgreSQL ${ version } published successfully!` ) ;
122+ } catch ( error ) {
123+ console . error ( ` ❌ Failed to publish PostgreSQL ${ version } :` , error . message ) ;
124+ const continuePublish = await question ( 'Continue with other versions? (y/N): ' ) ;
125+ if ( continuePublish . toLowerCase ( ) !== 'y' ) {
126+ rl . close ( ) ;
127+ process . exit ( 1 ) ;
128+ }
129+ }
130+ }
131+
132+ // Process full package if selected
133+ if ( includeFullPackage ) {
134+ console . log ( `\n📦 Publishing full package...` ) ;
135+ const fullPath = path . join ( __dirname , '..' , 'full' ) ;
136+
137+ try {
138+ // Version bump
139+ console . log ( ` 📝 Bumping version (${ bump } )...` ) ;
140+ execSync ( `pnpm version ${ bump } ` , { cwd : fullPath , stdio : 'inherit' } ) ;
141+
142+ // Commit
143+ console . log ( ` 💾 Committing version bump...` ) ;
144+ execSync ( `git add package.json` , { cwd : fullPath } ) ;
145+ execSync ( `git commit -m "release: bump @libpg-query/parser version"` , { stdio : 'inherit' } ) ;
146+
147+ // Build
148+ console . log ( ` 🔨 Building...` ) ;
149+ execSync ( 'pnpm build' , { cwd : fullPath , stdio : 'inherit' } ) ;
150+
151+ // Test
152+ console . log ( ` 🧪 Running tests...` ) ;
153+ execSync ( 'pnpm test' , { cwd : fullPath , stdio : 'inherit' } ) ;
154+
155+ // Publish with pg17 tag
156+ console . log ( ` 📤 Publishing to npm with pg17 tag...` ) ;
157+ execSync ( 'pnpm publish --tag pg17' , { cwd : fullPath , stdio : 'inherit' } ) ;
158+
159+ console . log ( ` ✅ Full package published successfully with pg17 tag!` ) ;
160+ } catch ( error ) {
161+ console . error ( ` ❌ Failed to publish full package:` , error . message ) ;
162+ }
163+ }
164+
165+ // Ask about promoting to latest
166+ if ( selectedVersions . includes ( '17' ) || includeFullPackage ) {
167+ console . log ( '\n🏷️ Tag Management' ) ;
168+
169+ if ( selectedVersions . includes ( '17' ) ) {
170+ const promoteVersions = await question ( 'Promote libpg-query@pg17 to latest? (y/N): ' ) ;
171+ if ( promoteVersions . toLowerCase ( ) === 'y' ) {
172+ try {
173+ execSync ( 'npm dist-tag add libpg-query@pg17 latest' , { stdio : 'inherit' } ) ;
174+ console . log ( '✅ libpg-query@pg17 promoted to latest' ) ;
175+ } catch ( error ) {
176+ console . error ( '❌ Failed to promote tag:' , error . message ) ;
177+ }
178+ }
179+ }
180+
181+ if ( includeFullPackage ) {
182+ const promoteFullPackage = await question ( 'Promote @libpg-query/parser@pg17 to latest? (y/N): ' ) ;
183+ if ( promoteFullPackage . toLowerCase ( ) === 'y' ) {
184+ try {
185+ execSync ( 'npm dist-tag add @libpg-query/parser@pg17 latest' , { stdio : 'inherit' } ) ;
186+ console . log ( '✅ @libpg-query/parser@pg17 promoted to latest' ) ;
187+ } catch ( error ) {
188+ console . error ( '❌ Failed to promote tag:' , error . message ) ;
189+ }
190+ }
191+ }
192+ }
193+
194+ console . log ( '\n✨ Publishing complete!' ) ;
195+ rl . close ( ) ;
37196}
197+
198+ main ( ) . catch ( error => {
199+ console . error ( '❌ Error:' , error ) ;
200+ rl . close ( ) ;
201+ process . exit ( 1 ) ;
202+ } ) ;
0 commit comments