@@ -17,6 +17,7 @@ import { pipe } from 'fp-ts/lib/pipeable'
1717import * as yargs from 'yargs'
1818
1919import { Clients , connect , disconnect } from './clients'
20+ import { CodegenTarget , codegenTargets } from './codegen'
2021import {
2122 sqlToStatementDescription ,
2223 generateTSCode ,
@@ -31,7 +32,8 @@ type Options = {
3132 verbose : boolean
3233 index : boolean
3334 prettify : boolean
34- pgModule : string
35+ target : CodegenTarget
36+ module : string
3537 terminalColumns : number | undefined
3638}
3739
@@ -51,7 +53,8 @@ async function main(): Promise<number> {
5153 verbose : args . verbose ,
5254 index : args . index ,
5355 prettify : args . prettify ,
54- pgModule : args [ 'pg-module' ] ,
56+ target : args . target ,
57+ module : args . module ?? args [ 'pg-module' ] ?? args . target ,
5558 terminalColumns : process . stdout . columns ,
5659 }
5760
@@ -68,7 +71,7 @@ async function main(): Promise<number> {
6871 const clients = await connect ( args . database )
6972 if ( Either . isLeft ( clients ) ) {
7073 console . error ( clients . left )
71- throw process . exit ( 1 )
74+ return 1
7275 }
7376
7477 let status = 0
@@ -87,7 +90,13 @@ Some files are out of date!`)
8790 status = 1
8891 }
8992 } else {
90- await processDirectories ( clients . right , fileExtensions , dirPaths , options ) ( )
93+ const moduleDirs = await processDirectories (
94+ clients . right ,
95+ fileExtensions ,
96+ dirPaths ,
97+ options
98+ ) ( )
99+ if ( moduleDirs . some ( moduleDir => moduleDir . hasErrors ) ) status = 1
91100 }
92101
93102 await disconnect ( clients . right )
@@ -130,6 +139,18 @@ function parseArgs() {
130139 type : 'boolean' ,
131140 default : false ,
132141 } )
142+ . option ( 'target' , {
143+ alias : 't' ,
144+ description :
145+ 'Postgres client library to use in generated TypeScript code' ,
146+ choices : codegenTargets ,
147+ default : codegenTargets [ 0 ] ,
148+ } )
149+ . option ( 'module' , {
150+ alias : 'm' ,
151+ description : 'Where to import node-postgres or postgres.js from.' ,
152+ type : 'string' ,
153+ } )
133154 . option ( 'check' , {
134155 alias : 'c' ,
135156 description :
@@ -146,9 +167,9 @@ function parseArgs() {
146167 default : false ,
147168 } )
148169 . option ( 'pg-module' , {
149- description : 'Where to import node-postgres from.' ,
170+ description :
171+ 'Where to import node-postgres from. (deprecated, use --module instead)' ,
150172 type : 'string' ,
151- default : 'pg' ,
152173 } )
153174 . epilogue (
154175 `\
@@ -246,7 +267,11 @@ async function handleWatchEvents(
246267 moduleDirs = pipe (
247268 modifyWhere (
248269 moduleDir => moduleDir . dirPath === dirPath ,
249- moduleDir => ( { dirPath : moduleDir . dirPath , modules : newModules } ) ,
270+ moduleDir => ( {
271+ dirPath : moduleDir . dirPath ,
272+ modules : newModules ,
273+ hasErrors : moduleDir . hasErrors ,
274+ } ) ,
250275 moduleDirs
251276 ) ,
252277 Option . getOrElse ( ( ) => moduleDirs )
@@ -362,9 +387,13 @@ function processDirectories(
362387 ( dirPath , tsModules ) => processSQLDirectory ( dirPath , tsModules , options )
363388 )
364389 ) ,
365- Task . map ( result => {
366- console . log ( 'done.' )
367- return result
390+ Task . map ( moduleDirs => {
391+ if ( moduleDirs . some ( moduleDir => moduleDir . hasErrors ) ) {
392+ console . log ( 'Compilation failed.' )
393+ } else {
394+ console . log ( 'done.' )
395+ }
396+ return moduleDirs
368397 } )
369398 )
370399}
@@ -436,7 +465,8 @@ function processSQLFile(
436465 TaskEither . chain ( source =>
437466 generateTSCode ( clients , path . basename ( filePath ) , source , fnName , {
438467 prettierFileName : options . prettify ? tsPath : undefined ,
439- pgModule : options . pgModule ,
468+ target : options . target ,
469+ module : options . module ,
440470 } )
441471 ) ,
442472 TaskEither . chain ( tsCode => async ( ) => {
@@ -474,14 +504,15 @@ function processSQLDirectory(
474504 options : Options
475505) : Task . Task < TsModuleDir > {
476506 const successfulModules = pipe ( modules , Array . filterMap ( identity ) )
507+ const hasErrors = modules . some ( Option . isNone )
477508 return pipe (
478509 maybeWriteIndexModule (
479510 options . index ,
480511 dirPath ,
481512 successfulModules ,
482513 options . prettify
483514 ) ,
484- Task . map ( modules => ( { dirPath, modules } ) )
515+ Task . map ( modules => ( { hasErrors , dirPath, modules } ) )
485516 )
486517}
487518
0 commit comments