@@ -32,7 +32,7 @@ Options
32
32
33
33
const OUTPUT_FILE = "FILE" ;
34
34
const OUTPUT_STDOUT = "STDOUT" ;
35
- const CWD = new URL ( `file:// ${ process . cwd ( ) } /` ) ;
35
+ const CWD = process . cwd ( ) ;
36
36
const EXT_RE = / \. [ ^ . ] + $ / i;
37
37
const HTTP_RE = / ^ h t t p s ? : \/ \/ / ;
38
38
@@ -70,7 +70,7 @@ const flags = parser(args, {
70
70
} ,
71
71
} ) ;
72
72
73
- async function generateSchema ( pathToSpec ) {
73
+ async function generateSchema ( pathToSpec , outputFile ) {
74
74
const output = flags . output ? OUTPUT_FILE : OUTPUT_STDOUT ; // FILE or STDOUT
75
75
76
76
// Parse incoming headers from CLI flags
@@ -111,15 +111,11 @@ async function generateSchema(pathToSpec) {
111
111
112
112
// output
113
113
if ( output === OUTPUT_FILE ) {
114
- let outputFilePath = new URL ( flags . output , CWD ) ; // note: may be directory
114
+ let outputFilePath = outputFile ; // note: may be directory
115
115
const isDir = fs . existsSync ( outputFilePath ) && fs . lstatSync ( outputFilePath ) . isDirectory ( ) ;
116
116
if ( isDir ) {
117
- const filename = pathToSpec . replace ( EXT_RE , ".ts" ) ;
118
- const originalOutputFilePath = outputFilePath ;
119
- outputFilePath = new URL ( filename , originalOutputFilePath ) ;
120
- if ( outputFilePath . protocol !== 'file:' ) {
121
- outputFilePath = new URL ( outputFilePath . host . replace ( EXT_RE , ".ts" ) , originalOutputFilePath ) ;
122
- }
117
+ const filename = path . basename ( pathToSpec ) . replace ( EXT_RE , ".ts" ) ;
118
+ outputFilePath = path . join ( outputFilePath , filename ) ;
123
119
}
124
120
125
121
fs . writeFileSync ( outputFilePath , result , "utf8" ) ;
@@ -140,15 +136,24 @@ async function main() {
140
136
console . info ( HELP ) ;
141
137
process . exit ( 0 ) ;
142
138
}
143
- const packageJSON = JSON . parse ( fs . readFileSync ( new URL ( "../package.json" , import . meta . url ) , "utf8" ) ) ;
139
+ const packageJSON = ( await import ( "../package.json" , { assert : { type : 'json' } } ) ) . default ;
144
140
if ( "version" in flags ) {
145
141
console . info ( `v${ packageJSON . version } ` ) ;
146
142
process . exit ( 0 ) ;
147
143
}
148
144
149
145
let output = flags . output ? OUTPUT_FILE : OUTPUT_STDOUT ; // FILE or STDOUT
150
- let outputFile = new URL ( flags . output , CWD ) ;
151
- let outputDir = new URL ( "." , outputFile ) ;
146
+ let outputFile
147
+ if ( flags . output ) {
148
+ if ( path . isAbsolute ( flags . output ) ) {
149
+ outputFile = flags . output ;
150
+ } else {
151
+ outputFile = path . join ( CWD , flags . output ) ;
152
+ }
153
+ } else {
154
+ outputFile = CWD ;
155
+ }
156
+ let outputDir = path . dirname ( outputFile ) ;
152
157
153
158
if ( output === OUTPUT_FILE ) console . info ( `✨ ${ c . bold ( `openapi-typescript ${ packageJSON . version } ` ) } ` ) ; // only log if we’re NOT writing to stdout
154
159
@@ -188,14 +193,14 @@ async function main() {
188
193
await Promise . all (
189
194
inputSpecPaths . map ( async ( specPath ) => {
190
195
if ( flags . output !== "." && output === OUTPUT_FILE ) {
196
+ // recursively make parent dirs. For globs, the passed output target is a directory, else it is a file
191
197
if ( isGlob ) {
192
- fs . mkdirSync ( new URL ( path . dirname ( specPath ) , outputDir ) , { recursive : true } ) ; // recursively make parent dirs
193
- }
194
- else {
195
- fs . mkdirSync ( outputDir , { recursive : true } ) ; // recursively make parent dirs
198
+ fs . mkdirSync ( outputFile , { recursive : true } ) ;
199
+ } else {
200
+ fs . mkdirSync ( outputDir , { recursive : true } ) ;
196
201
}
197
202
}
198
- await generateSchema ( specPath ) ;
203
+ await generateSchema ( specPath , outputFile ) ;
199
204
} )
200
205
) ;
201
206
}
0 commit comments