22import * as path from 'path' ;
33import * as fs from 'fs' ;
44import { sync as globSync } from 'glob' ;
5- import { parse , deparse as deparseSync } from '@pgsql/parser ' ;
5+ import { parse , deparse } from 'libpg-query ' ;
66import { ParseResult , RawStmt } from '@pgsql/types' ;
77
88const FIXTURE_DIR = path . join ( __dirname , '../../../__fixtures__/kitchen-sink' ) ;
@@ -18,32 +18,39 @@ ensureDir(OUT_DIR);
1818
1919const fixtures = globSync ( `${ FIXTURE_DIR } /**/*.sql` ) ;
2020
21- // Collect deparsed SQL in a single JSON
22- const results : Record < string , string > = { } ;
23- fixtures . forEach ( ( fixturePath ) => {
24- const relPath = path . relative ( FIXTURE_DIR , fixturePath ) ;
25- const sql = fs . readFileSync ( fixturePath , 'utf-8' ) ;
26- let parseResult : ParseResult ;
27- try {
28- parseResult = parse ( sql ) ;
29- } catch ( err : any ) {
30- console . error ( `Failed to parse ${ relPath } :` , err ) ;
31- return ;
32- }
33- parseResult . stmts . forEach ( ( stmt : RawStmt , idx : number ) => {
34- let deparsedSql : string ;
21+ async function main ( ) {
22+ // Collect deparsed SQL in a single JSON
23+ const results : Record < string , string > = { } ;
24+
25+ for ( const fixturePath of fixtures ) {
26+ const relPath = path . relative ( FIXTURE_DIR , fixturePath ) ;
27+ const sql = fs . readFileSync ( fixturePath , 'utf-8' ) ;
28+ let parseResult : ParseResult ;
3529 try {
36- deparsedSql = deparseSync ( { version : 170000 , stmts : [ stmt ] } ) ;
30+ parseResult = await parse ( sql ) ;
3731 } catch ( err : any ) {
38- console . error ( `Failed to deparse statement ${ idx + 1 } in ${ relPath } :` , err ) ;
39- return ;
32+ console . error ( `Failed to parse ${ relPath } :` , err ) ;
33+ continue ;
34+ }
35+
36+ for ( let idx = 0 ; idx < parseResult . stmts . length ; idx ++ ) {
37+ const stmt = parseResult . stmts [ idx ] ;
38+ let deparsedSql : string ;
39+ try {
40+ deparsedSql = await deparse ( { version : 170000 , stmts : [ stmt ] } ) ;
41+ } catch ( err : any ) {
42+ console . error ( `Failed to deparse statement ${ idx + 1 } in ${ relPath } :` , err ) ;
43+ continue ;
44+ }
45+ const key = `${ relPath . replace ( / \. s q l $ / , '' ) } -${ idx + 1 } .sql` ;
46+ results [ key ] = deparsedSql ;
4047 }
41- const key = `${ relPath . replace ( / \. s q l $ / , '' ) } -${ idx + 1 } .sql` ;
42- results [ key ] = deparsedSql ;
43- } ) ;
44- } ) ;
48+ }
49+
50+ // Write aggregated JSON to output file
51+ const outputFile = path . join ( OUT_DIR , 'generated.json' ) ;
52+ fs . writeFileSync ( outputFile , JSON . stringify ( results , null , 2 ) ) ;
53+ console . log ( `Wrote JSON to ${ outputFile } ` ) ;
54+ }
4555
46- // Write aggregated JSON to output file
47- const outputFile = path . join ( OUT_DIR , 'generated.json' ) ;
48- fs . writeFileSync ( outputFile , JSON . stringify ( results , null , 2 ) ) ;
49- console . log ( `Wrote JSON to ${ outputFile } ` ) ;
56+ main ( ) . catch ( console . error ) ;
0 commit comments