1
1
#!/usr/bin/env node
2
2
3
- import fs from "fs" ;
4
- import path from "path " ;
5
- import glob from "tiny -glob" ;
3
+ import fs from "node: fs" ;
4
+ import { URL } from "node:url " ;
5
+ import glob from "fast -glob" ;
6
6
import parser from "yargs-parser" ;
7
7
import openapiTS from "../dist/index.js" ;
8
8
@@ -35,6 +35,8 @@ Options
35
35
36
36
const OUTPUT_FILE = "FILE" ;
37
37
const OUTPUT_STDOUT = "STDOUT" ;
38
+ const CWD = new URL ( `file://${ process . cwd ( ) } /` ) ;
39
+ const EXT_RE = / \. [ ^ . ] + $ / i;
38
40
39
41
const timeStart = process . hrtime ( ) ;
40
42
@@ -114,11 +116,11 @@ async function generateSchema(pathToSpec) {
114
116
115
117
// output
116
118
if ( output === OUTPUT_FILE ) {
117
- let outputFilePath = path . resolve ( process . cwd ( ) , flags . output ) ; // note: may be directory
119
+ let outputFilePath = new URL ( flags . output , CWD ) ; // note: may be directory
118
120
const isDir = fs . existsSync ( outputFilePath ) && fs . lstatSync ( outputFilePath ) . isDirectory ( ) ;
119
121
if ( isDir ) {
120
- const filename = pathToSpec . replace ( new RegExp ( ` ${ path . extname ( pathToSpec ) } $` ) , ".ts" ) ;
121
- outputFilePath = path . join ( outputFilePath , filename ) ;
122
+ const filename = pathToSpec . replace ( EXT_RE , ".ts" ) ;
123
+ outputFilePath = new URL ( filename , outputFilePath ) ;
122
124
}
123
125
124
126
fs . writeFileSync ( outputFilePath , result , "utf8" ) ;
@@ -141,6 +143,9 @@ async function main() {
141
143
}
142
144
143
145
let output = flags . output ? OUTPUT_FILE : OUTPUT_STDOUT ; // FILE or STDOUT
146
+ let outputFile = new URL ( flags . output , CWD ) ;
147
+ let outputDir = new URL ( "." , outputFile ) ;
148
+
144
149
const pathToSpec = input ;
145
150
146
151
if ( output === OUTPUT_FILE ) {
@@ -155,20 +160,20 @@ async function main() {
155
160
156
161
// handle remote schema, exit
157
162
if ( / ^ h t t p s ? : \/ \/ / . test ( pathToSpec ) ) {
158
- if ( output !== "." && output === OUTPUT_FILE ) fs . mkdirSync ( path . dirname ( flags . output ) , { recursive : true } ) ;
163
+ if ( output !== "." && output === OUTPUT_FILE ) fs . mkdirSync ( outputDir , { recursive : true } ) ;
159
164
await generateSchema ( pathToSpec ) ;
160
165
return ;
161
166
}
162
167
163
168
// handle stdin schema, exit
164
169
if ( pathToSpec === "-" ) {
165
- if ( output !== "." && output === OUTPUT_FILE ) fs . mkdirSync ( path . dirname ( flags . output ) , { recursive : true } ) ;
170
+ if ( output !== "." && output === OUTPUT_FILE ) fs . mkdirSync ( outputDir , { recursive : true } ) ;
166
171
await generateSchema ( process . stdin ) ;
167
172
return ;
168
173
}
169
174
170
175
// handle local schema(s)
171
- const inputSpecPaths = await glob ( pathToSpec , { filesOnly : true } ) ;
176
+ const inputSpecPaths = await glob ( pathToSpec ) ;
172
177
const isGlob = inputSpecPaths . length > 1 ;
173
178
174
179
// error: no matches for glob
@@ -177,20 +182,15 @@ async function main() {
177
182
}
178
183
179
184
// error: tried to glob output to single file
180
- if ( isGlob && output === OUTPUT_FILE && fs . existsSync ( flags . output ) && fs . lstatSync ( flags . output ) . isFile ( ) ) {
185
+ if ( isGlob && output === OUTPUT_FILE && fs . existsSync ( outputDir ) && fs . lstatSync ( outputDir ) . isFile ( ) ) {
181
186
errorAndExit ( `❌ Expected directory for --output if using glob patterns. Received "${ flags . output } ".` ) ;
182
187
}
183
188
184
189
// generate schema(s) in parallel
185
190
await Promise . all (
186
191
inputSpecPaths . map ( async ( specPath ) => {
187
192
if ( flags . output !== "." && output === OUTPUT_FILE ) {
188
- let outputDir = path . resolve ( process . cwd ( ) , flags . output ) ;
189
- if ( isGlob ) {
190
- outputDir = path . resolve ( outputDir , path . dirname ( specPath ) ) ; // globs: use output dir + spec dir
191
- } else {
192
- outputDir = path . dirname ( outputDir ) ; // single files: just use output parent dir
193
- }
193
+ if ( isGlob ) outputDir = new URL ( specPath , outputDir ) ;
194
194
fs . mkdirSync ( outputDir , { recursive : true } ) ; // recursively make parent dirs
195
195
}
196
196
await generateSchema ( specPath ) ;
0 commit comments