Skip to content

Commit ebd31ff

Browse files
pvanagtmaalPiet van Agtmaal
and
Piet van Agtmaal
authored
improve CLI output path handling (#1281)
Fixes: - output directories without trailing slash - output directories with globs - output directories when the converted yaml is more than one level deeper than cwd (e.g. `./bin/cli.js 'test/fixtures/*.yaml' --output foo`) Co-authored-by: Piet van Agtmaal <[email protected]>
1 parent d93755b commit ebd31ff

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

.changeset/gold-gorillas-kiss.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Refactor CLI path handling, fixing several bugs

packages/openapi-typescript/bin/cli.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
import fs from "node:fs";
4-
import path from "path";
4+
import path from "node:path";
55
import { URL } from "node:url";
66
import glob from "fast-glob";
77
import parser from "yargs-parser";
@@ -114,7 +114,10 @@ async function generateSchema(pathToSpec) {
114114
let outputFilePath = new URL(flags.output, CWD); // note: may be directory
115115
const isDir = fs.existsSync(outputFilePath) && fs.lstatSync(outputFilePath).isDirectory();
116116
if (isDir) {
117-
const filename = pathToSpec.replace(EXT_RE, ".ts");
117+
if (typeof flags.output === 'string' && !flags.output.endsWith('/')) {
118+
outputFilePath = new URL(`${flags.output}/`, CWD)
119+
}
120+
const filename = path.basename(pathToSpec).replace(EXT_RE, ".ts");
118121
const originalOutputFilePath = outputFilePath;
119122
outputFilePath = new URL(filename, originalOutputFilePath);
120123
if (outputFilePath.protocol !== 'file:') {
@@ -189,7 +192,7 @@ async function main() {
189192
inputSpecPaths.map(async (specPath) => {
190193
if (flags.output !== "." && output === OUTPUT_FILE) {
191194
if (isGlob) {
192-
fs.mkdirSync(new URL(path.dirname(specPath), outputDir), { recursive: true }); // recursively make parent dirs
195+
fs.mkdirSync(outputFile, { recursive: true }); // recursively make parent dirs
193196
}
194197
else {
195198
fs.mkdirSync(outputDir, { recursive: true }); // recursively make parent dirs

0 commit comments

Comments
 (0)