Skip to content

Commit c1fbdae

Browse files
author
Piet van Agtmaal
committed
Refactor CLI path handling, fixing several bugs
1 parent e735ff2 commit c1fbdae

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
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

+22-17
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Options
3232

3333
const OUTPUT_FILE = "FILE";
3434
const OUTPUT_STDOUT = "STDOUT";
35-
const CWD = new URL(`file://${process.cwd()}/`);
35+
const CWD = process.cwd();
3636
const EXT_RE = /\.[^.]+$/i;
3737
const HTTP_RE = /^https?:\/\//;
3838

@@ -70,7 +70,7 @@ const flags = parser(args, {
7070
},
7171
});
7272

73-
async function generateSchema(pathToSpec) {
73+
async function generateSchema(pathToSpec, outputFile) {
7474
const output = flags.output ? OUTPUT_FILE : OUTPUT_STDOUT; // FILE or STDOUT
7575

7676
// Parse incoming headers from CLI flags
@@ -111,15 +111,11 @@ async function generateSchema(pathToSpec) {
111111

112112
// output
113113
if (output === OUTPUT_FILE) {
114-
let outputFilePath = new URL(flags.output, CWD); // note: may be directory
114+
let outputFilePath = outputFile; // 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");
118-
const originalOutputFilePath = outputFilePath;
119-
outputFilePath = new URL(filename, originalOutputFilePath);
120-
if (outputFilePath.protocol !== 'file:') {
121-
outputFilePath = new URL(outputFilePath.host.replace(EXT_RE, ".ts"), originalOutputFilePath);
122-
}
117+
const filename = path.basename(pathToSpec).replace(EXT_RE, ".ts");
118+
outputFilePath = path.join(outputFilePath, filename);
123119
}
124120

125121
fs.writeFileSync(outputFilePath, result, "utf8");
@@ -140,15 +136,24 @@ async function main() {
140136
console.info(HELP);
141137
process.exit(0);
142138
}
143-
const packageJSON = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"));
139+
const packageJSON = (await import("../package.json", { assert: { type: 'json' }})).default;
144140
if ("version" in flags) {
145141
console.info(`v${packageJSON.version}`);
146142
process.exit(0);
147143
}
148144

149145
let output = flags.output ? OUTPUT_FILE : OUTPUT_STDOUT; // FILE or STDOUT
150-
let outputFile = new URL(flags.output, CWD);
151-
let outputDir = new URL(".", outputFile);
146+
let outputFile
147+
if (flags.output) {
148+
if (path.isAbsolute(flags.output)) {
149+
outputFile = flags.output;
150+
} else {
151+
outputFile = path.join(CWD, flags.output);
152+
}
153+
} else {
154+
outputFile = CWD;
155+
}
156+
let outputDir = path.dirname(outputFile);
152157

153158
if (output === OUTPUT_FILE) console.info(`✨ ${c.bold(`openapi-typescript ${packageJSON.version}`)}`); // only log if we’re NOT writing to stdout
154159

@@ -188,14 +193,14 @@ async function main() {
188193
await Promise.all(
189194
inputSpecPaths.map(async (specPath) => {
190195
if (flags.output !== "." && output === OUTPUT_FILE) {
196+
// recursively make parent dirs. For globs, the passed output target is a directory, else it is a file
191197
if (isGlob) {
192-
fs.mkdirSync(new URL(path.dirname(specPath), outputDir), { recursive: true }); // recursively make parent dirs
193-
}
194-
else {
195-
fs.mkdirSync(outputDir, { recursive: true }); // recursively make parent dirs
198+
fs.mkdirSync(outputFile, { recursive: true });
199+
} else {
200+
fs.mkdirSync(outputDir, { recursive: true });
196201
}
197202
}
198-
await generateSchema(specPath);
203+
await generateSchema(specPath, outputFile);
199204
})
200205
);
201206
}

0 commit comments

Comments
 (0)