11#!/usr/bin/env node
22
3- import { command , run , option , string , flag , boolean , positional } from 'cmd-ts' ;
3+ import {
4+ command ,
5+ run ,
6+ option ,
7+ string ,
8+ optional ,
9+ flag ,
10+ boolean ,
11+ positional ,
12+ } from 'cmd-ts' ;
413import * as E from 'fp-ts/Either' ;
514import * as fs from 'fs' ;
615import * as p from 'path' ;
@@ -11,6 +20,7 @@ import { convertRoutesToOpenAPI } from './openapi';
1120import type { Route } from './route' ;
1221import type { Schema } from './ir' ;
1322import { Project } from './project' ;
23+ import { KNOWN_IMPORTS } from './knownImports' ;
1424
1525const app = command ( {
1626 name : 'api-ts' ,
@@ -57,11 +67,30 @@ const app = command({
5767 short : 'i' ,
5868 defaultValue : ( ) => false ,
5969 } ) ,
70+ codecFile : option ( {
71+ type : optional ( string ) ,
72+ description : 'Custom codec definition file' ,
73+ long : 'codec-file' ,
74+ short : 'c' ,
75+ defaultValue : ( ) => undefined ,
76+ } ) ,
6077 } ,
61- handler : async ( { input, name, version } ) => {
78+ handler : async ( { input, name, version, codecFile } ) => {
6279 const filePath = p . resolve ( input ) ;
6380
64- const project = await new Project ( ) . parseEntryPoint ( filePath ) ;
81+ let knownImports = KNOWN_IMPORTS ;
82+ if ( codecFile !== undefined ) {
83+ const codecFilePath = p . resolve ( codecFile ) ;
84+ const codecModule = await import ( codecFilePath ) ;
85+ if ( codecModule . default === undefined ) {
86+ console . error ( `Could not find default export in ${ codecFilePath } ` ) ;
87+ process . exit ( 1 ) ;
88+ }
89+ const customCodecs = codecModule . default ( E ) ;
90+ knownImports = { ...knownImports , ...customCodecs } ;
91+ }
92+
93+ const project = await new Project ( { } , knownImports ) . parseEntryPoint ( filePath ) ;
6594 if ( E . isLeft ( project ) ) {
6695 console . error ( project . left ) ;
6796 process . exit ( 1 ) ;
0 commit comments