From 4b20b25fd4265e4347c79712cadc5a018ea485f0 Mon Sep 17 00:00:00 2001 From: Piet van Agtmaal Date: Tue, 8 Aug 2023 16:09:31 +0200 Subject: [PATCH] improve CLI output path handling 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`) --- .changeset/gold-gorillas-kiss.md | 5 +++++ packages/openapi-typescript/bin/cli.js | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 .changeset/gold-gorillas-kiss.md diff --git a/.changeset/gold-gorillas-kiss.md b/.changeset/gold-gorillas-kiss.md new file mode 100644 index 000000000..d9e579230 --- /dev/null +++ b/.changeset/gold-gorillas-kiss.md @@ -0,0 +1,5 @@ +--- +"openapi-typescript": patch +--- + +Refactor CLI path handling, fixing several bugs diff --git a/packages/openapi-typescript/bin/cli.js b/packages/openapi-typescript/bin/cli.js index 75a8c750b..c73b445c3 100755 --- a/packages/openapi-typescript/bin/cli.js +++ b/packages/openapi-typescript/bin/cli.js @@ -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"; @@ -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:') { @@ -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