11#!/usr/bin/env node
22
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" ;
66import parser from "yargs-parser" ;
77import openapiTS from "../dist/index.js" ;
88
@@ -35,6 +35,8 @@ Options
3535
3636const OUTPUT_FILE = "FILE" ;
3737const OUTPUT_STDOUT = "STDOUT" ;
38+ const CWD = new URL ( `file://${ process . cwd ( ) } /` ) ;
39+ const EXT_RE = / \. [ ^ . ] + $ / i;
3840
3941const timeStart = process . hrtime ( ) ;
4042
@@ -114,11 +116,11 @@ async function generateSchema(pathToSpec) {
114116
115117 // output
116118 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
118120 const isDir = fs . existsSync ( outputFilePath ) && fs . lstatSync ( outputFilePath ) . isDirectory ( ) ;
119121 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 ) ;
122124 }
123125
124126 fs . writeFileSync ( outputFilePath , result , "utf8" ) ;
@@ -141,6 +143,9 @@ async function main() {
141143 }
142144
143145 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+
144149 const pathToSpec = input ;
145150
146151 if ( output === OUTPUT_FILE ) {
@@ -155,20 +160,20 @@ async function main() {
155160
156161 // handle remote schema, exit
157162 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 } ) ;
159164 await generateSchema ( pathToSpec ) ;
160165 return ;
161166 }
162167
163168 // handle stdin schema, exit
164169 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 } ) ;
166171 await generateSchema ( process . stdin ) ;
167172 return ;
168173 }
169174
170175 // handle local schema(s)
171- const inputSpecPaths = await glob ( pathToSpec , { filesOnly : true } ) ;
176+ const inputSpecPaths = await glob ( pathToSpec ) ;
172177 const isGlob = inputSpecPaths . length > 1 ;
173178
174179 // error: no matches for glob
@@ -177,20 +182,15 @@ async function main() {
177182 }
178183
179184 // 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 ( ) ) {
181186 errorAndExit ( `❌ Expected directory for --output if using glob patterns. Received "${ flags . output } ".` ) ;
182187 }
183188
184189 // generate schema(s) in parallel
185190 await Promise . all (
186191 inputSpecPaths . map ( async ( specPath ) => {
187192 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 ) ;
194194 fs . mkdirSync ( outputDir , { recursive : true } ) ; // recursively make parent dirs
195195 }
196196 await generateSchema ( specPath ) ;
0 commit comments