Skip to content

Commit

Permalink
improve CLI output path handling
Browse files Browse the repository at this point in the history
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`)
  • Loading branch information
Piet van Agtmaal committed Aug 9, 2023
1 parent d93755b commit 701e52f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-gorillas-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Refactor CLI path handling, fixing several bugs
11 changes: 7 additions & 4 deletions packages/openapi-typescript/bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import fs from "node:fs";
import path from "path";
import path from "node:path";
import { URL } from "node:url";
import glob from "fast-glob";
import parser from "yargs-parser";
Expand Down Expand Up @@ -114,7 +114,10 @@ async function generateSchema(pathToSpec) {
let outputFilePath = new URL(flags.output, CWD); // note: may be directory
const isDir = fs.existsSync(outputFilePath) && fs.lstatSync(outputFilePath).isDirectory();
if (isDir) {
const filename = pathToSpec.replace(EXT_RE, ".ts");
if (typeof flags.output === 'string' && !flags.output.endsWith('/')) {
outputFilePath = new URL(`${flags.output}/`, CWD)
}
const filename = path.basename(pathToSpec).replace(EXT_RE, ".ts");
const originalOutputFilePath = outputFilePath;
outputFilePath = new URL(filename, originalOutputFilePath);
if (outputFilePath.protocol !== 'file:') {
Expand Down Expand Up @@ -189,7 +192,7 @@ async function main() {
inputSpecPaths.map(async (specPath) => {
if (flags.output !== "." && output === OUTPUT_FILE) {
if (isGlob) {
fs.mkdirSync(new URL(path.dirname(specPath), outputDir), { recursive: true }); // recursively make parent dirs
fs.mkdirSync(outputFile, { recursive: true }); // recursively make parent dirs
}
else {
fs.mkdirSync(outputDir, { recursive: true }); // recursively make parent dirs
Expand All @@ -200,4 +203,4 @@ async function main() {
);
}

main();
main();

0 comments on commit 701e52f

Please sign in to comment.