@@ -17,6 +17,7 @@ import {z} from "zod/v4"
1717import { promptContinue } from "./core/cli-utils"
1818import { NodeFsAdaptor } from "./core/file-system/node-fs-adaptor"
1919import type { OperationGroupStrategy } from "./core/input"
20+ import { loadPackageJson } from "./core/loaders/package.json.loader"
2021import { loadTsConfigCompilerOptions } from "./core/loaders/tsconfig.loader"
2122import {
2223 loadTypescriptFormatterConfig ,
@@ -30,7 +31,7 @@ import {configSchema, generate} from "./index"
3031import { templateNames } from "./templates"
3132import type { ServerImplementationMethod } from "./templates.types"
3233
33- export const boolParser = ( arg : string ) : boolean => {
34+ const optionalBoolParser = ( arg : string ) : boolean | undefined = > {
3435 const TRUTHY_VALUES = [ "true" , "1" , "on" ]
3536 const FALSY_VALUES = [ "false" , "0" , "off" , "" ]
3637
@@ -41,6 +42,10 @@ export const boolParser = (arg: string): boolean => {
4142 return false
4243 }
4344
45+ if ( ! arg ) {
46+ return undefined
47+ }
48+
4449 throw new InvalidArgumentError (
4550 `'${ arg } ' is not a valid boolean parameter. Valid truthy values are: ${ TRUTHY_VALUES . map (
4651 ( it ) => JSON . stringify ( it ) ,
@@ -50,6 +55,16 @@ export const boolParser = (arg: string): boolean => {
5055 )
5156}
5257
58+ export const boolParser = ( arg : string ) : boolean => {
59+ const result = optionalBoolParser ( arg )
60+
61+ if ( result === undefined ) {
62+ throw new InvalidArgumentError ( `'${ arg } ' is not a valid boolean parameter.` )
63+ }
64+
65+ return result
66+ }
67+
5368export const remoteSpecRequestHeadersParser = ( arg : string ) => {
5469 return z
5570 . preprocess (
@@ -123,6 +138,14 @@ const program = new Command()
123138 . argParser ( boolParser )
124139 . default ( false ) ,
125140 )
141+ . addOption (
142+ new Option (
143+ "--ts-is-esm-project [bool]" ,
144+ `(typescript) whether the target project uss esm or commonjs. auto-detected from package.json when omitted.` ,
145+ )
146+ . env ( "OPENAPI_TS_IS_ESM_PROJECT" )
147+ . argParser ( optionalBoolParser ) ,
148+ )
126149 . addOption (
127150 new Option (
128151 "--ts-server-implementation-method <value>" ,
@@ -285,19 +308,25 @@ async function main() {
285308 )
286309
287310 const outputPath = path . join ( process . cwd ( ) , config . output )
311+
288312 const formatterOptions = await loadTypescriptFormatterConfig (
289313 outputPath ,
290314 fsAdaptor ,
291315 )
316+
292317 const formatter = await formatterFactory ( formatterOptions )
293318
319+ const projectPackageJson = await loadPackageJson ( outputPath , fsAdaptor )
320+
294321 const compilerOptions = await loadTsConfigCompilerOptions (
295322 outputPath ,
296323 fsAdaptor ,
297324 )
298325
299326 await generate (
300327 configSchema . parse ( {
328+ // can be overridden by config spread
329+ tsIsEsmProject : projectPackageJson . type === "module" ,
301330 ...config ,
302331 tsCompilerOptions : compilerOptions ,
303332 } ) ,
0 commit comments